casvanluijtelaar

refactor ios to use rectOfInterest

@@ -27,9 +27,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -27,9 +27,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
27 var analyzing: Bool = false 27 var analyzing: Bool = false
28 var position = AVCaptureDevice.Position.back 28 var position = AVCaptureDevice.Position.back
29 29
30 - // optional frame to crop camera image before barcode detection  
31 - var scanWindow: CGRect? = nil;  
32 -  
33 var scanner = BarcodeScanner.barcodeScanner() 30 var scanner = BarcodeScanner.barcodeScanner()
34 31
35 public static func register(with registrar: FlutterPluginRegistrar) { 32 public static func register(with registrar: FlutterPluginRegistrar) {
@@ -103,22 +100,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -103,22 +100,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
103 } 100 }
104 analyzing = true 101 analyzing = true
105 let buffer = CMSampleBufferGetImageBuffer(sampleBuffer) 102 let buffer = CMSampleBufferGetImageBuffer(sampleBuffer)
  103 + var image = VisionImage(image: buffer!.image)
106 104
107 - var image: VisionImage?  
108 -  
109 - if (scanWindow != nil) {  
110 - let cropped = cropSampleBuffer(imageBuffer: buffer!, cropRect: scanWindow!)  
111 - image = VisionImage(image: cropped)  
112 - } else {  
113 - image = VisionImage(image: buffer!.image)  
114 - }  
115 -  
116 - image!.orientation = imageOrientation( 105 + image.orientation = imageOrientation(
117 deviceOrientation: UIDevice.current.orientation, 106 deviceOrientation: UIDevice.current.orientation,
118 defaultOrientation: .portrait 107 defaultOrientation: .portrait
119 ) 108 )
120 109
121 - scanner.process(image!) { [self] barcodes, error in 110 + scanner.process(image) { [self] barcodes, error in
122 if error == nil && barcodes != nil { 111 if error == nil && barcodes != nil {
123 for barcode in barcodes! { 112 for barcode in barcodes! {
124 let event: [String: Any?] = ["name": "barcode", "data": barcode.data] 113 let event: [String: Any?] = ["name": "barcode", "data": barcode.data]
@@ -132,29 +121,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -132,29 +121,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
132 // } 121 // }
133 } 122 }
134 123
135 - public func cropSampleBuffer(imageBuffer: CVImageBuffer, cropRect: CGRect) -> UIImage {  
136 - CVPixelBufferLockBaseAddress(imageBuffer, .readOnly)  
137 -  
138 - let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)!  
139 - let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)  
140 - let cropX = Int(cropRect.minX)  
141 - let cropY = Int(cropRect.minY)  
142 - let cropWidth = Int(cropRect.width)  
143 - let cropHeight = Int(cropRect.height)  
144 - let colorSpace = CGColorSpaceCreateDeviceRGB()  
145 -  
146 - // calculate start position  
147 - let bytesPerPixel = 4  
148 - let startAddress = baseAddress + cropY * bytesPerRow + cropX * bytesPerPixel  
149 -  
150 - let context = CGContext(data: startAddress, width: cropWidth, height: cropHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue)  
151 - CVPixelBufferUnlockBaseAddress(imageBuffer, .readOnly)  
152 -  
153 - // create image  
154 - let cgImage: CGImage = context!.makeImage()!  
155 - return UIImage(cgImage: cgImage)  
156 - }  
157 -  
158 func imageOrientation( 124 func imageOrientation(
159 deviceOrientation: UIDeviceOrientation, 125 deviceOrientation: UIDeviceOrientation,
160 defaultOrientation: UIDeviceOrientation 126 defaultOrientation: UIDeviceOrientation
@@ -209,15 +175,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -209,15 +175,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
209 let facing: Int = argReader.int(key: "facing") ?? 1 175 let facing: Int = argReader.int(key: "facing") ?? 1
210 let formats: Array = argReader.intArray(key: "formats") ?? [] 176 let formats: Array = argReader.intArray(key: "formats") ?? []
211 177
212 - let scanWindowData: Array? = argReader.intArray(key: "scanWindow")  
213 - if(scanWindowData != nil) {  
214 - scanWindow = CGRect(  
215 - x: scanWindowData![0],  
216 - y: scanWindowData![1],  
217 - width: scanWindowData![2] - scanWindowData![0],  
218 - height: scanWindowData![3] - scanWindowData![1])  
219 - }  
220 -  
221 let formatList: NSMutableArray = [] 178 let formatList: NSMutableArray = []
222 for index in formats { 179 for index in formats {
223 formatList.add(BarcodeFormat(rawValue: index)) 180 formatList.add(BarcodeFormat(rawValue: index))
@@ -283,6 +240,23 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -283,6 +240,23 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
283 } 240 }
284 captureSession.commitConfiguration() 241 captureSession.commitConfiguration()
285 captureSession.startRunning() 242 captureSession.startRunning()
  243 +
  244 + /// limit captureSession area of interest to the scanWindow if provided
  245 + let scanWindowData: Array? = argReader.intArray(key: "scanWindow")
  246 + if(scanWindowData != nil) {
  247 +
  248 + let captureMetadataOutput = AVCaptureMetadataOutput()
  249 +
  250 + captureMetadataOutput.rectOfInterest = CGRect(
  251 + x: scanWindowData![0],
  252 + y: scanWindowData![1],
  253 + width: scanWindowData![2] - scanWindowData![0],
  254 + height: scanWindowData![3] - scanWindowData![1])
  255 +
  256 + captureSession.addOutput(captureMetadataOutput)
  257 + }
  258 +
  259 +
286 let demensions = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription) 260 let demensions = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription)
287 let width = Double(demensions.height) 261 let width = Double(demensions.height)
288 let height = Double(demensions.width) 262 let height = Double(demensions.width)