flutter Zxing2 pub 包有任何工作示例吗?我确实尝试了官方的 pub 包示例 [https://pub.dev/packages/zxing2/example | Dart包]但是不起作用。我收到以下错误
I/CameraManagerGlobal( 5452): Connecting to camera service
E/flutter ( 5452): [ERROR:flutter/lib/ui/ui_dart_state.cc(213)] Unhandled Exception: CameraException(Uninitialized CameraController, startImageStream() was called on an uninitialized CameraController.)
E/flutter ( 5452): #0 CameraController._throwIfNotInitialized (package:camera/src/camera_controller.dart:769:7)
E/flutter ( 5452): #1 CameraController.startImageStream (package:camera/src/camera_controller.dart:370:5)
E/flutter ( 5452): #2 _MyHomePageState._onCameraSelected (package:test_camera/main.dart:83:26)
E/flutter ( 5452): #3 _MyHomePageState.initState (package:test_camera/main.dart:46:5)
E/flutter ( 5452): #4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4796:57)
E/flutter ( 5452): #5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4631:5)
E/flutter ( 5452): #6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #7 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #8 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6186:14)
E/flutter ( 5452): #9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #11 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4683:16)
E/flutter ( 5452): #12 Element.rebuild (package:flutter/src/widgets/framework.dart:4348:5)
E/flutter ( 5452): #13 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4636:5)
E/flutter ( 5452): #14 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4631:5)
E/flutter ( 5452): #15 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #16 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #17 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6186:14)
E/flutter ( 5452): #18 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #19 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #20 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6186:14)
E/flutter ( 5452): #21 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4683:16)
E/flutter ( 5452): #24 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4831:11)
E/flutter ( 5452): #25 Element.rebuild (package:flutter/src/widgets/framework.dart:4348:5)
E/flutter ( 5452): #26 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4636:5)
E/flutter ( 5452): #27 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4822:11)
E/flutter ( 5452): #28 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4631:5)
E/flutter ( 5452): #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #31 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6186:14)
E/flutter ( 5452): #32 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #33 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #34 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6186:14)
E/flutter ( 5452): #35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #36 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4683:16)
E/flutter ( 5452): #38 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4831:11)
E/flutter ( 5452): #39 Element.rebuild (package:flutter/src/widgets/framework.dart:4348:5)
E/flutter ( 5452): #40 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4636:5)
E/flutter ( 5452): #41 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4822:11)
E/flutter ( 5452): #42 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4631:5)
E/flutter ( 5452): #43 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3666:14)
E/flutter ( 5452): #44 Element.updateChild (package:flutter/src/widgets/framework.dart:3418:18)
E/flutter ( 5452): #45 C
引发错误的代码位于第 83 行。
有人遇到同样的问题吗?如果有人给我一个使用 zxing2 的良好工作示例,我将非常感激。
注意:我只需要使用zxing2而不是qr或条形码扫描仪
谢谢
我通过添加调用修复了这个错误
await controller.initialize();
之前unawaited(controller.startImageStream((image) {
采用 _onCameraSelected
方法。
但是这个例子不包含zxing2的用法...
我知道这很旧,但没有一个好的答案。我可以回答问题的两部分:
import 'package:zxing2/qrcode.dart';
import 'package:image/image.dart' as img;
...
class _QrCodeScannerState extends State<QrCodeScanner> {
CameraController? controller;
List<CameraDescription>? cameras;
@override
void initState() {
super.initState();
initializeCamera(); // this is fire and forget
}
Future<void> initializeCamera() async {
cameras = await availableCameras(); // get available cameras
if (cameras!.isNotEmpty) {
controller = CameraController(cameras!.first, ResolutionPreset.high, imageFormatGroup: ImageFormatGroup.jpeg);
await controller!.initialize();
if (mounted) {
setState(() {}); // we can now show the preview
controller!.startImageStream(onLatestImage); // and can start the imageStream
}
}
}
void onLatestImage(CameraImage image) {
try {
final barcode = decode(image.planes.first.bytes);
print('$barcode');
} catch (e) {
print('Failed to decode image');
}
}
String decode(Uint8List fileBytes) {
final image = img.decodeImage(fileBytes)!;
LuminanceSource source = RGBLuminanceSource(
image.width,
image.height,
image
.convert(numChannels: 4)
.getBytes(order: img.ChannelOrder.rgba)
.buffer
.asInt32List());
final bitmap = BinaryBitmap(HybridBinarizer(source));
final reader = QRCodeReader();
return reader.decode(bitmap).text;
}
@override
Widget build(BuildContext context) {
if (controller == null || !controller!.value.isInitialized) {
// important as we have to wait for the CameraController to be initialised
return const Center(child: CircularProgressIndicator());
}
return Scaffold(
body: Center(
child: CameraPreview(controller!),
),
);
}
上面确实回答了第一部分——CameraController的正确使用以及获取CameraImage,以及最后的过去——如何使用zxing2扫描图像。但是,我不能保证回答了从 CameraImage 到 zxing2 能理解的图像转换的问题。