@@ -177,11 +177,11 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
177
177
} )
178
178
179
179
DispatchQueue . main. async {
180
+ // If the image is nil, use zero as the size.
180
181
guard let image = cgImage else {
181
- // Image not known, default image size to 1
182
182
self ? . sink ? ( [
183
183
" name " : " barcode " ,
184
- " data " : barcodes. map ( { $0. toMap ( width : 1 , height : 1 ) } ) ,
184
+ " data " : barcodes. map ( { $0. toMap ( imageWidth : 0 , imageHeight : 0 ) } ) ,
185
185
] )
186
186
return
187
187
}
@@ -194,10 +194,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
194
194
" height " : Double ( image. height) ,
195
195
]
196
196
197
-
198
197
self ? . sink ? ( [
199
198
" name " : " barcode " ,
200
- " data " : barcodes. map ( { $0. toMap ( width : image. width, height : image. height) } ) ,
199
+ " data " : barcodes. map ( { $0. toMap ( imageWidth : image. width, imageHeight : image. height) } ) ,
201
200
" image " : imageData,
202
201
] )
203
202
}
@@ -473,7 +472,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
473
472
device. torchMode = . on
474
473
device. unlockForConfiguration ( )
475
474
} catch ( _) {
476
-
475
+ // Do nothing.
477
476
}
478
477
}
479
478
@@ -526,7 +525,6 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
526
525
527
526
/// Set the zoom factor of the camera
528
527
func setScaleInternal( _ scale: CGFloat ) throws {
529
-
530
528
if ( device == nil ) {
531
529
throw MobileScannerError . zoomWhenStopped
532
530
}
@@ -545,10 +543,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
545
543
actualScale = min ( maxZoomFactor, actualScale)
546
544
547
545
// Limit to 1.0 scale
548
-
549
546
device. videoZoomFactor = actualScale
550
547
551
-
552
548
device. unlockForConfiguration ( )
553
549
#endif
554
550
} catch {
@@ -620,7 +616,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
620
616
try device. lockForConfiguration ( )
621
617
device. torchMode = newTorchMode
622
618
device. unlockForConfiguration ( )
623
- } catch ( _) { }
619
+ } catch ( _) {
620
+ // Do nothing.
621
+ }
624
622
625
623
result ( nil )
626
624
}
@@ -690,7 +688,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
690
688
691
689
result ( [
692
690
" name " : " barcode " ,
693
- " data " : barcodes. map ( { $0. toMap ( width : 1 , height : 1 ) } ) ,
691
+ " data " : barcodes. map ( { $0. toMap ( imageWidth : Int ( ciImage . extent . width ) , imageHeight : Int ( ciImage . extent . height ) ) } ) ,
694
692
] )
695
693
} )
696
694
@@ -803,28 +801,32 @@ extension VNBarcodeObservation {
803
801
return sqrt ( pow ( p1. x - p2. x, 2 ) + pow( p1. y - p2. y, 2 ) )
804
802
}
805
803
806
- public func toMap( width: Int , height: Int ) -> [ String : Any ? ] {
807
- let topLeftX = topLeft. x * CGFloat( width)
808
- let topRightX = topRight. x * CGFloat( width)
809
- let bottomRightX = bottomRight. x * CGFloat( width)
810
- let bottomLeftX = bottomLeft. x * CGFloat( width)
811
- let topLeftY = ( 1 - topLeft. y) * CGFloat( height)
812
- let topRightY = ( 1 - topRight. y) * CGFloat( height)
813
- let bottomRightY = ( 1 - bottomRight. y) * CGFloat( height)
814
- let bottomLeftY = ( 1 - bottomLeft. y) * CGFloat( height)
804
+ /// Map this `VNBarcodeObservation` to a dictionary.
805
+ ///
806
+ /// The `imageWidth` and `imageHeight` indicate the width and height of the input image that contains this observation.
807
+ public func toMap( imageWidth: Int , imageHeight: Int ) -> [ String : Any ? ] {
808
+ let topLeftX = topLeft. x * CGFloat( imageWidth)
809
+ let topRightX = topRight. x * CGFloat( imageWidth)
810
+ let bottomRightX = bottomRight. x * CGFloat( imageWidth)
811
+ let bottomLeftX = bottomLeft. x * CGFloat( imageWidth)
812
+ let topLeftY = ( 1 - topLeft. y) * CGFloat( imageHeight)
813
+ let topRightY = ( 1 - topRight. y) * CGFloat( imageHeight)
814
+ let bottomRightY = ( 1 - bottomRight. y) * CGFloat( imageHeight)
815
+ let bottomLeftY = ( 1 - bottomLeft. y) * CGFloat( imageHeight)
815
816
let data = [
817
+ // Clockwise, starting from the top-left corner.
816
818
" corners " : [
817
- [ " x " : bottomLeftX, " y " : bottomLeftY] ,
818
819
[ " x " : topLeftX, " y " : topLeftY] ,
819
820
[ " x " : topRightX, " y " : topRightY] ,
820
821
[ " x " : bottomRightX, " y " : bottomRightY] ,
822
+ [ " x " : bottomLeftX, " y " : bottomLeftY] ,
821
823
] ,
822
824
" format " : symbology. toInt ?? - 1 ,
823
825
" rawValue " : payloadStringValue ?? " " ,
824
826
" displayValue " : payloadStringValue ?? " " ,
825
827
" size " : [
826
- " width " : distanceBetween ( topLeft, topRight) * CGFloat( width ) ,
827
- " height " : distanceBetween ( topLeft, bottomLeft) * CGFloat( width ) ,
828
+ " width " : distanceBetween ( topLeft, topRight) * CGFloat( imageWidth ) ,
829
+ " height " : distanceBetween ( topLeft, bottomLeft) * CGFloat( imageHeight ) ,
828
830
] ,
829
831
] as [ String : Any ]
830
832
return data
0 commit comments