简体   繁体   English

Flutter。 如何在没有 UI 的情况下扫描条形码

[英]Flutter. How to scan a barcode without UI

How can you scan a 2D barcode using a flutter without the camera's UI?如何在没有相机 UI 的情况下使用 flutter 扫描二维条码? I don't need to see the camera preview.我不需要查看相机预览。 I just want to click on the "Scan" button, and the camera started looking for a barcode, but it did not display what the phone's camera sees in UI.我只想点击“扫描”按钮,相机开始寻找条形码,但它没有显示手机相机在 UI 中看到的内容。

You could look into using the qr_code_scanner package ( pub link ).您可以考虑使用 qr_code_scanner package(发布链接)。 Instead of opening in a new view, it uses a custom view for the preview.它不是在新视图中打开,而是使用自定义视图进行预览。 You can then manipulate the view's dimensions to fit your needs.然后,您可以操纵视图的尺寸以满足您的需要。 Note that making it invisible using Visibility or by giving its parent size 0 will disable the camera.请注意,使用Visibility或将其父尺寸设为 0 使其不可见将禁用相机。 Giving it (for example) a height of 1 pixel will still work.给它(例如)1 像素的高度仍然有效。

Example code (run with version 0.4.0 of the qr_code_scanner package)示例代码(使用qr_code_scanner包的0.4.0版运行)

import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'QR scanner demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String result = '';
  QRViewController controller;
  bool isVisible = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 200,
              // 1 pixel, as a height 0 disables the view
              height: isVisible ? 100 : 1,
              child: QRView(
                key: GlobalKey(),
                onQRViewCreated: _onQRViewCreated,
              ),
            ),
            Text(
              'The barcode value was:',
            ),
            Text(
              '$result',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.visibility),
        onPressed: () {
          setState(() {
            isVisible = !isVisible;
          });
        },
      ),
    );
  }

  void _onQRViewCreated(QRViewController controller) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
      setState(() {
        result = scanData.code;
      });
    });
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }
}

I found that one solution is to put the QRView inside a stack, st it is hidden under another view我发现一个解决方案是将QRView放在一个堆栈中,它隐藏在另一个视图下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM