Просмотр исходного кода

Merge pull request #191 from juliuscanute/165-scan-area-ios

165 scan area ios
flutter-beta
Julian Steenbakker 5 лет назад
committed by GitHub
Родитель
Сommit
472f247263
Не найден GPG ключ соответствующий данной подписи Идентификатор GPG ключа: 4AEE18F83AFDEB23
6 измененных файлов: 74 добавлений и 28 удалений
  1. +7
    -3
      README.md
  2. +18
    -6
      example/README.md
  3. +3
    -3
      example/ios/Runner.xcodeproj/project.pbxproj
  4. +17
    -3
      example/lib/main.dart
  5. +12
    -4
      ios/Classes/QRView.swift
  6. +17
    -9
      lib/src/qr_code_scanner.dart

+ 7
- 3
README.md Просмотреть файл

@@ -64,12 +64,16 @@ class _QRViewExampleState extends State<QRViewExample> {
Barcode result;
QRViewController controller;

/// Overriding reassemble and pausing the camera
/// ensures us that hot reload won't give a black screen
// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
@override
void reassemble() {
super.reassemble();
controller.pauseCamera();
if (Platform.isAndroid) {
controller.pauseCamera();
} else if (Platform.isIOS) {
controller.resumeCamera();
}
}

@override


+ 18
- 6
example/README.md Просмотреть файл

@@ -47,12 +47,16 @@ class _QRViewExampleState extends State<QRViewExample> {
QRViewController controller;
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

/// Overriding reassemble and pausing the camera
/// ensures us that hot reload won't give a black screen
// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
@override
void reassemble() {
super.reassemble();
controller.pauseCamera();
if (Platform.isAndroid) {
controller.pauseCamera();
} else if (Platform.isIOS) {
controller.resumeCamera();
}
}

@override
@@ -69,7 +73,8 @@ class _QRViewExampleState extends State<QRViewExample> {
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
if (result != null)
Text('Barcode Type: ${describeEnum(result.format)} Data: ${result.code}')
Text(
'Barcode Type: ${describeEnum(result.format)} Data: ${result.code}')
else
Text('Scan a code'),
Row(
@@ -162,11 +167,17 @@ class _QRViewExampleState extends State<QRViewExample> {
}

Widget _buildQrView(BuildContext context) {
// For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
var scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 150.0
: 300.0;
// To ensure the Scanner view is properly sizes after rotation
// we need to listen for Flutter SizeChanged notification and update controller
return NotificationListener<SizeChangedLayoutNotification>(
onNotification: (notification) {
Future.microtask(() => controller?.updateDimensions(qrKey));
Future.microtask(
() => controller?.updateDimensions(qrKey, scanArea: scanArea));
return false;
},
child: SizeChangedLayoutNotifier(
@@ -179,7 +190,7 @@ class _QRViewExampleState extends State<QRViewExample> {
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: 300,
cutOutSize: scanArea,
),
)));
}
@@ -201,6 +212,7 @@ class _QRViewExampleState extends State<QRViewExample> {
}



```




+ 3
- 3
example/ios/Runner.xcodeproj/project.pbxproj Просмотреть файл

@@ -384,7 +384,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.capture;
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.qr;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.0;
@@ -522,7 +522,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.capture;
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.qr;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
@@ -553,7 +553,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.capture;
PRODUCT_BUNDLE_IDENTIFIER = dev.steenbakker.qr;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";


+ 17
- 3
example/lib/main.dart Просмотреть файл

@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
@@ -25,10 +27,16 @@ class _QRViewExampleState extends State<QRViewExample> {
QRViewController controller;
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
@override
void reassemble() {
super.reassemble();
controller.pauseCamera();
if (Platform.isAndroid) {
controller.pauseCamera();
} else if (Platform.isIOS) {
controller.resumeCamera();
}
}

@override
@@ -139,11 +147,17 @@ class _QRViewExampleState extends State<QRViewExample> {
}

Widget _buildQrView(BuildContext context) {
// For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
var scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 150.0
: 300.0;
// To ensure the Scanner view is properly sizes after rotation
// we need to listen for Flutter SizeChanged notification and update controller
return NotificationListener<SizeChangedLayoutNotification>(
onNotification: (notification) {
Future.microtask(() => controller?.updateDimensions(qrKey));
Future.microtask(
() => controller?.updateDimensions(qrKey, scanArea: scanArea));
return false;
},
child: SizeChangedLayoutNotifier(
@@ -156,7 +170,7 @@ class _QRViewExampleState extends State<QRViewExample> {
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: 300,
cutOutSize: scanArea,
),
)));
}


+ 12
- 4
ios/Classes/QRView.swift Просмотреть файл

@@ -53,7 +53,6 @@ public class QRView:NSObject,FlutterPlatformView {
default:
return
}

guard let stringValue = code.stringValue else { continue }
let result = ["code": stringValue, "type": typeString]
self?.channel.invokeMethod("onRecognizeQR", arguments: result)
@@ -74,7 +73,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)
self?.setDimensions(width: arguments["width"] ?? 0, height: arguments["height"] ?? 0, scanArea: arguments["scanArea"] ?? 0)
case "flipCamera":
self?.flipCamera()
case "toggleFlash":
@@ -91,15 +90,24 @@ public class QRView:NSObject,FlutterPlatformView {
return previewView
}
func setDimensions(width: Double, height: Double) -> Void {
func setDimensions(width: Double, height: Double, scanArea: Double) -> Void {
previewView.frame = CGRect(x: 0, y: 0, width: width, height: height)

let midX = self.view().bounds.midX
let midY = self.view().bounds.midY
if let sc: MTBBarcodeScanner = scanner {
if let previewLayer = sc.previewLayer {
previewLayer.frame = previewView.bounds;
}
} else {
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)
}
}


MTBBarcodeScanner.requestCameraPermission(success: isCameraAvailable)
}
}


+ 17
- 9
lib/src/qr_code_scanner.dart Просмотреть файл

@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

typedef QRViewCreatedCallback = void Function(QRViewController);

@@ -110,7 +111,7 @@ class _QRViewState extends State<QRView> {
Widget build(BuildContext context) {
return Stack(
children: [
_getPlatformQrView(),
_getPlatformQrView(widget.key),
if (widget.overlay != null)
Container(
padding: widget.overlayMargin,
@@ -124,7 +125,7 @@ class _QRViewState extends State<QRView> {
);
}

Widget _getPlatformQrView() {
Widget _getPlatformQrView(GlobalKey key) {
Widget _platformQrView;
switch (defaultTargetPlatform) {
case TargetPlatform.android:
@@ -137,7 +138,9 @@ class _QRViewState extends State<QRView> {
_platformQrView = UiKitView(
viewType: 'net.touchcapture.qr.flutterqr/qrview',
onPlatformViewCreated: _onPlatformViewCreated,
creationParams: _CreationParams.fromWidget(0, 0).toMap(),
creationParams:
_CreationParams.fromWidget(MediaQuery.of(context).size.width, 400)
.toMap(),
creationParamsCodec: StandardMessageCodec(),
);
break;
@@ -152,7 +155,9 @@ class _QRViewState extends State<QRView> {
if (widget.onQRViewCreated == null) {
return;
}
widget.onQRViewCreated(QRViewController._(id, widget.key));

widget.onQRViewCreated(QRViewController._(
id, widget.key, (widget.overlay as QrScannerOverlayShape).cutOutSize));
}
}

@@ -178,9 +183,9 @@ class _CreationParams {
}

class QRViewController {
QRViewController._(int id, GlobalKey qrKey)
QRViewController._(int id, GlobalKey qrKey, double scanArea)
: _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') {
updateDimensions(qrKey);
updateDimensions(qrKey, scanArea: scanArea);
_channel.setMethodCallHandler(
(call) async {
switch (call.method) {
@@ -231,11 +236,14 @@ class QRViewController {
_scanUpdateController.close();
}

void updateDimensions(GlobalKey key) {
void updateDimensions(GlobalKey key, {double scanArea}) {
if (defaultTargetPlatform == TargetPlatform.iOS) {
final RenderBox renderBox = key.currentContext.findRenderObject();
_channel.invokeMethod('setDimensions',
{'width': renderBox.size.width, 'height': renderBox.size.height});
_channel.invokeMethod('setDimensions', {
'width': renderBox.size.width,
'height': renderBox.size.height,
'scanArea': scanArea ?? 0
});
}
}
}

Загрузка…
Отмена
Сохранить