简体   繁体   English

在颤振网络中扫描二维码的可能性

[英]Possibilities to scan qr-code in flutter web

I want to build a PWA based on flutter web which relies on scanning a QR-Code and accessing the user's current position.我想构建一个基于 flutter web 的 PWA,它依赖于扫描二维码并访问用户的当前位置。

From my point of knowledge for both functionalities there are currently no packages available.根据我对这两个功能的了解,目前没有可用的软件包。

However for getting the user's location there is a stackoverflow thread ( Get User's Location for flutter Web ) providing a solution.但是,为了获取用户的位置,有一个 stackoverflow 线程( Get User's Location for flutter Web )提供了解决方案。

So I'm wondering if there is any chance that there is a similar solution for the QR-Scanning issue?所以我想知道是否有可能针对 QR 扫描问题有类似的解决方案?

Sadly, I'm not very familiar with web and javascript development.遗憾的是,我对 web 和 javascript 开发不是很熟悉。 So any help would be highly appreciated.因此,任何帮助将不胜感激。

Following these two articels, I managed to use the jsQR Library in my flutter web application.遵循这两篇文章,我设法在我的 Flutter Web 应用程序中使用了 jsQR 库。

https://medium.com/@mk.pyts/how-to-access-webcam-video-stream-in-flutter-for-web-1bdc74f2e9c7 https://medium.com/@mk.pyts/how-to-access-webcam-video-stream-in-flutter-for-web-1bdc74f2e9c7

https://medium.com/flutter-community/using-javascript-code-in-flutter-web-903de54a2000 https://medium.com/flutter-community/using-javascript-code-in-flutter-web-903de54a2000

https://github.com/cozmo/jsQR https://github.com/cozmo/jsQR

By this, I'm now able to scan a QR-code in flutter web.通过这个,我现在可以在 flutter web 中扫描二维码。

I used the library ai_barcode .我使用了图书馆ai_barcode

Documentation of the plugin is teribble so I post here my widget:该插件的文档太糟糕了,所以我在这里发布了我的小部件:

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

class _BarcodeScannerWidget extends StatefulWidget {
  const _BarcodeScannerWidget(this.resultCallback);

  final void Function(String result) resultCallback;

  @override
  State<StatefulWidget> createState() {
    return _AppBarcodeScannerWidgetState();
  }
}

class _AppBarcodeScannerWidgetState extends State<_BarcodeScannerWidget> {
  late ScannerController _scannerController;

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

    _scannerController = ScannerController(scannerResult: (result) {
      widget.resultCallback(result);
    }, scannerViewCreated: () {
      final TargetPlatform platform = Theme.of(context).platform;
      if (TargetPlatform.iOS == platform) {
        Future.delayed(const Duration(seconds: 2), () {
          _scannerController
            ..startCamera()
            ..startCameraPreview();
        });
      } else {
        _scannerController
          ..startCamera()
          ..startCameraPreview();
      }
    });
  }

  @override
  void dispose() {
    super.dispose();

    _scannerController
      ..stopCameraPreview()
      ..stopCamera();
  }

  @override
  Widget build(BuildContext context) {
    return PlatformAiBarcodeScannerWidget(
      platformScannerController: _scannerController,
    );
  }
}

Also add the jsQR.js from plugin repository.还要从插件存储库中添加jsQR.js。

ai_barcode: ^3.0.1

And add this line to index.html:并将这一行添加到 index.html:

<script src="jsQR.js" type="application/javascript"></script> <!-- Add this line -->
<script src="main.dart.js" type="application/javascript"></script>

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

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