我正在使用 flutter 构建一个具有二维码扫描功能的 Android 应用程序。我已经将二维码逻辑分解为最小重现案例here .
奇怪的是,二维码扫描仅适用于我的三星设备。使用 google/firebase ml Vision 条形码扫描模型扫描 QR 码时,我的 Google Pixel XL 和 Oneplus 6t 均未拾取任何内容。
存储库中的关键代码位置是:
android/app/build.gradle
我在其中包含条形码模型 api:
api 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.2'
lib/src/bloc/services/qr_service.dart
我在图像上运行条形码检测:
_processImage(CameraImage image) async {
if (!_alreadyCheckingImage && !_foundRoomId) {
_alreadyCheckingImage = true;
try {
final barcodes = await barcodeDetector.detectInImage(
FirebaseVisionImage.fromBytes(
_concatenatePlanes(image.planes),
FirebaseVisionImageMetadata(
rawFormat: image.format.raw,
size: Size(image.width.toDouble(), image.height.toDouble()),
rotation: ImageRotation.rotation0,
planeData: image.planes
.map((plane) => FirebaseVisionImagePlaneMetadata(
bytesPerRow: plane.bytesPerRow,
height: plane.height,
width: plane.width,
),
)
.toList(),
),
),
);
if (barcodes != null && barcodes.length > 0) {
try {
print('\n~~~');
print(barcodes.first.toString());
print(barcodes.first.displayValue);
print(barcodes.first.valueType);
print(barcodes.first.rawValue);
print('~~~\n');
final barcode = barcodes.first;
print(barcode.rawValue);
qrResult.sink.add(barcode.rawValue);
_foundRoomId = true;
} catch (err, stack) {
print('$err\n$stack');
}
}
} catch (err, stack) {
debugPrint('$err, $stack');
}
_alreadyCheckingImage = false;
}
}
Uint8List _concatenatePlanes(List<Plane> planes) {
final WriteBuffer allBytes = WriteBuffer();
planes.forEach((plane) => allBytes.putUint8List(plane.bytes));
return allBytes.done().buffer.asUint8List();
}
我希望我做错了什么。但很奇怪的是,这个最低限度的重现在我的三星 S8、三星 J7 和三星 S10+ 上完美运行,但在我的 Oneplus 6t (android 10) 上不起作用,在我的 Google Pixel XL (android 9) 上不起作用
最佳答案
尝试在 CameraController 中将相机设置设置为“高”,这对我来说很有效。
当您初始化相机 Controller 时,您想要执行以下操作:
final newCameraController = CameraController(
cameras.first,
ResolutionPreset.high,
enableAudio: false,
);
这应该让所有设备都可以进行扫描。
关于android - Google 的 Firebase ML Vision 仅适用于我的三星设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59111768/