From a9d6ec8b51189687cd76454ddca7b7c05e5b3a7b Mon Sep 17 00:00:00 2001 From: Timo von Holtz Date: Wed, 24 Jun 2020 16:25:38 +0200 Subject: [PATCH] Add BarcodeFormat to result --- .../net/touchcapture/qr/flutterqr/QRView.kt | 3 +- example/lib/main.dart | 14 ++- ios/Classes/QRView.swift | 31 +++++- lib/src/qr_code_scanner.dart | 97 ++++++++++++++++++- 4 files changed, 136 insertions(+), 9 deletions(-) diff --git a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt index d99ddfb..d3b9145 100644 --- a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt +++ b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt @@ -124,7 +124,8 @@ class QRView(private val registrar: PluginRegistry.Registrar, id: Int) : barcode.decodeContinuous( object : BarcodeCallback { override fun barcodeResult(result: BarcodeResult) { - channel.invokeMethod("onRecognizeQR", result.text) + val code = mapOf("code" to result.text, "type" to result.barcodeFormat.name) + channel.invokeMethod("onRecognizeQR", code) } override fun possibleResultPoints(resultPoints: List) {} diff --git a/example/lib/main.dart b/example/lib/main.dart index 25855f9..052d32e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; @@ -18,7 +19,7 @@ class QRViewExample extends StatefulWidget { } class _QRViewExampleState extends State { - var qrText = ''; + Barcode result; var flashState = flashOn; var cameraState = frontCamera; QRViewController controller; @@ -26,6 +27,13 @@ class _QRViewExampleState extends State { @override Widget build(BuildContext context) { + String resultText; + if (result == null) { + resultText = 'No previous result'; + } else { + resultText = + 'This is the result of scan (${describeEnum(result.format)}): ${result.code}'; + } return Scaffold( body: Column( children: [ @@ -50,7 +58,7 @@ class _QRViewExampleState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - Text('This is the result of scan: $qrText'), + Text(resultText), Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, @@ -144,7 +152,7 @@ class _QRViewExampleState extends State { this.controller = controller; controller.scannedDataStream.listen((scanData) { setState(() { - qrText = scanData; + result = scanData; }); }); } diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index 68f7381..45fb3d1 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -26,8 +26,37 @@ public class QRView:NSObject,FlutterPlatformView { try scanner?.startScanning(resultBlock: { 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 } - self.channel.invokeMethod("onRecognizeQR", arguments: stringValue) + let result = ["code": stringValue, "type": typeString] + self.channel.invokeMethod("onRecognizeQR", arguments: result) } } }) diff --git a/lib/src/qr_code_scanner.dart b/lib/src/qr_code_scanner.dart index fad8126..852d7f6 100644 --- a/lib/src/qr_code_scanner.dart +++ b/lib/src/qr_code_scanner.dart @@ -6,6 +6,86 @@ import 'package:flutter/services.dart'; typedef QRViewCreatedCallback = void Function(QRViewController); +enum BarcodeFormat { + /// Aztec 2D barcode format. + aztec, + + /// CODABAR 1D format. + codabar, + + /// Code 39 1D format. + code39, + + /// Code 93 1D format. + code93, + + /// Code 128 1D format. + code128, + + /// Data Matrix 2D barcode format. + dataMatrix, + + /// EAN-8 1D format. + ean8, + + /// EAN-13 1D format. + ean13, + + /// ITF (Interleaved Two of Five) 1D format. + itf, + + /// MaxiCode 2D barcode format. + maxicode, + + /// PDF417 format. + pdf417, + + /// QR Code 2D barcode format. + qrcode, + + /// RSS 14 + rss14, + + /// RSS EXPANDED + rssExpanded, + + /// UPC-A 1D format. + upcA, + + /// UPC-E 1D format. + upcE, + + /// UPC/EAN extension format. Not a stand-alone format. + upcEanExtension +} + +const _formatNames = { + 'AZTEC': BarcodeFormat.aztec, + 'CODABAR': BarcodeFormat.codabar, + 'CODE_39': BarcodeFormat.code39, + 'CODE_93': BarcodeFormat.code93, + 'CODE_128': BarcodeFormat.code128, + 'DATA_MATRIX': BarcodeFormat.dataMatrix, + 'EAN_8': BarcodeFormat.ean8, + 'EAN_13': BarcodeFormat.ean13, + 'ITF': BarcodeFormat.itf, + 'MAXICODE': BarcodeFormat.maxicode, + 'PDF_417': BarcodeFormat.pdf417, + 'QR_CODE': BarcodeFormat.qrcode, + 'RSS_14': BarcodeFormat.rss14, + 'RSS_EXPANDED': BarcodeFormat.rssExpanded, + 'UPC_A': BarcodeFormat.upcA, + 'UPC_E': BarcodeFormat.upcE, + 'UPC_EAN_EXTENSION': BarcodeFormat.upcEanExtension, +}; + +class Barcode { + Barcode(this.code, this.format); + + final String code; + final BarcodeFormat format; +} + class QRView extends StatefulWidget { const QRView({ @required Key key, @@ -107,7 +187,16 @@ class QRViewController { switch (call.method) { case scanMethodCall: if (call.arguments != null) { - _scanUpdateController.sink.add(call.arguments.toString()); + final args = call.arguments as Map; + final code = args['code'] as String; + final rawType = args['type'] as String; + final format = _formatNames[rawType]; + if (format != null) { + final barcode = Barcode(code, format); + _scanUpdateController.sink.add(barcode); + } else { + throw Exception('Unexpected barcode type $rawType'); + } } } }, @@ -118,10 +207,10 @@ class QRViewController { final MethodChannel _channel; - final StreamController _scanUpdateController = - StreamController(); + final StreamController _scanUpdateController = + StreamController(); - Stream get scannedDataStream => _scanUpdateController.stream; + Stream get scannedDataStream => _scanUpdateController.stream; void flipCamera() { _channel.invokeMethod('flipCamera');