Przeglądaj źródła

Merge pull request #102 from factisresearch/codeformat

Add BarcodeFormat to result
flutter-beta
Julian Steenbakker 5 lat temu
committed by GitHub
rodzic
commit
e8c008f739
Nie znaleziono w bazie danych klucza dla tego podpisu ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 136 dodań i 9 usunięć
  1. +2
    -1
      android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt
  2. +11
    -3
      example/lib/main.dart
  3. +30
    -1
      ios/Classes/QRView.swift
  4. +93
    -4
      lib/src/qr_code_scanner.dart

+ 2
- 1
android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt Wyświetl plik

@@ -116,7 +116,8 @@ class QRView(messenger: BinaryMessenger, id: Int, private val context: Context)
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<ResultPoint>) {}


+ 11
- 3
example/lib/main.dart Wyświetl plik

@@ -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<QRViewExample> {
var qrText = '';
Barcode result;
var flashState = flashOn;
var cameraState = frontCamera;
QRViewController controller;
@@ -32,6 +33,13 @@ class _QRViewExampleState extends State<QRViewExample> {

@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: <Widget>[
@@ -43,7 +51,7 @@ class _QRViewExampleState extends State<QRViewExample> {
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('This is the result of scan: $qrText'),
Text(resultText),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
@@ -160,7 +168,7 @@ class _QRViewExampleState extends State<QRViewExample> {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {
setState(() {
qrText = scanData;
result = scanData;
});
});
}


+ 30
- 1
ios/Classes/QRView.swift Wyświetl plik

@@ -26,8 +26,37 @@ public class QRView:NSObject,FlutterPlatformView {
try scanner?.startScanning(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 }
self?.channel.invokeMethod("onRecognizeQR", arguments: stringValue)
let result = ["code": stringValue, "type": typeString]
self?.channel.invokeMethod("onRecognizeQR", arguments: result)
}
}
})


+ 93
- 4
lib/src/qr_code_scanner.dart Wyświetl plik

@@ -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 = <String, BarcodeFormat>{
'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,
@@ -106,7 +186,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');
}
}
}
},
@@ -117,10 +206,10 @@ class QRViewController {

final MethodChannel _channel;

final StreamController<String> _scanUpdateController =
StreamController<String>();
final StreamController<Barcode> _scanUpdateController =
StreamController<Barcode>();

Stream<String> get scannedDataStream => _scanUpdateController.stream;
Stream<Barcode> get scannedDataStream => _scanUpdateController.stream;

void flipCamera() {
_channel.invokeMethod('flipCamera');


Ładowanie…
Anuluj
Zapisz