-
-
Notifications
You must be signed in to change notification settings - Fork 522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: support formats for analyze image #1177
Changes from all commits
d9767a4
5c61820
54c609f
1d27f7e
33f9859
89888b8
a971a18
6d90418
2d767fd
69504bf
7ec9e58
cdac3f7
84a121e
94bbad4
0c02087
3f5ad4d
f332965
f2902bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,28 +151,16 @@ class MobileScannerHandler( | |
null | ||
} | ||
|
||
var barcodeScannerOptions: BarcodeScannerOptions? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was refactored & moved to reuse it. |
||
if (formats != null) { | ||
val formatsList: MutableList<Int> = mutableListOf() | ||
for (formatValue in formats) { | ||
formatsList.add(BarcodeFormats.fromRawValue(formatValue).intValue) | ||
} | ||
barcodeScannerOptions = if (formatsList.size == 1) { | ||
BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first()) | ||
.build() | ||
} else { | ||
BarcodeScannerOptions.Builder().setBarcodeFormats( | ||
formatsList.first(), | ||
*formatsList.subList(1, formatsList.size).toIntArray() | ||
).build() | ||
} | ||
} | ||
val barcodeScannerOptions: BarcodeScannerOptions? = buildBarcodeScannerOptions(formats) | ||
|
||
val position = | ||
if (facing == 0) CameraSelector.DEFAULT_FRONT_CAMERA else CameraSelector.DEFAULT_BACK_CAMERA | ||
|
||
val detectionSpeed: DetectionSpeed = if (speed == 0) DetectionSpeed.NO_DUPLICATES | ||
else if (speed ==1) DetectionSpeed.NORMAL else DetectionSpeed.UNRESTRICTED | ||
val detectionSpeed: DetectionSpeed = when (speed) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a refactor that was proposed by the Kotlin language tooling |
||
0 -> DetectionSpeed.NO_DUPLICATES | ||
1 -> DetectionSpeed.NORMAL | ||
else -> DetectionSpeed.UNRESTRICTED | ||
} | ||
|
||
mobileScanner!!.start( | ||
barcodeScannerOptions, | ||
|
@@ -243,13 +231,13 @@ class MobileScannerHandler( | |
|
||
private fun analyzeImage(call: MethodCall, result: MethodChannel.Result) { | ||
analyzerResult = result | ||
val uri = Uri.fromFile(File(call.arguments.toString())) | ||
|
||
// TODO: parse options from the method call | ||
// See https://github.com/juliansteenbakker/mobile_scanner/issues/1069 | ||
val formats: List<Int>? = call.argument<List<Int>>("formats") | ||
val filePath: String = call.argument<String>("filePath")!! | ||
|
||
mobileScanner!!.analyzeImage( | ||
uri, | ||
null, | ||
Uri.fromFile(File(filePath)), | ||
buildBarcodeScannerOptions(formats), | ||
analyzeImageSuccessCallback, | ||
analyzeImageErrorCallback) | ||
} | ||
|
@@ -284,4 +272,26 @@ class MobileScannerHandler( | |
|
||
result.success(null) | ||
} | ||
|
||
private fun buildBarcodeScannerOptions(formats: List<Int>?): BarcodeScannerOptions? { | ||
if (formats == null) { | ||
return null | ||
} | ||
|
||
val formatsList: MutableList<Int> = mutableListOf() | ||
|
||
for (formatValue in formats) { | ||
formatsList.add(BarcodeFormats.fromRawValue(formatValue).intValue) | ||
} | ||
|
||
if (formatsList.size == 1) { | ||
return BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first()) | ||
.build() | ||
} | ||
|
||
return BarcodeScannerOptions.Builder().setBarcodeFormats( | ||
formatsList.first(), | ||
*formatsList.subList(1, formatsList.size).toIntArray() | ||
).build() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:image_picker/image_picker.dart'; | ||
import 'package:mobile_scanner/mobile_scanner.dart'; | ||
|
||
class BarcodeScannerAnalyzeImage extends StatefulWidget { | ||
const BarcodeScannerAnalyzeImage({super.key}); | ||
|
||
@override | ||
State<BarcodeScannerAnalyzeImage> createState() => | ||
_BarcodeScannerAnalyzeImageState(); | ||
} | ||
|
||
class _BarcodeScannerAnalyzeImageState | ||
extends State<BarcodeScannerAnalyzeImage> { | ||
final MobileScannerController _controller = MobileScannerController(); | ||
|
||
BarcodeCapture? _barcodeCapture; | ||
|
||
Future<void> _analyzeImageFromFile() async { | ||
try { | ||
final XFile? file = | ||
await ImagePicker().pickImage(source: ImageSource.gallery); | ||
|
||
if (!mounted || file == null) { | ||
return; | ||
} | ||
|
||
final BarcodeCapture? barcodeCapture = | ||
await _controller.analyzeImage(file.path); | ||
|
||
if (mounted) { | ||
setState(() { | ||
_barcodeCapture = barcodeCapture; | ||
}); | ||
} | ||
} catch (_) {} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
Widget label = const Text('Pick a file to detect barcode'); | ||
|
||
if (_barcodeCapture != null) { | ||
label = Text( | ||
_barcodeCapture?.barcodes.firstOrNull?.displayValue ?? | ||
'No barcode detected', | ||
); | ||
} | ||
|
||
return Scaffold( | ||
appBar: AppBar(title: const Text('Analyze image from file')), | ||
body: Column( | ||
children: [ | ||
Expanded( | ||
child: Center( | ||
child: ElevatedButton( | ||
onPressed: kIsWeb ? null : _analyzeImageFromFile, | ||
child: kIsWeb | ||
? const Text('Analyze image is not supported on web') | ||
: const Text('Choose file'), | ||
), | ||
), | ||
), | ||
Expanded(child: Center(child: label)), | ||
], | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_controller.dispose(); | ||
super.dispose(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,16 +39,16 @@ class _BarcodeScannerWithScanWindowState | |
final scannedBarcode = barcodeCapture.barcodes.first; | ||
|
||
// No barcode corners, or size, or no camera preview size. | ||
if (scannedBarcode.corners.isEmpty || | ||
value.size.isEmpty || | ||
barcodeCapture.size.isEmpty) { | ||
if (value.size.isEmpty || | ||
scannedBarcode.size.isEmpty || | ||
scannedBarcode.corners.isEmpty) { | ||
return const SizedBox(); | ||
} | ||
|
||
return CustomPaint( | ||
painter: BarcodeOverlay( | ||
barcodeCorners: scannedBarcode.corners, | ||
barcodeSize: barcodeCapture.size, | ||
barcodeSize: scannedBarcode.size, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "barcode size" is wrong here |
||
boxFit: BoxFit.contain, | ||
cameraPreviewSize: value.size, | ||
), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to update this one for
returnImage