瀏覽代碼

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

Fixed permission callback.
flutter-beta
Julian Steenbakker 5 年之前
committed by GitHub
父節點
當前提交
19a8615bd3
沒有發現已知的金鑰在資料庫的簽署中 GPG Key ID: 4AEE18F83AFDEB23
共有 3 個文件被更改,包括 162 次插入94 次删除
  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 查看文件

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

barcodeView!!.pause()
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) {
if (barcodeView == null) {
return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) {
return cameraPermissionNotSet(result)
}

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

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

if (!barcodeView!!.isPreviewActive) {
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)
}

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

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

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

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


+ 122
- 72
ios/Classes/QRView.swift 查看文件

@@ -46,7 +46,7 @@ public class QRView:NSObject,FlutterPlatformView {
switch(call.method){
case "setDimensions":
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":
self?.startScan(call.arguments as! Array<Int>, result)
case "flipCamera":
@@ -71,32 +71,40 @@ public class QRView:NSObject,FlutterPlatformView {
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 {
@@ -104,14 +112,14 @@ public class QRView:NSObject,FlutterPlatformView {
scanner = MTBBarcodeScanner(previewView: previewView)
}
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 {
@@ -154,80 +162,122 @@ public class QRView:NSObject,FlutterPlatformView {
})
} catch {
let error = FlutterError(code: "unknown-error", message: "Unable to start scanning", details: nil)
result(error)
return result(error)
}
} 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 {
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){
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 {
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){
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) {
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) {
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 {


+ 19
- 10
lib/src/qr_code_scanner.dart 查看文件

@@ -211,9 +211,14 @@ class QRViewController {
Future<void> _startScan(GlobalKey key, double cutOutSize,
List<BarcodeFormat> barcodeFormats) async {
// 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.
@@ -289,15 +294,19 @@ class QRViewController {
}

/// 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) {
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);
}
}
}
}

Loading…
取消
儲存