Flutter——扫描条形码以及二维码

扫描条形码以及二维码,需要用到barcode_scan插件。

  • 添加依赖

dependencies:

  ...
  barcode_scan: ^1.0.0

 

  • Android配置

  • 先配置 android\app\src\main\AndroidManifest.xml,在该文件中添加添加app获取相机权限及BarcodeScanner activity

<!--获取相机权限-->
<uses-permission android:name="android.permission.CAMERA" />
  
<activity android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/>
  • 编辑android/build.gradle文件
buildscript {
    ext.kotlin_version = '1.3.0'
    ...
    dependencies {
        ...
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
...
  • 编辑android/app/build.gradle文件(注意  apply plugin: 'kotlin-android'  需要放在  apply plugin: 'com.android.application'  之后)
apply plugin: 'kotlin-android'
...
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    ...
}
  • 此时可以试着运行一下,如果报下面的错误,则继续配置

错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:preDebugBuild'.
> Android dependency 'androidx.core:core' has different version for the compile (1.0.0) and runtime (1.0.1) classpath. You should manually set the same version via DependencyResolution

解决方法:

1.android/gradle/wrapper/gradle-wrapper.properties里面
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

2.android/build.gradle
dependencies { classpath 'com.android.tools.build:gradle:3.3.0' }

3.android/gradle.properties
加入
android.enableJetifier=true
android.useAndroidX=true

4.android/app/build.gradle 修改版本号:
 make sure compileSdkVersion and targetSdkVersion are at least 28.
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

5.android/app/build.gradle /defaultConfig加上
multiDexEnabled true

 

  • IOS配置

  • 在Info.plist中添加一下内容
<key>NSCameraUsageDescription</key>
<string>Camera permission is required for barcode scanning.</string>

 

  • 使用

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> {
  String 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.photo_camera),
          onPressed: _scan,
        ),
        appBar: AppBar(
          title: Text("扫码"),
        ),
        body: Text("${barcode}")
    );
  }
}

 

  • 补充一个无厘头的大坑:

问题:

使用出现activity是否有注册到清单文件问题

(如:android.content.ActivityNotFoundException: Unable to find explicit activity class {com.met.metchain2/com.apptreesoftware.barcodescan.BarcodeScannerActivity}; have you declared this activity in your AndroidManifest.xml?)

解决方法:

这还是一个比较无厘头的问题,直接粗暴的方法,将项目下的flutter_app/android/.gradle文件夹直接删除,重新编译,即可以解决。

posted @ 2019-12-25 14:07  苦瓜爆炒牛肉  阅读(5595)  评论(0编辑  收藏  举报