android - flutter : Overlaying the graphics on the barcode which is detected by firebase vision

标签 android flutter barcode-scanner firebase-mlkit

我正在尝试在 Flutter 中实现一个条形码扫描器应用程序,其中相机可以作为小部件嵌入而不是全屏显示。

所以我选择了 flutter_qr_mobile_vision打包,因为我们可以使用 native 相机功能并限制相机预览大小。

但上面的包只给出了 MLKit 检测到的条码字符串,而不是边界框、条码类型等条码细节,所以我把上面包的源代码做了一些修改,这样我得到了可用于在检测到的条形码上叠加图形的边界框。

可以找到我试图做的代码here

现在我在使用这些更改时面临两个问题(目前仅限 Android 实现)

  1. 当我尝试在条形码顶部叠加边界框时,结果在多个设备上不一致。

例如:

在一加上全屏工作正常

Result on oneplus

Redmi 5 上相同的代码输出

Result on redmi 5

我将所有设备的相机分辨率默认设置为 (1280 x 720),并在叠加之前缩放输出

我试图了解在不同设备上造成这种情况的原因

  1. 现在如果我尝试调整相机预览的小部件高度,结果在 oneplus 上也不一致

enter image description here

我觉得这与缩放部分有关,但我不确定。

以下代码用于相机预览和叠加图形

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:qr_mobile_vision/qr_camera.dart';
import 'package:qr_mobile_vision/qr_barcode.dart';
import 'package:vision_demo/barcode_detctor_painter.dart';
import 'package:vision_demo/overlay.dart';

void main() {
  debugPaintSizeEnabled = false;
  runApp(new HomePage());
}

class HomePage extends StatefulWidget {
  @override
  HomeState createState() => new HomeState();
}

class HomeState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(home: new MyApp());
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool camState = false;
  List<Barcode> barcode = List<Barcode>();

  @override
  initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Demo app'),
      ),
      body: new Center(
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            camState
                ? new SizedBox(
                    width: MediaQuery.of(context).size.width,
                    height: MediaQuery.of(context).size.height - 300 - AppBar().preferredSize.height,
                    child: Stack(
                      children: [
                        new QrCamera(
                          onError: (context, error) => Text(
                            error.toString(),
                            style: TextStyle(color: Colors.red),
                          ),
                          qrCodeCallback: (code) {
                            setState(() {
                              barcode = code;
                            });
                          },
                          child: new Container(
                            decoration: new BoxDecoration(
                              color: Colors.transparent,
                            ),
                          ),
                        ),
                        Container(
                            constraints: const BoxConstraints.expand(),
                            decoration: ShapeDecoration(
                              shape: QrScannerOverlayShape(cutOutSize: MediaQuery.of(context).size.width - 160, borderColor: Colors.white),
                            )),
                        LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
                          return _buildResults(constraints);
                        })
                      ],
                    ),
                  )
                : Expanded(child: new Center(child: new Text("Camera inactive"))),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
          child: new Text(
            "press me",
            textAlign: TextAlign.center,
          ),
          onPressed: () {
            setState(() {
              camState = !camState;
            });
          }),
    );
  }

  Widget _buildResults(BoxConstraints constraints) {
    const Text noResultsText = Text('No results!');

    if (barcode == null) {
      return noResultsText;
    }

    CustomPainter painter;

    final Size imageSize = Size(720.0, 1280.0);

    if (barcode is! List<Barcode>) return noResultsText;
    painter = BarcodeDetectorPainter(imageSize, barcode);

    return CustomPaint(
      size: Size(double.maxFinite, double.maxFinite),
      painter: painter,
    );
  }
}

下面是我用来调整覆盖图形大小的代码。

import 'package:flutter/material.dart';
import 'package:qr_mobile_vision/qr_barcode.dart';

class BarcodeDetectorPainter extends CustomPainter {
  BarcodeDetectorPainter(this.absoluteImageSize, this.barcodeLocations); // absoluteImageSize will always be (1280 x 720) 

  final List<Barcode> barcodeLocations;
  final Size absoluteImageSize;

  @override
  void paint(Canvas canvas, Size size) {
    final double scaleX = size.width / absoluteImageSize.width;
    final double scaleY = size.height / absoluteImageSize.height;

    Rect scaleRect(Barcode barcode) {
      return Rect.fromLTRB(
        barcode.boundingBox.left * scaleX,
        barcode.boundingBox.top * scaleY,
        barcode.boundingBox.right * scaleX,
        barcode.boundingBox.bottom * scaleY,
      );
    }

    final Paint paint = Paint()
      ..style = PaintingStyle.fill
      ..strokeWidth = 2.0;

    for (Barcode barcode in barcodeLocations) {
      paint.color = Colors.green;

      canvas.drawRect(scaleRect(barcode), paint);
    }
  }

  @override
  bool shouldRepaint(BarcodeDetectorPainter oldDelegate) {
    return oldDelegate.absoluteImageSize != absoluteImageSize || oldDelegate.barcodeLocations != barcodeLocations;
  }
}


解决这两个问题很重要,因为在此之后我想限制扫描区域,以便我只能捕获与我添加的切口尺寸对齐的条码。

感谢任何形式的帮助。

最佳答案

我能够解决这个问题,问题是在多个设备上有一个恒定的分辨率,有些设备可能没有我设置的分辨率,所以根据屏幕尺寸以编程方式设置最佳最小分辨率解决了我的问题.

关于android - flutter : Overlaying the graphics on the barcode which is detected by firebase vision,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65953755/

相关文章:

firebase - Future <dynamic>不是 “Widget”的子类型

Flutter:将数据传递给状态

iphone - PhoneGap + iOS 异常 : [UIWebOverflowScrollView _viewDelegate] message sent to deallocated instance 0x21e330

java - 错误 RunTimeException 无法实例化 Activity ComponentInfo{...} : java. lang.NullPointerException

工具栏下方的 Android RecyclerView

firebase - 即使我修改了某些字段,如何才能每次只从 Firestore 获取一次数据?

android - 使用 Android 阅读低对比度 (3D) 打印的二维码

C# 移动扫描仪值表现得很奇怪

android - 如何从排行榜中删除或更改玩家的分数?

android - OpenGL ES 2.0 奇怪的着色器故障