从0开始搭建Flutter条形码二维码扫描插件及应用

您所在的位置:网站首页 flutter扫码插件 从0开始搭建Flutter条形码二维码扫描插件及应用

从0开始搭建Flutter条形码二维码扫描插件及应用

2024-01-12 13:49| 来源: 网络整理| 查看: 265

Google今年推出了Flutter 2。一套Dart代码可以覆盖桌面,web和移动开发,大大降低跨平台应用开发成本。然而平台相关的逻辑还是需要用相应的本地代码去实现。这篇文章分享如何从0开始,搭建发布Flutter的条形码二维码扫描插件,以及如何使用该插件来实现一个Android的扫码应用。

Flutter安装

参考Flutter中国区安装指南:https://flutter.dev/community/china

Linux, mac配置环境变量:

export PUB_HOSTED_URL=https://pub.flutter-io.cn export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

Windows在系统设置里添加PUB_HOSTED_URL和FLUTTER_STORAGE_BASE_URL。

Flutter条形码,二维码SDK插件搭建及发布

创建对应平台的插件模板。这里使用安卓:

flutter create --org com.dynamsoft --template=plugin --platforms=android -a java flutter_barcode_sdk

后面要添加iOS可以在当前工程目录里运行:

flutter create --template=plugin --platforms=ios . 开发Flutter插件 Dart代码

打开自动生成的lib/flutter_barcode_sdk.dart文件。这里面定义插件的API。

创建一个BarcodeResult类,用于反序列化Android Java返回的结果数据:

class BarcodeResult { final String format; final String text; final int x1; final int y1; final int x2; final int y2; final int x3; final int y3; final int x4; final int y4; BarcodeResult(this.format, this.text, this.x1, this.y1, this.x2, this.y2, this.x3, this.y3, this.x4, this.y4); BarcodeResult.fromJson(Map json) : format = json['format'], text = json['text'], x1 = json['x1'], y1 = json['y1'], x2 = json['x2'], y2 = json['y2'], x3 = json['x3'], y3 = json['y3'], x4 = json['x4'], y4 = json['y4']; Map toJson() => { 'format': format, 'text': text, 'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2, 'x3': x3, 'y3': y3, 'x4': x4, 'y4': y4, }; }

创建两个接口。decodeFile()用于文件解码,decodeImageBuffer()用于图像数据解码。

List _convertResults(List ret) { return ret.map((data) => BarcodeResult.fromJson(data)).toList(); } Future decodeFile(String filename) async { List ret = List.from( await _channel.invokeMethod('decodeFile', { 'filename': filename})); return _convertResults(ret); } Future decodeImageBuffer( Uint8List bytes, int width, int height, int stride


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3