Flutter二维码扫描 [mobile_scanner]

引入库:https://pub.dev/packages/mobile_scanner

dependencies:
   mobile_scanner: ^1.0.0

最小SDK不能低于:21

// l:\Flutter\FlutterProject\untitled\android\app\build.gradle

defaultConfig {
        minSdkVersion 21
    }

实例代码:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

void main() => runApp(const MaterialApp(home: MyHome()));

class MyHome extends StatelessWidget {
  const MyHome({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ///移动扫描仪控制器
    var _controller = MobileScannerController(torchEnabled: false);
    var zt = true;
    return Scaffold(
      appBar: AppBar(title: const Text('二维码扫描')),
      body: Column(
        children: [
          Container(
            width: 300,
            height: 300,
           //移动扫描仪组件
            child: MobileScanner(
              //允许重复
              // allowDuplicates: true,
              //控制器
              controller: _controller,
              //扫描仪扫描结果回调
              onDetect: (barcode, args) {
                // print(args!.hasTorch);
                // print(args.size);
                // print(args.textureId);
                // print(args.webId);

                ///扫描的值
                print(barcode.rawValue); //字符串
                print(barcode.rawBytes); //字节
                print(barcode.wifi?.password); //扫描wifi二维码,返回WiFi数据
                print(barcode.wifi?.encryptionType); //扫描wifi二维码,返回WiFi数据
                print(barcode.wifi?.ssid); //扫描wifi二维码,返回WiFi数据
              },
            ),
          ),

          ///开启关闭闪光灯
          MaterialButton(
            onPressed: () {
              // _controller.analyzeImage('path'); //加载图片
              _controller.toggleTorch(); //切换闪光灯状态
            },
            child: Text('开启闪光灯'),
          ),

          ///开始暂定
          MaterialButton(
            onPressed: () {
              zt ? _controller.stop() : _controller.start();
              zt = !zt;
            },
            child: Text('开始/暂停'),
          ),

          ///相机切换
          MaterialButton(
            onPressed: () {
              _controller.switchCamera(); //
            },
            child: Text('相机切换'),
          ),
        ],
      ),
    );
  }
}
THE END
喜欢就支持一下吧
点赞8 分享