Julian Steenbakker

imp: fix build on macos

<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:Runner.xcodeproj">
</FileRef>
<FileRef
location = "group:Pods/Pods.xcodeproj">
</FileRef>
</Workspace>
... ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
... ...
//
// DetectionSpeed.swift
// mobile_scanner
//
// Created by Julian Steenbakker on 11/11/2022.
//
enum DetectionSpeed: Int {
case noDuplicates = 0
case normal = 1
case unrestricted = 2
}
... ...
import AVFoundation
import FlutterMacOS
import Vision
import UIKit
import AppKit
public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, FlutterTexture, AVCaptureVideoDataOutputSampleBufferDelegate {
... ... @@ -25,6 +25,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
// optional window to limit scan search
var scanWindow: CGRect?
var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates
// var analyzeMode: Int = 0
var analyzing: Bool = false
... ... @@ -93,20 +95,15 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
// Gets called when a new image is added to the buffer
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
i = i + 1;
latestBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
print("Failed to get image buffer from sample buffer.")
return
}
latestBuffer = imageBuffer
registry.textureFrameAvailable(textureId)
// switch analyzeMode {
// case 1: // barcode
// Limit the analyzer because the texture output will freeze otherwise
if i / 10 == 1 {
if ((detectionSpeed == DetectionSpeed.normal || detectionSpeed == DetectionSpeed.noDuplicates) && i > 10 || detectionSpeed == DetectionSpeed.unrestricted) {
i = 0
} else {
return
}
let imageRequestHandler = VNImageRequestHandler(
cvPixelBuffer: latestBuffer,
orientation: .right)
... ... @@ -116,8 +113,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
if error == nil {
if let results = request.results as? [VNBarcodeObservation] {
for barcode in results {
if scanWindow != nil {
let match = isbarCodeInScanWindow(scanWindow!, barcode, buffer!.image)
if self.scanWindow != nil {
let match = self.isbarCodeInScanWindow(self.scanWindow!, barcode, self.latestBuffer)
if (!match) {
continue
}
... ... @@ -141,10 +138,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
} catch {
print(error)
}
// default: // none
// break
// }
} else {
i+=1
}
}
func checkPermission(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
... ... @@ -188,11 +184,11 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
scanWindow = CGRect(x: minX, y: minY, width: width, height: height)
}
func isbarCodeInScanWindow(_ scanWindow: CGRect, _ barcode: Barcode, _ inputImage: UIImage) -> Bool {
let barcodeBoundingBox = barcode.frame
func isbarCodeInScanWindow(_ scanWindow: CGRect, _ barcode: VNBarcodeObservation, _ inputImage: CVImageBuffer) -> Bool {
let size = CVImageBufferGetEncodedSize(inputImage)
let imageWidth = inputImage.size.width;
let imageHeight = inputImage.size.height;
let imageWidth = size.width;
let imageHeight = size.height;
let minX = scanWindow.minX * imageWidth
let minY = scanWindow.minY * imageHeight
... ... @@ -200,7 +196,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
let height = scanWindow.height * imageHeight
let scaledScanWindow = CGRect(x: minX, y: minY, width: width, height: height)
return scaledScanWindow.contains(barcodeBoundingBox)
return scaledScanWindow.contains(barcode.boundingBox)
}
func start(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
... ... @@ -219,6 +215,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
// let ratio: Int = argReader.int(key: "ratio")
let torch: Bool = argReader.bool(key: "torch") ?? false
let facing: Int = argReader.int(key: "facing") ?? 1
let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0
detectionSpeed = DetectionSpeed(rawValue: speed)!
// Set the camera to use
position = facing == 0 ? AVCaptureDevice.Position.front : .back
... ...