二維碼做爲信息的載體,普遍應用於咱們生活的方方面面,例如:使用支付寶支付,二維碼加好友,二維碼推送等等,能舉例的例子多不勝數,而若是你的應用支持二維碼的掃描,用戶和體驗將會翻倍的增加,若是你是應用的開發者,歡迎來使用此二維碼掃描插件!並但願能給予項目一個star,謝謝!項目地址:github.com/rhymelph/r_…android
你能夠在pub.dev網站上面搜索r_scan
便可找到該插件,添加下面代碼到pubspec.yaml
文件git
dependencies:
r_scan: last version
複製代碼
r_scan
獲得android6.0系統下請動態受權,能夠結合permission_handler
插件使用,代碼以下:github
import 'package:permission_handler/permission_handler.dart';
Future<bool> canReadStorage() async {
if(Platform.isIOS) return true;
var status = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.storage);
if (status != PermissionStatus.granted) {
var future = await PermissionHandler()
.requestPermissions([PermissionGroup.storage]);
for (final item in future.entries) {
if (item.value != PermissionStatus.granted) {
return false;
}
}
} else {
return true;
}
return true;
}
Future<bool> canOpenCamera() async {
var status =
await PermissionHandler().checkPermissionStatus(PermissionGroup.camera);
if (status != PermissionStatus.granted) {
var future = await PermissionHandler()
.requestPermissions([PermissionGroup.camera]);
for (final item in future.entries) {
if (item.value != PermissionStatus.granted) {
return false;
}
}
} else {
return true;
}
return true;
}
複製代碼
須要在info.plist
文件下添加以下代碼:async
<key>NSCameraUsageDescription</key>
<string>掃描二維碼時須要使用您的相機</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>掃描二維碼時須要訪問您的相冊</string>
<key>io.flutter.embedded_views_preview</key>
<true/>
複製代碼
import 'package:r_scan/r_scan.dart';
複製代碼
final result=await RScan.scanImagePath('你的文件路徑');
if(result.isNotEmpty){
//result 爲二維碼內容
}
複製代碼
final result=await RScan.scanImagePath('你的圖片連接');
if(result.isNotEmpty){
//result 爲二維碼內容
}
複製代碼
ByteData data=await rootBundle.load('images/qrCode.png');
final result=await RScan.scanImageMemory(data.buffer.asUint8List());
if(result.isNotEmpty){
//result 爲二維碼內容
}
複製代碼
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:r_scan/r_scan.dart';
class RScanDialog extends StatefulWidget {
@override
_RScanDialogState createState() => _RScanDialogState();
}
class _RScanDialogState extends State<RScanDialog> {
RScanController _controller;
@override
void initState() {
super.initState();
initController();
}
bool isFirst=true;
Future<void> initController() async {
_controller = RScanController();
_controller.addListener(() {//監聽掃描到的二維碼
final result = _controller.result;
if (result != null) {
if(isFirst){
Navigator.of(context).pop(result);
isFirst=false;
}
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: FutureBuilder<bool>(
future: canOpenCameraView(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData && snapshot.data == true) {
return ScanImageView(//這個爲本身寫的前景
child: RScanView(
controller: _controller,
),
);
} else {
return Container();
}
},
),
),
);
}
Future<bool> canOpenCameraView() async {
var status =
await PermissionHandler().checkPermissionStatus(PermissionGroup.camera);
if (status != PermissionStatus.granted) {
var future = await PermissionHandler()
.requestPermissions([PermissionGroup.camera]);
for (final item in future.entries) {
if (item.value != PermissionStatus.granted) {
return false;
}
}
} else {
return true;
}
return true;
}
}
複製代碼