我發佈的第二個Flutter插件-Flutter二維碼掃描(支持文件、url、內存、相機)

介紹

二維碼做爲信息的載體,普遍應用於咱們生活的方方面面,例如:使用支付寶支付,二維碼加好友,二維碼推送等等,能舉例的例子多不勝數,而若是你的應用支持二維碼的掃描,用戶和體驗將會翻倍的增加,若是你是應用的開發者,歡迎來使用此二維碼掃描插件!並但願能給予項目一個star,謝謝!項目地址:github.com/rhymelph/r_…android

使用

你能夠在pub.dev網站上面搜索r_scan便可找到該插件,添加下面代碼到pubspec.yaml文件git

dependencies:
 r_scan: last version
複製代碼
  • last version 能夠在pub.dev網站搜索r_scan獲得

各設備註意事項

  • Android平臺下

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;
  }
複製代碼
  • IOS平臺下

須要在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';
複製代碼

1.掃描文件圖片二維碼

final result=await RScan.scanImagePath('你的文件路徑');
if(result.isNotEmpty){
    //result 爲二維碼內容
}
複製代碼

2.掃描圖片連接二維碼

final result=await RScan.scanImagePath('你的圖片連接');
if(result.isNotEmpty){
    //result 爲二維碼內容
}
複製代碼

3.掃描內存圖片二維碼

ByteData data=await rootBundle.load('images/qrCode.png');
final result=await RScan.scanImageMemory(data.buffer.asUint8List());
if(result.isNotEmpty){
    //result 爲二維碼內容
}
複製代碼

4.使用相機掃描二維碼

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;
  }
}
複製代碼

後續開發

  1. 支持手電筒
  2. 支持自定義區域觸發掃描
  3. 還有更多...
相關文章
相關標籤/搜索