-
-
Notifications
You must be signed in to change notification settings - Fork 608
fix: support formats for analyze image #1177
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
Changes from 16 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 |
---|---|---|
|
@@ -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, | ||
), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | |
/// The selected camera | ||
var device: AVCaptureDevice! | ||
|
||
/// Barcode scanner for results | ||
var scanner = BarcodeScanner.barcodeScanner() | ||
/// The long lived barcode scanner for scanning barcodes from a camera preview. | ||
var scanner: BarcodeScanner? = nil | ||
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. |
||
|
||
/// Default position of camera | ||
var videoPosition: AVCaptureDevice.Position = AVCaptureDevice.Position.back | ||
|
@@ -146,7 +146,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | |
position: videoPosition | ||
) | ||
|
||
scanner.process(image) { [self] barcodes, error in | ||
scanner?.process(image) { [self] barcodes, error in | ||
imagesCurrentlyBeingProcessed = false | ||
|
||
if (detectionSpeed == DetectionSpeed.noDuplicates) { | ||
|
@@ -314,6 +314,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | |
textureId = nil | ||
captureSession = nil | ||
device = nil | ||
scanner = nil | ||
} | ||
|
||
/// Toggle the torch. | ||
|
@@ -431,13 +432,16 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | |
} | ||
|
||
/// Analyze a single image | ||
func analyzeImage(image: UIImage, position: AVCaptureDevice.Position, callback: @escaping BarcodeScanningCallback) { | ||
func analyzeImage(image: UIImage, position: AVCaptureDevice.Position, | ||
barcodeScannerOptions: BarcodeScannerOptions?, callback: @escaping BarcodeScanningCallback) { | ||
let image = VisionImage(image: image) | ||
image.orientation = imageOrientation( | ||
deviceOrientation: UIDevice.current.orientation, | ||
defaultOrientation: .portrait, | ||
position: position | ||
) | ||
|
||
let scanner: BarcodeScanner = barcodeScannerOptions != nil ? BarcodeScanner.barcodeScanner(options: barcodeScannerOptions!) : BarcodeScanner.barcodeScanner() | ||
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. Create a short lived scanner for the analyze image call |
||
|
||
scanner.process(image, completion: callback) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,16 +134,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | |
self.mobileScanner.timeoutSeconds = Double(timeoutMs) / Double(1000) | ||
MobileScannerPlugin.returnImage = returnImage | ||
|
||
let formatList = formats.map { format in return BarcodeFormat(rawValue: format)} | ||
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 |
||
var barcodeOptions: BarcodeScannerOptions? = nil | ||
|
||
if (formatList.count != 0) { | ||
var barcodeFormats: BarcodeFormat = [] | ||
for index in formats { | ||
barcodeFormats.insert(BarcodeFormat(rawValue: index)) | ||
} | ||
barcodeOptions = BarcodeScannerOptions(formats: barcodeFormats) | ||
} | ||
let barcodeOptions: BarcodeScannerOptions? = buildBarcodeScannerOptions(formats) | ||
|
||
let position = facing == 0 ? AVCaptureDevice.Position.front : .back | ||
let detectionSpeed: DetectionSpeed = DetectionSpeed(rawValue: speed)! | ||
|
@@ -262,7 +253,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | |
|
||
/// Analyzes a single image. | ||
private func analyzeImage(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { | ||
let uiImage = UIImage(contentsOfFile: call.arguments as? String ?? "") | ||
let formats: Array<Int> = (call.arguments as! Dictionary<String, Any?>)["formats"] as? Array ?? [] | ||
let scannerOptions: BarcodeScannerOptions? = buildBarcodeScannerOptions(formats) | ||
let uiImage = UIImage(contentsOfFile: (call.arguments as! Dictionary<String, Any?>)["filePath"] as? String ?? "") | ||
|
||
if (uiImage == nil) { | ||
result(FlutterError(code: "MobileScanner", | ||
|
@@ -271,7 +264,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | |
return | ||
} | ||
|
||
mobileScanner.analyzeImage(image: uiImage!, position: AVCaptureDevice.Position.back, callback: { barcodes, error in | ||
mobileScanner.analyzeImage(image: uiImage!, position: AVCaptureDevice.Position.back, | ||
barcodeScannerOptions: scannerOptions, callback: { barcodes, error in | ||
if error != nil { | ||
DispatchQueue.main.async { | ||
result(FlutterError(code: "MobileScanner", | ||
|
@@ -297,4 +291,18 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | |
} | ||
}) | ||
} | ||
|
||
private func buildBarcodeScannerOptions(_ formats: [Int]) -> BarcodeScannerOptions? { | ||
guard !formats.isEmpty else { | ||
return nil | ||
} | ||
|
||
var barcodeFormats: BarcodeFormat = [] | ||
|
||
for format in formats { | ||
barcodeFormats.insert(BarcodeFormat(rawValue: format)) | ||
} | ||
|
||
return BarcodeScannerOptions(formats: barcodeFormats) | ||
} | ||
} |
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