zoukankan      html  css  js  c++  java
  • lutter 调用原生硬件 Api 实现扫码

    一、Flutter 扫描二维码条形码插件 barcode_scan

    1、安装

    2、配置权限
    Add the camera permission to your AndroidManifest.xml

    Add the BarcodeScanner activity to your AndroidManifest.xml. Do NOT modify the name.

    3、检查、配置 build.gradle

    dependencies: barcode_scan: ^1.0.0

    <activity android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/>

    3.1 编辑你的 android 目录下面的 build.gradle (Edit your project-level build.gradle file to look like this)

    注意:官方文档配置的kotlin_version的版本是1.2.31,但是实际发现1.2.31 会报错。所以本项目使用 1.3.0。

    buildscript { ext.kotlin_version = '1.3.0' ...
    dependencies {

    ...

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    } }

    3.2 编辑你的 android/app 目录下面的 build.gradle (Edit your app-level build.gradle file to look like this)

    apply plugin: 'kotlin-android' ...
    dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    ...

    }

    4、使用

    import 'package:flutter/material.dart';
    import 'package:barcode_scan/barcode_scan.dart'; import 'package:flutter/services.dart';
    class ScanPage extends StatefulWidget {
      ScanPage({Key key}) : super(key: key);
    

    _ScanPageState createState() => _ScanPageState(); }

    class _ScanPageState extends State<ScanPage> {

    var barcode;

    Future _scan() async { try {

    String barcode = await BarcodeScanner.scan(); setState(() {

    return this.barcode = barcode; });

    } on PlatformException catch (e) {
    if (e.code == BarcodeScanner.CameraAccessDenied) {

    setState(() {
    return this.barcode = 'The user did not grant the camera

    permission!'; });

          } else {
            setState(() {
    

    return this.barcode = 'Unknown error: $e'; });

          }
        } on FormatException{
    

    setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');

    } catch (e) {
    setState(() => this.barcode = 'Unknown error: $e');

    } }

      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          floatingActionButton: FloatingActionButton(
    

    child: Icon(Icons.camera_roll),

    onPressed: _scan, ),

    appBar: AppBar( title: Text("扫码"),

    ),

    body:Text("扫码--${barcode}"), );

    } }

    二、Flutter 使用barcode_scan提示如下 错误解决方案

    Android dependency ‘androidx.core:core’ has different version for the compile (1.0.0) and runtime (1.0.2) classpath. You should manually set the same version via DependencyResolution

    http://bbs.itying.com/topic/5d0468735923fe0334c35ea2

  • 相关阅读:
    Silverlight C# 游戏开发:Flyer05与什么什么进行搏斗
    Silverlight C# 游戏开发:Flyer07做一个有开始的游戏
    Silverlight C# 游戏开发:面向对象在游戏中的实例(一)
    Silverlight C# 游戏开发:面向对象在游戏中的实例(二)
    Silverlight C# 游戏开发:Flyer06小小的改进让游戏更有趣
    linux网络命令ip\route\links回顾
    Google Style的C++编码规范
    TCP/IP协议和IP组播的视频传输
    Multicast server and client in Python
    用户profile中umask码的含义详解(默认是022)
  • 原文地址:https://www.cnblogs.com/zhaofeis/p/12763078.html
Copyright © 2011-2022 走看看