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

 

上一篇:使用ZXing库的首选开发人员Android手机


下一篇:vue条形码+二维码