og

基于zxing撸一个flutter二维码扫描插件(android)

第一步不用多说先创建一个flutter plugin项目

https://hooyim.oss-accelerate.aliyuncs.com/uploads%2F2020%2F07%2F29%2FDdUA531QcpJXAlca

去git上下载zxing最新的jar包和资源文件和相关类代码导入到项目中

https://hooyim.oss-accelerate.aliyuncs.com/uploads%2F2020%2F07%2F29%2Ff7H0Ys4pDcyiSqNA

接下来在android端你的包名的文件夹下创建扫描delegate

直接上代码,加入了权限判断

        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
package com.yh.flutter_qrscaner; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import androidx.core.app.ActivityCompat; import com.zxing.android.CaptureActivity; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.PluginRegistry; public class ScanerDelegate implements PluginRegistry.ActivityResultListener, PluginRegistry.RequestPermissionsResultListener { static final int REQUEST_CODE_STAR_SCAN = 2000; private final Activity activity; private MethodChannel.Result result; public ScanerDelegate(final Activity activity) { this.activity = activity; } public boolean isPermissionGranted(String permissionName) { return ActivityCompat.checkSelfPermission(activity, permissionName) == PackageManager.PERMISSION_GRANTED; } public void askForPermission(String[] permissionNames, int requestCode) { ActivityCompat.requestPermissions(activity, permissionNames, requestCode); } public void StartScan(MethodChannel.Result result) { this.result = result; if (!isPermissionGranted(Manifest.permission.CAMERA)) { askForPermission(new String[]{Manifest.permission.CAMERA} , REQUEST_CODE_STAR_SCAN); return; } scanCore(); } private void scanCore() { Intent intent = new Intent(activity, CaptureActivity.class); activity.startActivityForResult(intent, REQUEST_CODE_STAR_SCAN); } @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_STAR_SCAN) { if (data!=null){ String content = data.getStringExtra("codedContent"); result.success(content); }else{ result.success(""); } return true; } else { return false; } } @Override public boolean onRequestPermissionsResult( int requestCode, String[] permissions, int[] grantResults) { boolean permissionGranted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; if (requestCode == REQUEST_CODE_STAR_SCAN) { if (permissionGranted) { scanCore(); } } else { return false; } if (!permissionGranted) { result.success(null); } return true; } }

实现插件类,暴露出扫描方法供外部调用

直接上代码

        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
package com.yh.flutter_qrscaner; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; /** * FlutterQrscanerPlugin */ public class FlutterQrscanerPlugin implements MethodCallHandler { private final ScanerDelegate delegate; /** * Plugin registration. */ public static void registerWith(Registrar registrar) { final ScanerDelegate delegate = new ScanerDelegate(registrar.activity()); registrar.addActivityResultListener(delegate); registrar.addRequestPermissionsResultListener(delegate); final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_qrscaner"); channel.setMethodCallHandler(new FlutterQrscanerPlugin(delegate)); } FlutterQrscanerPlugin(ScanerDelegate delegate) { this.delegate = delegate; } @Override public void onMethodCall(MethodCall call, Result result) { if (call.method.equals("startScan")) { delegate.StartScan(result); } else { result.notImplemented(); } } }

android端的代码完成后,开始编写flutter端的插件代码

暴露出扫码方法供插件使用者使用

        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
import 'dart:async'; import 'package:flutter/services.dart'; class FlutterQrscaner { static const MethodChannel _channel = const MethodChannel('flutter_qrscaner'); static Future<String> startScan() => _channel .invokeMethod("startScan") .then<String>((dynamic result) => result); }

使用

直接上代码

        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
import 'package:flutter/material.dart'; import 'package:flutter_qrscaner/flutter_qrscaner.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { String _qrContent = ""; @override void initState() { super.initState(); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Spacer(), RaisedButton( onPressed: () { FlutterQrscaner.startScan().then((value) { setState(() { _qrContent = value; }); }); }, child: Text('开始扫码')), Spacer(), Text('扫码结果:$_qrContent'), Spacer() ], ), ), ), ); } }

效果

https://hooyim.oss-accelerate.aliyuncs.com/uploads%2F2020%2F07%2F29%2FexfvyKpDingz5CG5

注意点

使用时需要在android端添加相机的权限

        
  • 1
<uses-permission android:name="android.permission.CAMERA" />

Article created at   2020/7/29 PM  in category  code ,   154  Views

Related tags: flutter

Article Address: https://www.hooyim.com/article/5