Navaron Bracke

provide image width/height on iOS/MacOS even when returnImage is false

@@ -69,22 +69,18 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -69,22 +69,18 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
69 return 69 return
70 } 70 }
71 71
72 - if (!MobileScannerPlugin.returnImage) {  
73 - barcodeHandler.publishEvent([  
74 - "name": "barcode",  
75 - "data": barcodesMap,  
76 - ])  
77 - return  
78 - } 72 + // The image dimensions are always provided.
  73 + // The image bytes are only non-null when `returnImage` is true.
  74 + let imageData: [String: Any?] = [
  75 + "bytes": MobileScannerPlugin.returnImage ? FlutterStandardTypedData(bytes: image.jpegData(compressionQuality: 0.8)!) : nil,
  76 + "width": image.size.width,
  77 + "height": image.size.height,
  78 + ]
79 79
80 barcodeHandler.publishEvent([ 80 barcodeHandler.publishEvent([
81 "name": "barcode", 81 "name": "barcode",
82 "data": barcodesMap, 82 "data": barcodesMap,
83 - "image": [  
84 - "bytes": FlutterStandardTypedData(bytes: image.jpegData(compressionQuality: 0.8)!),  
85 - "width": image.size.width,  
86 - "height": image.size.height,  
87 - ], 83 + "image": imageData,
88 ]) 84 ])
89 }, torchModeChangeCallback: { torchState in 85 }, torchModeChangeCallback: { torchState in
90 barcodeHandler.publishEvent(["name": "torchState", "data": torchState]) 86 barcodeHandler.publishEvent(["name": "torchState", "data": torchState])
@@ -156,22 +156,26 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -156,22 +156,26 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
156 }) 156 })
157 157
158 DispatchQueue.main.async { 158 DispatchQueue.main.async {
159 - if (!MobileScannerPlugin.returnImage) { 159 + guard let image = cgImage else {
160 self?.sink?([ 160 self?.sink?([
161 "name": "barcode", 161 "name": "barcode",
162 "data": barcodes.map({ $0.toMap() }), 162 "data": barcodes.map({ $0.toMap() }),
163 ]) 163 ])
164 return 164 return
165 } 165 }
166 - 166 +
  167 + // The image dimensions are always provided.
  168 + // The image bytes are only non-null when `returnImage` is true.
  169 + let imageData: [String: Any?] = [
  170 + "bytes": MobileScannerPlugin.returnImage ? FlutterStandardTypedData(bytes: image.jpegData(compressionQuality: 0.8)!) : nil,
  171 + "width": Double(image.width),
  172 + "height": Double(image.height),
  173 + ]
  174 +
167 self?.sink?([ 175 self?.sink?([
168 "name": "barcode", 176 "name": "barcode",
169 "data": barcodes.map({ $0.toMap() }), 177 "data": barcodes.map({ $0.toMap() }),
170 - "image": cgImage == nil ? nil : [  
171 - "bytes": FlutterStandardTypedData(bytes: cgImage!.jpegData(compressionQuality: 0.8)!),  
172 - "width": Double(cgImage!.width),  
173 - "height": Double(cgImage!.height),  
174 - ], 178 + "image": imageData,
175 ]) 179 ])
176 } 180 }
177 }) 181 })