diff --git a/README.md b/README.md index 9a53a46..ea9af2f 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ A barcode and QR code scanner based on [ZXing C++](https://github.com/nu-book/zxing-cpp) library natively in Flutter with Dart FFI. - ## Demo Screenshots +
 01_scanner_screen  02_creator_screen 
 
@@ -34,6 +34,7 @@ A barcode and QR code scanner based on [ZXing C++](https://github.com/nu-book/zx | | DataBar Expanded | ## Features + - Scan barcode from camera stream, image path or url - Scan multiple barcodes from camera stream, image path or url - Create barcode from text @@ -41,10 +42,13 @@ A barcode and QR code scanner based on [ZXing C++](https://github.com/nu-book/zx - Flashlight and pinch to zoom support ## Todo + - Write tests ## Getting Started -### To read barcode: + +### To read barcode + ```dart import 'package:flutter_zxing/flutter_zxing.dart'; @@ -62,31 +66,34 @@ Widget build(BuildContext context) { // Or use flutter_zxing plugin methods // To read barcode from camera image directly -await startCameraProcessing(); // Call this in initState +await zx.startCameraProcessing(); // Call this in initState + cameraController?.startImageStream((image) async { - CodeResult result = await processCameraImage(image); - if (result.isValidBool) { - debugPrint(result.textString); + Code result = await zx.processCameraImage(image); + if (result.isValid) { + debugPrint(result.text); } return null; }); -stopCameraProcessing(); // Call this in dispose + +zx.stopCameraProcessing(); // Call this in dispose // To read barcode from XFile, String, url or Uint8List bytes XFile xFile = XFile('Your image path'); -CodeResult? resultFromXFile = await readBarcodeImagePath(xFile); +Code? resultFromXFile = await zx.readBarcodeImagePath(xFile); String path = 'Your local image path'; -CodeResult? resultFromPath = await readBarcodeImagePathString(path); +Code? resultFromPath = await zx.readBarcodeImagePathString(path); String url = 'Your remote image url'; -CodeResult? resultFromUrl = await readBarcodeImageUrl(url); +Code? resultFromUrl = await zx.readBarcodeImageUrl(url); Uint8List bytes = Uint8List.fromList(yourImageBytes); -CodeResult? resultFromBytes = await readBarcode(bytes); +Code? resultFromBytes = await zx.readBarcode(bytes); ``` -### To create barcode: +### To create barcode + ```dart import 'package:flutter_zxing/flutter_zxing.dart'; import 'dart:typed_data'; @@ -108,7 +115,7 @@ Widget build(BuildContext context) { ); // Or use FlutterZxing to create barcode directly -final result = encodeBarcode( +final result = zx.encodeBarcode( 'Text to encode', format: Format.QRCode, width: 300, @@ -116,7 +123,7 @@ final result = encodeBarcode( margin: 10, eccLevel: 0, ); -if (result.isValidBool) { +if (result.isValid) { final img = imglib.Image.fromBytes(width, height, result.bytes); final encodedBytes = Uint8List.fromList(imglib.encodeJpg(img)); // use encodedBytes as you wish diff --git a/zxscanner/lib/main.dart b/zxscanner/lib/main.dart index 3a1cdbf..8b4dd26 100644 --- a/zxscanner/lib/main.dart +++ b/zxscanner/lib/main.dart @@ -18,7 +18,7 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); await initializePrefs(); await DbService.instance.initializeApp(); - setZxingLogEnabled(kDebugMode); + zx.setLogEnabled(kDebugMode); runApp(const MyApp()); } diff --git a/zxscanner/lib/models/code.dart b/zxscanner/lib/models/code.dart index bc3d53d..f5f6b05 100644 --- a/zxscanner/lib/models/code.dart +++ b/zxscanner/lib/models/code.dart @@ -1,4 +1,4 @@ -import 'package:flutter_zxing/flutter_zxing.dart'; +import 'package:flutter_zxing/flutter_zxing.dart' as zxing; import 'package:hive_flutter/hive_flutter.dart'; part 'code.g.dart'; @@ -7,10 +7,10 @@ part 'code.g.dart'; class Code extends HiveObject { Code(); - Code.fromCodeResult(CodeResult result) { - isValid = result.isValidBool; + Code.fromCodeResult(zxing.Code result) { + isValid = result.isValid; format = result.format; - text = result.textString; + text = result.text; } @HiveField(0) bool? isValid; @@ -21,5 +21,5 @@ class Code extends HiveObject { @HiveField(2) String? text; - String get formatName => barcodeFormatName(format ?? 0); + String get formatName => zxing.zx.barcodeFormatName(format ?? 0); } diff --git a/zxscanner/lib/models/encode.dart b/zxscanner/lib/models/encode.dart index 65789a2..a70b999 100644 --- a/zxscanner/lib/models/encode.dart +++ b/zxscanner/lib/models/encode.dart @@ -1,6 +1,6 @@ import 'dart:typed_data'; -import 'package:flutter_zxing/flutter_zxing.dart'; +import 'package:flutter_zxing/flutter_zxing.dart' as zxing; import 'package:hive_flutter/hive_flutter.dart'; part 'encode.g.dart'; @@ -9,10 +9,10 @@ part 'encode.g.dart'; class Encode extends HiveObject { Encode(); - Encode.fromEncodeResult(EncodeResult result, Uint8List? bytes) { - isValid = result.isValidBool; + Encode.fromEncodeResult(zxing.Encode result, Uint8List? bytes) { + isValid = result.isValid; format = result.format; - text = result.textString; + text = result.text; data = bytes; length = result.length; } @@ -31,5 +31,5 @@ class Encode extends HiveObject { @HiveField(4) int? length; - String get formatName => barcodeFormatName(format ?? 0); + String get formatName => zxing.zx.barcodeFormatName(format ?? 0); } diff --git a/zxscanner/lib/pages/creator_page.dart b/zxscanner/lib/pages/creator_page.dart index b370df5..b0ccaa5 100644 --- a/zxscanner/lib/pages/creator_page.dart +++ b/zxscanner/lib/pages/creator_page.dart @@ -7,7 +7,7 @@ import 'package:path_provider/path_provider.dart'; import 'package:share_plus/share_plus.dart'; import '../configs/constants.dart'; -import '../models/encode.dart'; +import '../models/encode.dart' as model; import '../utils/db_service.dart'; import '../utils/extensions.dart'; import '../widgets/common_widgets.dart'; @@ -28,7 +28,7 @@ class _CreatorPageState extends State { bool isAndroid() => Theme.of(context).platform == TargetPlatform.android; // Write result - Encode? encode; + model.Encode? encode; @override void initState() { @@ -54,9 +54,9 @@ class _CreatorPageState extends State { child: Column( children: [ WriterWidget( - onSuccess: (EncodeResult result, Uint8List? bytes) { + onSuccess: (Encode result, Uint8List? bytes) { setState(() { - encode = Encode.fromEncodeResult(result, bytes); + encode = model.Encode.fromEncodeResult(result, bytes); }); }, onError: (String error) { diff --git a/zxscanner/lib/pages/scanner_page.dart b/zxscanner/lib/pages/scanner_page.dart index 2b7f45f..dd69b99 100644 --- a/zxscanner/lib/pages/scanner_page.dart +++ b/zxscanner/lib/pages/scanner_page.dart @@ -3,7 +3,7 @@ import 'package:flutter_zxing/flutter_zxing.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:image_picker/image_picker.dart'; -import '../models/models.dart'; +import '../models/models.dart' as model; import '../utils/db_service.dart'; import '../utils/extensions.dart'; @@ -26,7 +26,7 @@ class _ScannerPageState extends State { title: const Text('Scanner'), ), body: ReaderWidget( - onScan: (CodeResult result) async { + onScan: (Code result) async { addCode(result); }, ), @@ -51,8 +51,8 @@ class _ScannerPageState extends State { } Future readCodeFromImage(XFile file) async { - final CodeResult? result = await readBarcodeImagePath(file); - if (result != null && result.isValidBool) { + final Code? result = await zx.readBarcodeImagePath(file); + if (result != null && result.isValid) { addCode(result); } else { if (!mounted) { @@ -62,8 +62,8 @@ class _ScannerPageState extends State { } } - void addCode(CodeResult result) { - final Code code = Code.fromCodeResult(result); + void addCode(Code result) { + final model.Code code = model.Code.fromCodeResult(result); DbService.instance.addCode(code); context.showToast('Code added:\n${code.text ?? ''}'); } diff --git a/zxscanner/pubspec.lock b/zxscanner/pubspec.lock index cfcff9c..9e95d37 100644 --- a/zxscanner/pubspec.lock +++ b/zxscanner/pubspec.lock @@ -77,7 +77,7 @@ packages: name: build_runner url: "https://pub.dartlang.org" source: hosted - version: "2.3.2" + version: "2.3.3" build_runner_core: dependency: transitive description: @@ -321,7 +321,7 @@ packages: name: flutter_zxing url: "https://pub.dartlang.org" source: hosted - version: "0.8.5" + version: "0.9.0" font_awesome_flutter: dependency: "direct main" description: @@ -426,7 +426,7 @@ packages: name: image_picker_android url: "https://pub.dartlang.org" source: hosted - version: "0.8.5+3" + version: "0.8.5+4" image_picker_for_web: dependency: transitive description: @@ -440,7 +440,7 @@ packages: name: image_picker_ios url: "https://pub.dartlang.org" source: hosted - version: "0.8.6+1" + version: "0.8.6+2" image_picker_platform_interface: dependency: transitive description: @@ -928,7 +928,7 @@ packages: name: win32 url: "https://pub.dartlang.org" source: hosted - version: "3.1.2" + version: "3.1.3" xdg_directories: dependency: transitive description: diff --git a/zxscanner/pubspec.yaml b/zxscanner/pubspec.yaml index 5c84f10..cffcc1f 100644 --- a/zxscanner/pubspec.yaml +++ b/zxscanner/pubspec.yaml @@ -36,7 +36,7 @@ flutter_intl: enabled: true dev_dependencies: - build_runner: ^2.2.1 + build_runner: ^2.3.3 # espresso: ^0.2.0+2 flutter_lints: ^2.0.1 flutter_native_splash: ^2.2.16 # flutter pub run flutter_native_splash:create