Explorar el Código

Merge pull request #236 from juliuscanute/60-camera-permission

Fixed permission callback.
flutter-beta
Julian Steenbakker hace 5 años
committed by GitHub
padre
commit
19a8615bd3
No se encontró ninguna clave conocida en la base de datos para esta firma ID de clave GPG: 4AEE18F83AFDEB23
Se han modificado 3 ficheros con 162 adiciones y 94 borrados
  1. +21
    -12
      android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt
  2. +122
    -72
      ios/Classes/QRView.swift
  3. +19
    -10
      lib/src/qr_code_scanner.dart

+ 21
- 12
android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt Ver fichero

@@ -89,7 +89,10 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun flipCamera(result: MethodChannel.Result) { private fun flipCamera(result: MethodChannel.Result) {
if (barcodeView == null) { if (barcodeView == null) {
return barCodeViewNotSet(result) return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) {
return cameraPermissionNotSet(result)
} }

barcodeView!!.pause() barcodeView!!.pause()
val settings = barcodeView!!.cameraSettings val settings = barcodeView!!.cameraSettings


@@ -113,6 +116,8 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun toggleFlash(result: MethodChannel.Result) { private fun toggleFlash(result: MethodChannel.Result) {
if (barcodeView == null) { if (barcodeView == null) {
return barCodeViewNotSet(result) return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) {
return cameraPermissionNotSet(result)
} }


if (hasFlash()) { if (hasFlash()) {
@@ -128,7 +133,10 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun pauseCamera(result: MethodChannel.Result) { private fun pauseCamera(result: MethodChannel.Result) {
if (barcodeView == null) { if (barcodeView == null) {
return barCodeViewNotSet(result) return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) {
return cameraPermissionNotSet(result)
} }

if (barcodeView!!.isPreviewActive) { if (barcodeView!!.isPreviewActive) {
barcodeView!!.pause() barcodeView!!.pause()
} }
@@ -138,7 +146,10 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun resumeCamera(result: MethodChannel.Result) { private fun resumeCamera(result: MethodChannel.Result) {
if (barcodeView == null) { if (barcodeView == null) {
return barCodeViewNotSet(result) return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) {
return cameraPermissionNotSet(result)
} }

if (!barcodeView!!.isPreviewActive) { if (!barcodeView!!.isPreviewActive) {
barcodeView!!.resume() barcodeView!!.resume()
} }
@@ -166,19 +177,12 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
result.error("404", "No barcode view found", null) result.error("404", "No barcode view found", null)
} }


private fun cameraPermissionNotSet(result: MethodChannel.Result) {
result.error("cameraPermission", " Permission denied to access the camera", null)
}

override fun getView(): View { override fun getView(): View {
return initBarCodeView()?.apply {
if (!hasBackCamera()) {
if (!hasFrontCamera()) {
// No camera available!
} else {
this.cameraSettings.requestedCameraId = CameraInfo.CAMERA_FACING_FRONT
}
} else {
this.cameraSettings.requestedCameraId = CameraInfo.CAMERA_FACING_BACK
}
resume()
}!!
return initBarCodeView().apply {}!!
} }


private fun initBarCodeView(): BarcodeView? { private fun initBarCodeView(): BarcodeView? {
@@ -187,11 +191,16 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
if (params["cameraFacing"] as Int == 1) { if (params["cameraFacing"] as Int == 1) {
barcodeView?.cameraSettings?.requestedCameraId = CameraInfo.CAMERA_FACING_FRONT barcodeView?.cameraSettings?.requestedCameraId = CameraInfo.CAMERA_FACING_FRONT
} }
barcodeView!!.resume()
} }
return barcodeView return barcodeView
} }


private fun startScan(arguments: List<Int>?, result: MethodChannel.Result) { private fun startScan(arguments: List<Int>?, result: MethodChannel.Result) {
if (!hasCameraPermission()) {
return cameraPermissionNotSet(result)
}

val allowedBarcodeTypes = mutableListOf<BarcodeFormat>() val allowedBarcodeTypes = mutableListOf<BarcodeFormat>()
try { try {
arguments?.forEach { arguments?.forEach {


+ 122
- 72
ios/Classes/QRView.swift Ver fichero

@@ -46,7 +46,7 @@ public class QRView:NSObject,FlutterPlatformView {
switch(call.method){ switch(call.method){
case "setDimensions": case "setDimensions":
let arguments = call.arguments as! Dictionary<String, Double> let arguments = call.arguments as! Dictionary<String, Double>
self?.setDimensions(width: arguments["width"] ?? 0, height: arguments["height"] ?? 0, scanArea: arguments["scanArea"] ?? 0)
self?.setDimensions(result, width: arguments["width"] ?? 0, height: arguments["height"] ?? 0, scanArea: arguments["scanArea"] ?? 0)
case "startScan": case "startScan":
self?.startScan(call.arguments as! Array<Int>, result) self?.startScan(call.arguments as! Array<Int>, result)
case "flipCamera": case "flipCamera":
@@ -71,32 +71,40 @@ public class QRView:NSObject,FlutterPlatformView {
return previewView return previewView
} }
func setDimensions(width: Double, height: Double, scanArea: Double) -> Void {
// First set the size of the preview area.
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 = scanner {
if let previewLayer = sc.previewLayer {
previewLayer.frame = previewView.bounds;
}
if (scanArea != 0) {
sc.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea)
}
} else {
// Create a scanner view if it doesn't exist yet.
scanner = MTBBarcodeScanner(previewView: previewView)
if (scanArea != 0) {
scanner?.didStartScanningBlock = {
self.scanner?.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea)
func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double) -> Void {
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)
}
} 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)
}
}
} }
return result(width)
} else {
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil))
} }
}
})
} }
func startScan(_ arguments: Array<Int>, _ result: @escaping FlutterResult) -> Void { func startScan(_ arguments: Array<Int>, _ result: @escaping FlutterResult) -> Void {
@@ -104,14 +112,14 @@ public class QRView:NSObject,FlutterPlatformView {
scanner = MTBBarcodeScanner(previewView: previewView) scanner = MTBBarcodeScanner(previewView: previewView)
} }
var allowedBarcodeTypes: Array<AVMetadataObject.ObjectType> = [] var allowedBarcodeTypes: Array<AVMetadataObject.ObjectType> = []
arguments.forEach { arg in arguments.forEach { arg in
allowedBarcodeTypes.append( QRCodeTypes[arg]!) allowedBarcodeTypes.append( QRCodeTypes[arg]!)
} }
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in
if permissionGranted { if permissionGranted {
self.channel.invokeMethod("onPermissionSet", arguments: true)
do { do {
try self.scanner?.startScanning(with: self.cameraFacing, resultBlock: { [weak self] codes in try self.scanner?.startScanning(with: self.cameraFacing, resultBlock: { [weak self] codes in
if let codes = codes { if let codes = codes {
@@ -154,80 +162,122 @@ public class QRView:NSObject,FlutterPlatformView {
}) })
} catch { } catch {
let error = FlutterError(code: "unknown-error", message: "Unable to start scanning", details: nil) let error = FlutterError(code: "unknown-error", message: "Unable to start scanning", details: nil)
result(error)
return result(error)
} }
} else { } else {
let error = FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil)
result(error)
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil))
} }
}) })
} }
func stopScan(){
if let sc: MTBBarcodeScanner = scanner {
if sc.isScanning() {
sc.stopScanning()
func stopScan(_ 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 getCameraInfo(_ result: @escaping FlutterResult) -> Void { func getCameraInfo(_ result: @escaping FlutterResult) -> Void {
if let sc: MTBBarcodeScanner = scanner {
result(sc.camera.rawValue)
} else {
let error = FlutterError(code: "cameraInformationError", message: "Could not get camera information", details: nil)
result(error)
}
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in
if permissionGranted {
if let sc: MTBBarcodeScanner = self.scanner {
result(sc.camera.rawValue)
} else {
let error = FlutterError(code: "cameraInformationError", message: "Could not get camera information", details: nil)
result(error)
}
} else {
return result(FlutterError(code: "cameraPermission", message: "Permission denied to access the camera", details: nil))
}
})
} }
func flipCamera(_ result: @escaping FlutterResult){ func flipCamera(_ result: @escaping FlutterResult){
if let sc: MTBBarcodeScanner = scanner {
if sc.hasOppositeCamera() {
sc.flipCamera()
MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in
if permissionGranted {
if let sc: MTBBarcodeScanner = self.scanner {
if sc.hasOppositeCamera() {
sc.flipCamera()
}
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))
} }
return result(sc.camera.rawValue)
}
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil))
})
} }
func getFlashInfo(_ result: @escaping FlutterResult) -> Void { func getFlashInfo(_ result: @escaping FlutterResult) -> Void {
if let sc: MTBBarcodeScanner = scanner {
result(sc.torchMode.rawValue != 0)
} else {
let error = FlutterError(code: "cameraInformationError", message: "Could not get flash information", details: nil)
result(error)
}
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 toggleFlash(_ result: @escaping FlutterResult){ func toggleFlash(_ result: @escaping FlutterResult){
if let sc: MTBBarcodeScanner = scanner {
if sc.hasTorch() {
sc.toggleTorch()
return result(sc.torchMode == MTBTorchMode(rawValue: 1))
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))
} }
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) { func pauseCamera(_ result: @escaping FlutterResult) {
if let sc: MTBBarcodeScanner = scanner {
if sc.isScanning() {
sc.freezeCapture()
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))
} }
return result(true)
}
return result(FlutterError(code: "404", message: "No barcode scanner found", details: nil))
})
} }
func resumeCamera(_ result: @escaping FlutterResult) { func resumeCamera(_ result: @escaping FlutterResult) {
if let sc: MTBBarcodeScanner = scanner {
if !sc.isScanning() {
sc.unfreezeCapture()
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))
} }
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) -> Void {


+ 19
- 10
lib/src/qr_code_scanner.dart Ver fichero

@@ -211,9 +211,14 @@ class QRViewController {
Future<void> _startScan(GlobalKey key, double cutOutSize, Future<void> _startScan(GlobalKey key, double cutOutSize,
List<BarcodeFormat> barcodeFormats) async { List<BarcodeFormat> barcodeFormats) async {
// We need to update the dimension before the scan is started. // We need to update the dimension before the scan is started.
QRViewController.updateDimensions(key, _channel, scanArea: cutOutSize);
return _channel.invokeMethod(
'startScan', barcodeFormats?.map((e) => e.asInt())?.toList() ?? []);
try {
await QRViewController.updateDimensions(key, _channel,
scanArea: cutOutSize);
return await _channel.invokeMethod(
'startScan', barcodeFormats?.map((e) => e.asInt())?.toList() ?? []);
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
} }


/// Gets information about which camera is active. /// Gets information about which camera is active.
@@ -289,15 +294,19 @@ class QRViewController {
} }


/// Updates the view dimensions for iOS. /// Updates the view dimensions for iOS.
static void updateDimensions(GlobalKey key, MethodChannel channel,
{double scanArea}) {
static Future<void> updateDimensions(GlobalKey key, MethodChannel channel,
{double scanArea}) async {
if (defaultTargetPlatform == TargetPlatform.iOS) { if (defaultTargetPlatform == TargetPlatform.iOS) {
final RenderBox renderBox = key.currentContext.findRenderObject(); final RenderBox renderBox = key.currentContext.findRenderObject();
channel.invokeMethod('setDimensions', {
'width': renderBox.size.width,
'height': renderBox.size.height,
'scanArea': scanArea ?? 0
});
try {
await channel.invokeMethod('setDimensions', {
'width': renderBox.size.width,
'height': renderBox.size.height,
'scanArea': scanArea ?? 0
});
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
} }
} }
} }

Cargando…
Cancelar
Guardar