Flutter - 生成二维码与识别二维码
生活随笔
收集整理的這篇文章主要介紹了
Flutter - 生成二维码与识别二维码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#生成二維碼
##首先需要在pubspec.yaml:中添加
qr_flutter: ^1.1.3
其次,引入代碼:
import 'package:qr_flutter/qr_flutter.dart';
核心代碼如下:
child: QrImage(data: "這里是需要生成二維碼的數據",size: 100.0,onError: (ex) {print("[QR] ERROR - $ex");}, 復制代碼整體代碼與效果如下:
#識別二維碼
這里也用第三方的包在pubspec.yaml中添加庫鏈接;
barcode_scan: ^0.0.4
識別的主要代碼如下:
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');}} 復制代碼整體代碼如下:
import 'package:flutter/material.dart'; import 'package:barcode_scan/barcode_scan.dart'; import 'dart:async'; import 'package:flutter/services.dart';class scanqr extends StatelessWidget {@overrideWidget build(BuildContext context) {// TODO: implement buildreturn MaterialApp(home: sacnBody(),);} }class sacnBody extends StatefulWidget { @overrideState<StatefulWidget> createState() {// TODO: implement createStatereturn _MyScanState();} }class _MyScanState extends State<sacnBody> {String barcode = "";@overridevoid initState() {// TODO: implement initStatesuper.initState();}@overrideWidget build(BuildContext context) {// TODO: implement buildreturn Scaffold(appBar: new AppBar(title: new Text('QR Code'),),body: new Center(child: new Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.stretch,children: <Widget>[Padding(padding: EdgeInsets.symmetric(horizontal: 15.0,vertical: 8.0),child: RaisedButton(color: Colors.orange,textColor: Colors.white,splashColor: Colors.blueGrey,onPressed: scan,child: const Text('START CAMERA SCAN')),),Padding (padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),child: Text(barcode, textAlign: TextAlign.center,),)],),),);}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');}}} 復制代碼安卓和iOS工程設置如下:
Android For Android, you must do the following before you can use the plugin:
Add the camera permission to your AndroidManifest.xml<uses-permission android:name="android.permission.CAMERA" />Add the Barcode activity to your AndroidManifest.xml<activity android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/> 復制代碼iOS To use on iOS, you must add the the camera usage description to your Info.plist
<key>NSCameraUsageDescription</key> <string>Camera permission is required for barcode scanning.</string> 復制代碼總結
以上是生活随笔為你收集整理的Flutter - 生成二维码与识别二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单片机c语言必背100代码,单片机C语言
- 下一篇: 1668智能下数教程视频_你需要的教程合