• Stars
    star
    118
  • Rank 299,923 (Top 6 %)
  • Language
    Java
  • License
    BSD 3-Clause "New...
  • Created about 5 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📷🖨Flutter二维码&条形码扫描插件,支持相机、文件、链接、Uint8List类型扫描

r_scan

pub package

A flutter plugin about qr code or bar code scan , it can scan from file、url、memory and camera qr code or bar code .Welcome to feedback your issue.

Getting Started

Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  r_scan: last version

Android Platform

require read storage permission and camera permission, use permission_handler plugin.

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 Platform

add the permissions in your Info.plist

    <key>NSCameraUsageDescription</key>
	<string>扫描二维码时需要使用您的相机</string>
	<key>NSPhotoLibraryUsageDescription</key>
	<string>扫描二维码时需要访问您的相册</string>
	<key>io.flutter.embedded_views_preview</key>
    <true/>

no another.

Usage

1.scan Image File

final result=await RScan.scanImagePath('your file path');

2.scan Image url

final result=await RScan.scanImagePath('your image url');

3.scan Image memory

 ByteData data=await rootBundle.load('images/qrCode.png');
 final result=await RScan.scanImageMemory(data.buffer.asUint8List());

4.scan camera(new! please upgrade this plugin to v0.1,4)

  • Step First: Get available cameras
List<RScanCameraDescription> rScanCameras = await availableRScanCameras();;

if you want to get it in main() method, you can use this code.

List<RScanCameraDescription> rScanCameras;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  rScanCameras = await availableRScanCameras();
  runApp(...);
}
  • Step Second:Use it.
class RScanCameraDialog extends StatefulWidget {
  @override
  _RScanCameraDialogState createState() => _RScanCameraDialogState();
}

class _RScanCameraDialogState extends State<RScanCameraDialog> {
  RScanCameraController _controller;
  bool isFirst = true;

  @override
  void initState() {
    super.initState();
    if (rScanCameras != null && rScanCameras.length > 0) {
      _controller = RScanCameraController(
          rScanCameras[1], RScanCameraResolutionPreset.max)
        ..addListener(() {
          final result = _controller.result;
          if (result != null) {
            if (isFirst) {
              Navigator.of(context).pop(result);
              isFirst = false;
            }
          }
        })
        ..initialize().then((_) {
          if (!mounted) {
            return;
          }
          setState(() {});
        });
    }
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (rScanCameras == null || rScanCameras.length == 0) {
      return Scaffold(
        body: Container(
          alignment: Alignment.center,
          child: Text('not have available camera'),
        ),
      );
    }
    if (!_controller.value.isInitialized) {
      return Container();
    }
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        children: <Widget>[
          ScanImageView(
            child: AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: RScanCamera(_controller),
            ),
          ),
          Align(
              alignment: Alignment.bottomCenter,
              child: FutureBuilder(
                future: getFlashMode(),
                builder: _buildFlashBtn,
              ))
        ],
      ),
    );
  }
  Future<bool> getFlashMode() async {
    bool isOpen = false;
    try {
      isOpen = await _controller.getFlashMode();
    } catch (_) {}
    return isOpen;
  }

  Widget _buildFlashBtn(BuildContext context, AsyncSnapshot<bool> snapshot) {
    return snapshot.hasData
        ? Padding(
      padding:  EdgeInsets.only(bottom:24+MediaQuery.of(context).padding.bottom),
      child: IconButton(
          icon: Icon(snapshot.data ? Icons.flash_on : Icons.flash_off),
          color: Colors.white,
          iconSize: 46,
          onPressed: () {
            if (snapshot.data) {
              _controller.setFlashMode(false);
            } else {
              _controller.setFlashMode(true);
            }
            setState(() {});
          }),
    )
        : Container();
  }
}

5.scan view(Deprecated)

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
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @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;
  }
}

6. open flash lamp / get flash lamp status.

You can use RScanController class.

//turn off the flash lamp.
await _controller.setFlashMode(false);

//turn on the flash lamp.
await _controller.setFlashMode(true);

// get the flash lamp status.

bool isOpen = await _controller.getFlashMode();

7. RScanResult

when you scan finish,will return the RScanResult...

class RScanResult {
  /// barcode type
  final RScanBarType type;

  ///barcode message
  final String message;

  ///barcode points include [x , y]
  final List<RScanPoint> points;
}

More Repositories

1

r_upgrade

🆙🚀 Flutter application upgrade/ Flutter App Upgrade /Flutter App Update/Flutter Update / download Plug-in (with notice bar progress), supports full upgrade, hot update and incremental upgrade
Java
421
star
2

flutter_douban

豆瓣Flutter版
Dart
52
star
3

r_calendar

📅📆Flutter日历插件
Dart
41
star
4

FlutterByRhyme

a Flutter Course app
Dart
29
star
5

r_dotted_line_border

一个关于虚线边框的Flutter纯Dart语言插件包.
Dart
16
star
6

r_router

✌️📃A Flutter router package,you can not need use context to navigate,and support dialog.
Dart
10
star
7

fluct

A command-lint tool for help develop flutter application.
Dart
10
star
8

aqueduct_base

Dart服务端Aqueduct框架开发,基础项目
Dart
8
star
9

r_album

Flutter保存图片和视频到相册,支持Android和IOS平台
Dart
7
star
10

r_get_ip

🆕👀超级简单获取手机IP地址,外网地址,当前连接网络类型,支持Android/IOS/MacOS/Web/Windows
Swift
4
star
11

MaterialDemo

Material Design Demo
Java
4
star
12

r_barcode

Flutter Camera Scan QR Code OR BAR Code Awesome Plugin.
C++
4
star
13

code_generator_demo

dart编写代码生成器示例代码
Dart
3
star
14

abc_plugin

Flutter农行支付插件
Dart
2
star
15

r_upgrade_ui

Dart
2
star
16

bugly_plugin

bugly插件,只支持异常捕获
Dart
2
star
17

r_forbidshot

Flutter截屏,录屏屏蔽插件
Dart
2
star
18

ForeverViewPager

forever 无限循环轮播图非Integer.MAX_VALUE实现,打造简单易用的轮播图
Java
2
star
19

flutter_calculator

➕➖✖➗mac calculator
JavaScript
2
star
20

rhy_basis

My Flutter Basis Plugin.
Dart
1
star
21

r_citychoice

🏢🏙Flutter 省-城市-区域-街道 选择
Dart
1
star
22

r_launch_app

download app to app store
Dart
1
star
23

r_marquee_text

Flutter使用Painter实现跑马灯效果,支持↑↓↔
Dart
1
star
24

r_gps

Android GPS 是否打开,GPS跳转到打开页面
Java
1
star
25

fly_game

Flame 蚊子🦟游戏
Dart
1
star
26

r_screenshot

✂📱screenshot and long screenshot flutter plugin.
Dart
1
star
27

r_angular_blob

使用Angular Dart编写的博客网站 - by rhyme_lph
HTML
1
star
28

practical_jump

Flutter插件实用跳转
Java
1
star
29

simple_page_indicator

Page Indicator
Dart
1
star