Navaron Bracke
Committed by GitHub

Merge pull request #811 from navaronbracke/ios_macos_cleanup

feat: Cleanup iOS & MacOS setup
... ... @@ -367,7 +367,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.mobileScannerExample;
PRODUCT_BUNDLE_IDENTIFIER = "com.example.mobile-scanner";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
... ... @@ -499,7 +499,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.mobileScannerExample;
PRODUCT_BUNDLE_IDENTIFIER = "com.example.mobile-scanner";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
... ... @@ -525,7 +525,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.mobileScannerExample;
PRODUCT_BUNDLE_IDENTIFIER = "com.example.mobile-scanner";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
... ...
... ... @@ -27,9 +27,9 @@
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSCameraUsageDescription</key>
<string>We use the camera to scan barcodes</string>
<string>This app needs camera access to scan QR codes</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>We need access in order to open photos of barcodes</string>
<string>This app needs photos access to get QR code from photo library</string>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UILaunchStoryboardName</key>
... ... @@ -51,10 +51,5 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSCameraUsageDescription</key>
<string>This app needs camera access to scan QR codes</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs photos access to get QR code from photo library</string>
</dict>
</plist>
... ...
name: mobile_scanner_example
description: Demonstrates how to use the mobile_scanner plugin.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 0.0.1
environment:
sdk: ">=2.12.0 <4.0.0"
... ...
... ... @@ -5,6 +5,7 @@
// Created by Julian Steenbakker on 24/08/2022.
//
import Flutter
import Foundation
public class BarcodeHandler: NSObject, FlutterStreamHandler {
... ...
//
// SwiftMobileScanner.swift
// MobileScanner.swift
// mobile_scanner
//
// Created by Julian Steenbakker on 15/02/2022.
//
import Foundation
import Flutter
import AVFoundation
import MLKitVision
import MLKitBarcodeScanning
... ... @@ -191,7 +191,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
throw MobileScannerError.cameraError(error)
}
captureSession.sessionPreset = AVCaptureSession.Preset.photo;
captureSession.sessionPreset = AVCaptureSession.Preset.photo
// Add video output.
let videoOutput = AVCaptureVideoDataOutput()
... ... @@ -294,7 +294,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch keyPath {
case "torchMode":
// off = 0; on = 1; auto = 2;
// off = 0; on = 1; auto = 2
let state = change?[.newKey] as? Int
torchModeChangeCallback(state)
case "videoZoomFactor":
... ... @@ -348,7 +348,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
}
}
/// Analyze a single image
func analyzeImage(image: UIImage, position: AVCaptureDevice.Position, callback: @escaping BarcodeScanningCallback) {
let image = VisionImage(image: image)
... ... @@ -363,18 +362,16 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
var barcodesString: Array<String?>?
// /// Convert image buffer to jpeg
// private func ciImageToJpeg(ciImage: CIImage) -> Data {
//
// // let ciImage = CIImage(cvPixelBuffer: latestBuffer)
// let context:CIContext = CIContext.init(options: nil)
// let cgImage:CGImage = context.createCGImage(ciImage, from: ciImage.extent)!
// let uiImage:UIImage = UIImage(cgImage: cgImage, scale: 1, orientation: UIImage.Orientation.up)
//
// return uiImage.jpegData(compressionQuality: 0.8)!;
// }
// /// Convert image buffer to jpeg
// private func ciImageToJpeg(ciImage: CIImage) -> Data {
//
// // let ciImage = CIImage(cvPixelBuffer: latestBuffer)
// let context:CIContext = CIContext.init(options: nil)
// let cgImage:CGImage = context.createCGImage(ciImage, from: ciImage.extent)!
// let uiImage:UIImage = UIImage(cgImage: cgImage, scale: 1, orientation: UIImage.Orientation.up)
//
// return uiImage.jpegData(compressionQuality: 0.8)!
// }
/// Rotates images accordingly
func imageOrientation(
... ...
#import <Flutter/Flutter.h>
@interface MobileScannerPlugin : NSObject<FlutterPlugin>
@end
#import "MobileScannerPlugin.h"
#if __has_include(<mobile_scanner/mobile_scanner-Swift.h>)
#import <mobile_scanner/mobile_scanner-Swift.h>
#else
// Support project import fallback if the generated compatibility header
// is not copied when this plugin is created as a library.
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
#import "mobile_scanner-Swift.h"
#endif
@implementation MobileScannerPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[SwiftMobileScannerPlugin registerWithRegistrar:registrar];
}
@end
... ... @@ -4,7 +4,7 @@ import MLKitBarcodeScanning
import AVFoundation
import UIKit
public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
public class MobileScannerPlugin: NSObject, FlutterPlugin {
/// The mobile scanner object that handles all logic
private let mobileScanner: MobileScanner
... ... @@ -16,7 +16,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
static var scanWindow: [CGFloat]?
private static func isBarcodeInScanWindow(barcode: Barcode, imageSize: CGSize) -> Bool {
let scanwindow = SwiftMobileScannerPlugin.scanWindow!
let scanwindow = MobileScannerPlugin.scanWindow!
let barcodeminX = barcode.cornerPoints![0].cgPointValue.x
let barcodeminY = barcode.cornerPoints![1].cgPointValue.y
... ... @@ -24,7 +24,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
let barcodeheight = barcode.cornerPoints![3].cgPointValue.y - barcodeminY
let barcodeBox = CGRect(x: barcodeminX, y: barcodeminY, width: barcodewidth, height: barcodeheight)
let minX = scanwindow[0] * imageSize.width
let minY = scanwindow[1] * imageSize.height
... ... @@ -40,8 +39,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
self.mobileScanner = MobileScanner(registry: registry, mobileScannerCallback: { barcodes, error, image in
if barcodes != nil {
let barcodesMap: [Any?] = barcodes!.compactMap { barcode in
if (SwiftMobileScannerPlugin.scanWindow != nil) {
if (SwiftMobileScannerPlugin.isBarcodeInScanWindow(barcode: barcode, imageSize: image.size)) {
if (MobileScannerPlugin.scanWindow != nil) {
if (MobileScannerPlugin.isBarcodeInScanWindow(barcode: barcode, imageSize: image.size)) {
return barcode.data
} else {
return nil
... ... @@ -66,10 +65,9 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
}
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = SwiftMobileScannerPlugin(barcodeHandler: BarcodeHandler(registrar: registrar), registry: registrar.textures())
let methodChannel = FlutterMethodChannel(name:
"dev.steenbakker.mobile_scanner/scanner/method", binaryMessenger: registrar.messenger())
registrar.addMethodCallDelegate(instance, channel: methodChannel)
let channel = FlutterMethodChannel(name: "dev.steenbakker.mobile_scanner/scanner/method", binaryMessenger: registrar.messenger())
let instance = MobileScannerPlugin(barcodeHandler: BarcodeHandler(registrar: registrar), registry: registrar.textures())
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
... ... @@ -118,7 +116,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
barcodeOptions = BarcodeScannerOptions(formats: barcodeFormats)
}
let position = facing == 0 ? AVCaptureDevice.Position.front : .back
let detectionSpeed: DetectionSpeed = DetectionSpeed(rawValue: speed)!
... ... @@ -216,11 +213,10 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
result(nil)
}
/// Toggles the torch
func updateScanWindow(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
let scanWindowData: Array? = (call.arguments as? [String: Any])?["rect"] as? [CGFloat]
SwiftMobileScannerPlugin.scanWindow = scanWindowData
MobileScannerPlugin.scanWindow = scanWindowData
result(nil)
}
... ...
//
// Util.swift
// camerax
//
// Created by 闫守旺 on 2021/2/6.
//
import AVFoundation
import Flutter
import Foundation
... ...
... ... @@ -32,14 +32,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
var symbologies:[VNBarcodeSymbology] = []
// var analyzeMode: Int = 0
// var analyzeMode: Int = 0
var analyzing: Bool = false
var position = AVCaptureDevice.Position.back
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = MobileScannerPlugin(registrar.textures)
let method = FlutterMethodChannel(name:
"dev.steenbakker.mobile_scanner/scanner/method", binaryMessenger: registrar.messenger)
let event = FlutterEventChannel(name:
... ... @@ -53,7 +51,6 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
super.init()
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "state":
... ... @@ -64,8 +61,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
start(call, result)
case "torch":
toggleTorch(call, result)
// case "analyze":
// switchAnalyzeMode(call, result)
// case "analyze":
// switchAnalyzeMode(call, result)
case "stop":
stop(result)
case "updateScanWindow":
... ... @@ -139,11 +136,11 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
DispatchQueue.main.async {
self?.sink?(["name": "barcodeMac", "data" : ["payload": barcode.payloadStringValue, "symbology": barcode.symbology.toInt as Any?]] as [String : Any])
}
// if barcodeType == "QR" {
// let image = CIImage(image: source)
// image?.cropping(to: barcode.boundingBox)
// self.qrCodeDescriptor(qrCode: barcode, qrCodeImage: image!)
// }
// if barcodeType == "QR" {
// let image = CIImage(image: source)
// image?.cropping(to: barcode.boundingBox)
// self.qrCodeDescriptor(qrCode: barcode, qrCodeImage: image!)
// }
}
}
} else {
... ... @@ -204,9 +201,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
func isBarCodeInScanWindow(_ scanWindow: CGRect, _ barcode: VNBarcodeObservation, _ inputImage: CGImage) -> Bool {
let imageWidth = CGFloat(inputImage.width);
let imageHeight = CGFloat(inputImage.height);
let imageWidth = CGFloat(inputImage.width)
let imageHeight = CGFloat(inputImage.height)
let minX = scanWindow.minX * imageWidth
let minY = scanWindow.minY * imageHeight
... ... @@ -220,8 +216,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
func isBarCodeInScanWindow(_ scanWindow: CGRect, _ barcode: VNBarcodeObservation, _ inputImage: CVImageBuffer) -> Bool {
let size = CVImageBufferGetEncodedSize(inputImage)
let imageWidth = size.width;
let imageHeight = size.height;
let imageWidth = size.width
let imageHeight = size.height
let minX = scanWindow.minX * imageWidth
let minY = scanWindow.minY * imageHeight
... ... @@ -293,7 +289,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
} catch {
result(FlutterError(code: error.localizedDescription, message: nil, details: nil))
}
captureSession.sessionPreset = AVCaptureSession.Preset.photo;
captureSession.sessionPreset = AVCaptureSession.Preset.photo
// Add video output.
let videoOutput = AVCaptureVideoDataOutput()
... ... @@ -331,10 +327,10 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
}
// func switchAnalyzeMode(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
// analyzeMode = call.arguments as! Int
// result(nil)
// }
// func switchAnalyzeMode(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
// analyzeMode = call.arguments as! Int
// result(nil)
// }
func stop(_ result: FlutterResult) {
if (device == nil) {
... ... @@ -352,7 +348,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
device.removeObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode))
registry.unregisterTexture(textureId)
// analyzeMode = 0
// analyzeMode = 0
latestBuffer = nil
captureSession = nil
device = nil
... ... @@ -365,7 +361,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch keyPath {
case "torchMode":
// off = 0; on = 1; auto = 2;
// off = 0 on = 1 auto = 2
let state = change?[.newKey] as? Int
let event: [String: Any?] = ["name": "torchState", "data": state]
sink?(event)
... ... @@ -376,7 +372,6 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
class MapArgumentReader {
let args: [String: Any]?
init(_ args: [String: Any]?) {
... ... @@ -422,7 +417,6 @@ class MapArgumentReader {
}
extension VNBarcodeSymbology {
static func fromInt(_ mapValue:Int) -> VNBarcodeSymbology? {
if #available(macOS 12.0, *) {
if(mapValue == 8){
... ... @@ -487,7 +481,7 @@ extension VNBarcodeSymbology {
case VNBarcodeSymbology.aztec:
return 4096
default:
return -1;
return -1
}
}
}
... ...