lutter 调用原生硬件 Api 实现扫码

Posted zhaofeis

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于lutter 调用原生硬件 Api 实现扫码的主要内容,如果未能解决你的问题,请参考以下文章

ios7原生扫码

如何用JS调用几十万原生API

java实现微信支付之扫码支付

PHP原生代码写的微信扫码支付实例

如何用JS调用几十万原生API

原生js实现查询天气的小应用