|
|
|
@@ -77,222 +77,170 @@ public class QRView:NSObject,FlutterPlatformView { |
|
|
|
return previewView |
|
|
|
} |
|
|
|
|
|
|
|
func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double, scanAreaOffset: Double) -> Void { |
|
|
|
func requestPermissions(_ result: @escaping FlutterResult) { |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
// self?.channel.invokeMethod("onPermissionSet", arguments: true) |
|
|
|
// First set the size of the preview area. |
|
|
|
self.previewView.frame = CGRect(x: 0, y: 0, width: width, height: height) |
|
|
|
|
|
|
|
// Then set the size of the scan area. |
|
|
|
let midX = self.view().bounds.midX |
|
|
|
let midY = self.view().bounds.midY |
|
|
|
|
|
|
|
// Check if the scanner is already created. |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if let previewLayer = sc.previewLayer { |
|
|
|
previewLayer.frame = self.previewView.bounds; |
|
|
|
} |
|
|
|
if (scanArea != 0) { |
|
|
|
sc.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) |
|
|
|
|
|
|
|
// Set offset if provided. |
|
|
|
if (scanAreaOffset != 0) { |
|
|
|
sc.scanRect = sc.scanRect.offsetBy(dx: 0, dy: CGFloat(scanAreaOffset)) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
// Create a scanner view if it doesn't exist yet. |
|
|
|
self.scanner = MTBBarcodeScanner(previewView: self.previewView) |
|
|
|
|
|
|
|
if (scanArea != 0) { |
|
|
|
self.scanner?.didStartScanningBlock = { |
|
|
|
self.scanner?.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) |
|
|
|
|
|
|
|
// Set offset if provided. |
|
|
|
if (scanAreaOffset != 0) { |
|
|
|
self.scanner?.scanRect = (self.scanner?.scanRect.offsetBy(dx: 0, dy: CGFloat(scanAreaOffset)))! |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return result(width) |
|
|
|
self.channel.invokeMethod("onPermissionSet", arguments: true) |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
func startScan(_ arguments: Array<Int>, _ result: @escaping FlutterResult) -> Void { |
|
|
|
if (scanner == nil) { |
|
|
|
scanner = MTBBarcodeScanner(previewView: previewView) |
|
|
|
func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double, scanAreaOffset: Double) { |
|
|
|
// First ask for permission |
|
|
|
requestPermissions(result) |
|
|
|
|
|
|
|
// Then set the size of the preview area. |
|
|
|
self.previewView.frame = CGRect(x: 0, y: 0, width: width, height: height) |
|
|
|
|
|
|
|
// Then set the size of the scan area. |
|
|
|
let midX = self.view().bounds.midX |
|
|
|
let midY = self.view().bounds.midY |
|
|
|
|
|
|
|
// Check if the scanner already exists, else create one. |
|
|
|
if (self.scanner == nil) { |
|
|
|
self.scanner = MTBBarcodeScanner(previewView: self.previewView) |
|
|
|
} |
|
|
|
|
|
|
|
// Set the size of the preview. |
|
|
|
if let previewLayer = self.scanner?.previewLayer { |
|
|
|
previewLayer.frame = self.previewView.bounds; |
|
|
|
} |
|
|
|
|
|
|
|
// Set scanArea if provided. |
|
|
|
if (scanArea != 0) { |
|
|
|
self.scanner?.didStartScanningBlock = { |
|
|
|
self.scanner?.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) |
|
|
|
|
|
|
|
// Set offset if provided. |
|
|
|
if (scanAreaOffset != 0) { |
|
|
|
self.scanner?.scanRect = (self.scanner?.scanRect.offsetBy(dx: 0, dy: CGFloat(scanAreaOffset)))! |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return result(width) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func startScan(_ arguments: Array<Int>, _ result: @escaping FlutterResult) { |
|
|
|
// Check for allowed barcodes |
|
|
|
var allowedBarcodeTypes: Array<AVMetadataObject.ObjectType> = [] |
|
|
|
arguments.forEach { arg in |
|
|
|
allowedBarcodeTypes.append( QRCodeTypes[arg]!) |
|
|
|
} |
|
|
|
|
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
self.channel.invokeMethod("onPermissionSet", arguments: true) |
|
|
|
do { |
|
|
|
try self.scanner?.startScanning(with: self.cameraFacing, resultBlock: { [weak self] codes in |
|
|
|
if let codes = codes { |
|
|
|
for code in codes { |
|
|
|
var typeString: String; |
|
|
|
switch(code.type) { |
|
|
|
case AVMetadataObject.ObjectType.aztec: |
|
|
|
typeString = "AZTEC" |
|
|
|
case AVMetadataObject.ObjectType.code39: |
|
|
|
typeString = "CODE_39" |
|
|
|
case AVMetadataObject.ObjectType.code93: |
|
|
|
typeString = "CODE_93" |
|
|
|
case AVMetadataObject.ObjectType.code128: |
|
|
|
typeString = "CODE_128" |
|
|
|
case AVMetadataObject.ObjectType.dataMatrix: |
|
|
|
typeString = "DATA_MATRIX" |
|
|
|
case AVMetadataObject.ObjectType.ean8: |
|
|
|
typeString = "EAN_8" |
|
|
|
case AVMetadataObject.ObjectType.ean13: |
|
|
|
typeString = "EAN_13" |
|
|
|
case AVMetadataObject.ObjectType.itf14: |
|
|
|
typeString = "ITF" |
|
|
|
case AVMetadataObject.ObjectType.pdf417: |
|
|
|
typeString = "PDF_417" |
|
|
|
case AVMetadataObject.ObjectType.qr: |
|
|
|
typeString = "QR_CODE" |
|
|
|
case AVMetadataObject.ObjectType.upce: |
|
|
|
typeString = "UPC_E" |
|
|
|
default: |
|
|
|
return |
|
|
|
} |
|
|
|
guard let stringValue = code.stringValue else { continue } |
|
|
|
let result = ["code": stringValue, "type": typeString] |
|
|
|
if allowedBarcodeTypes.count == 0 || allowedBarcodeTypes.contains(code.type) { |
|
|
|
self?.channel.invokeMethod("onRecognizeQR", arguments: result) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
do { |
|
|
|
try self.scanner?.startScanning(with: self.cameraFacing, resultBlock: { [weak self] codes in |
|
|
|
if let codes = codes { |
|
|
|
for code in codes { |
|
|
|
var typeString: String; |
|
|
|
switch(code.type) { |
|
|
|
case AVMetadataObject.ObjectType.aztec: |
|
|
|
typeString = "AZTEC" |
|
|
|
case AVMetadataObject.ObjectType.code39: |
|
|
|
typeString = "CODE_39" |
|
|
|
case AVMetadataObject.ObjectType.code93: |
|
|
|
typeString = "CODE_93" |
|
|
|
case AVMetadataObject.ObjectType.code128: |
|
|
|
typeString = "CODE_128" |
|
|
|
case AVMetadataObject.ObjectType.dataMatrix: |
|
|
|
typeString = "DATA_MATRIX" |
|
|
|
case AVMetadataObject.ObjectType.ean8: |
|
|
|
typeString = "EAN_8" |
|
|
|
case AVMetadataObject.ObjectType.ean13: |
|
|
|
typeString = "EAN_13" |
|
|
|
case AVMetadataObject.ObjectType.itf14: |
|
|
|
typeString = "ITF" |
|
|
|
case AVMetadataObject.ObjectType.pdf417: |
|
|
|
typeString = "PDF_417" |
|
|
|
case AVMetadataObject.ObjectType.qr: |
|
|
|
typeString = "QR_CODE" |
|
|
|
case AVMetadataObject.ObjectType.upce: |
|
|
|
typeString = "UPC_E" |
|
|
|
default: |
|
|
|
return |
|
|
|
} |
|
|
|
guard let stringValue = code.stringValue else { continue } |
|
|
|
let result = ["code": stringValue, "type": typeString] |
|
|
|
if allowedBarcodeTypes.count == 0 || allowedBarcodeTypes.contains(code.type) { |
|
|
|
self?.channel.invokeMethod("onRecognizeQR", arguments: result) |
|
|
|
} |
|
|
|
}) |
|
|
|
} catch { |
|
|
|
let error = FlutterError(code: "unknown-error", message: "Unable to start scanning", details: nil) |
|
|
|
return result(error) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} catch { |
|
|
|
let error = FlutterError(code: "unknown-error", message: "Unable to start scanning", details: nil) |
|
|
|
return result(error) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func stopCamera(_ result: @escaping FlutterResult){ |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.isScanning() { |
|
|
|
sc.stopScanning() |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
func stopCamera(_ result: @escaping FlutterResult) { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.isScanning() { |
|
|
|
sc.stopScanning() |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func getCameraInfo(_ result: @escaping FlutterResult) -> Void { |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
result(self.cameraFacing.rawValue) |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
} |
|
|
|
}) |
|
|
|
func getCameraInfo(_ result: @escaping FlutterResult) { |
|
|
|
result(self.cameraFacing.rawValue) |
|
|
|
} |
|
|
|
|
|
|
|
func flipCamera(_ result: @escaping FlutterResult){ |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.hasOppositeCamera() { |
|
|
|
sc.flipCamera() |
|
|
|
self.cameraFacing = sc.camera |
|
|
|
} |
|
|
|
return result(sc.camera.rawValue) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
func flipCamera(_ result: @escaping FlutterResult) { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.hasOppositeCamera() { |
|
|
|
sc.flipCamera() |
|
|
|
self.cameraFacing = sc.camera |
|
|
|
} |
|
|
|
}) |
|
|
|
return result(sc.camera.rawValue) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} |
|
|
|
|
|
|
|
func getFlashInfo(_ result: @escaping FlutterResult) -> Void { |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
result(sc.torchMode.rawValue != 0) |
|
|
|
} else { |
|
|
|
let error = FlutterError(code: "cameraInformationError", message: "Could not get flash information", details: nil) |
|
|
|
result(error) |
|
|
|
} |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
} |
|
|
|
}) |
|
|
|
func getFlashInfo(_ result: @escaping FlutterResult) { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
result(sc.torchMode.rawValue != 0) |
|
|
|
} else { |
|
|
|
let error = FlutterError(code: "cameraInformationError", message: "Could not get flash information", details: nil) |
|
|
|
result(error) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func toggleFlash(_ result: @escaping FlutterResult){ |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.hasTorch() { |
|
|
|
sc.toggleTorch() |
|
|
|
return result(sc.torchMode == MTBTorchMode(rawValue: 1)) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "This device doesn\'t support flash", details: nil)) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.hasTorch() { |
|
|
|
sc.toggleTorch() |
|
|
|
return result(sc.torchMode == MTBTorchMode(rawValue: 1)) |
|
|
|
} |
|
|
|
}) |
|
|
|
return result(FlutterError(code: "404", message: "This device doesn\'t support flash", details: nil)) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} |
|
|
|
|
|
|
|
func pauseCamera(_ result: @escaping FlutterResult) { |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.isScanning() { |
|
|
|
sc.freezeCapture() |
|
|
|
} |
|
|
|
return result(true) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if sc.isScanning() { |
|
|
|
sc.freezeCapture() |
|
|
|
} |
|
|
|
}) |
|
|
|
return result(true) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} |
|
|
|
|
|
|
|
func resumeCamera(_ result: @escaping FlutterResult) { |
|
|
|
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in |
|
|
|
if permissionGranted { |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if !sc.isScanning() { |
|
|
|
sc.unfreezeCapture() |
|
|
|
} |
|
|
|
return result(true) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} else { |
|
|
|
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)) |
|
|
|
if let sc: MTBBarcodeScanner = self.scanner { |
|
|
|
if !sc.isScanning() { |
|
|
|
sc.unfreezeCapture() |
|
|
|
} |
|
|
|
}) |
|
|
|
return result(true) |
|
|
|
} |
|
|
|
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil)) |
|
|
|
} |
|
|
|
|
|
|
|
func getSystemFeatures(_ result: @escaping FlutterResult) -> Void { |
|
|
|
func getSystemFeatures(_ result: @escaping FlutterResult) { |
|
|
|
if let sc: MTBBarcodeScanner = scanner { |
|
|
|
var hasBackCameraVar = false |
|
|
|
var hasFrontCameraVar = false |
|
|
|
|