casvanluijtelaar

implemented ios

... ... @@ -2,6 +2,7 @@ import AVFoundation
import Flutter
import MLKitVision
import MLKitBarcodeScanning
import UIKit
public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, FlutterTexture, AVCaptureVideoDataOutputSampleBufferDelegate {
... ... @@ -25,6 +26,9 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
// var analyzeMode: Int = 0
var analyzing: Bool = false
var position = AVCaptureDevice.Position.back
// optional frame to crop camera image before barcode detection
var scanWindow: CGRect?
var scanner = BarcodeScanner.barcodeScanner()
... ... @@ -99,13 +103,22 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
}
analyzing = true
let buffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let image = VisionImage(image: buffer!.image)
image.orientation = imageOrientation(
var image: VisionImage?
if (scanWindow != nil) {
let cropped = cropSampleBuffer(imageBuffer: buffer!, cropRect: scanWindow!)
image = VisionImage(image: cropped)
} else {
image = VisionImage(image: buffer!.image)
}
image!.orientation = imageOrientation(
deviceOrientation: UIDevice.current.orientation,
defaultOrientation: .portrait
)
scanner.process(image) { [self] barcodes, error in
scanner.process(image!) { [self] barcodes, error in
if error == nil && barcodes != nil {
for barcode in barcodes! {
let event: [String: Any?] = ["name": "barcode", "data": barcode.data]
... ... @@ -118,6 +131,29 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
// break
// }
}
public func cropSampleBuffer(imageBuffer: CVImageBuffer, cropRect: CGRect) -> UIImage {
CVPixelBufferLockBaseAddress(imageBuffer, .readOnly)
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)!
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
let cropX = Int(cropRect.minX)
let cropY = Int(cropRect.minY)
let cropWidth = Int(cropRect.width)
let cropHeight = Int(cropRect.height)
let colorSpace = CGColorSpaceCreateDeviceRGB()
// calculate start position
let bytesPerPixel = 4
let startAddress = baseAddress + cropY * bytesPerRow + cropX * bytesPerPixel
let context = CGContext(data: startAddress, width: cropWidth, height: cropHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue)
CVPixelBufferUnlockBaseAddress(imageBuffer, .readOnly)
// create image
let cgImage: CGImage = context!.makeImage()!
return UIImage(cgImage: cgImage)
}
func imageOrientation(
deviceOrientation: UIDeviceOrientation,
... ... @@ -172,6 +208,15 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
let torch: Bool = argReader.bool(key: "torch") ?? false
let facing: Int = argReader.int(key: "facing") ?? 1
let formats: Array = argReader.intArray(key: "formats") ?? []
let scanWindowData: Array? = argReader.intArray(key: "scanWindow")
if(scanWindowData != nil) {
scanWindow = CGRect(
x: scanWindowData![0],
y: scanWindowData![1],
width: scanWindowData![2] - scanWindowData![0],
height: scanWindowData![3] - scanWindowData![1])
}
let formatList: NSMutableArray = []
for index in formats {
... ...