鸿蒙开源三方组件--code-scanner 原创 精华
mb60ffc367523f3
发布于 2021-7-27 18:13
浏览
2收藏
1.介绍
这个三方库主要实现了相机的一些功能和条形码扫描的功能。其中,
相机的功能有:
- 自动对焦和闪光灯控制
- 前后摄像头
- 可定制取景器
- 触摸对焦
条形码扫描:(该功能调用三方库zxing实现)
- 一维码扫描
- 二维码扫描
条形码扫描支持的格式如下:
1D product | 1D industrial | 2D |
---|---|---|
UPC-A | Code 39 | QR Code |
UPC-E | Code 93 | Data Matrix |
EAN-8 | Code 128 | Aztec |
EAN-13 | Codabar | PDF 417 |
ITF | MaxiCode | |
RSS-14 | ||
RSS-Expanded |
2.依赖
(1). 在工程的build.gradle的allprojects中,添加HAR所在的Maven仓地址:
repositories {
maven {
url 'https://s01.oss.sonatype.org/content/repositories/releases/'
}
}
(2).在应用模块的build.gradle的dependencies闭包中,添加如下代码:
dependencies {
implementation ' io.github.dzsf:code-scanner:1.0.1 '
}
3.使用说明
(1).将相机权限添加到config.json(不要忘记动态权限):
{
"reqPermissions": [
{
"name": "ohos.permission.CAMERA",
"reason": "Need camera permission",
"usedScene": {
"ability": [
"com.budiyev.ohos.libdemoapp.codescanner.MainAbility",
"com.budiyev.ohos.libdemoapp.codescanner.CodeScannerAbility"
],
"when": "always"
}
}
]
}
(2).在layout文件中定义视图:
<?xml version="1.0" encoding="utf-8"?>
<StackLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
xmlns:app="http://schemas.huawei.com/res/ohos-auto"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:id="$+id:stack_layout">
<com.budiyev.ohos.codescanner.CodeScannerView
ohos:id="$+id:scanner_view"
ohos:height="match_parent"
ohos:width="match_parent"
app:autoFocusButtonColor="#FFFFFFFF"
app:autoFocusButtonVisible="true"
app:flashButtonColor="#FFFFFFFF"
app:flashButtonVisible="true"
app:frameColor="#FFFFFFFF"
app:frameCornersSize="50"
app:frameCornersRadius="0"
app:frameAspectRatioWidth="1"
app:frameAspectRatioHeight="1"
app:portraitFrameAspectRatioWidth="16"
app:portraitFrameAspectRatioHeight="9"
app:frameSize="0.75"
app:frameThickness="2"
app:maskColor="#77000000"/>
</StackLayout>
(3).将以下代码添加至自己的Ability和AbilitySlice中:
public class CodeScannerAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(CodeScannerSlice.class.getName());
}
}
public class CodeScannerSlice extends AbilitySlice {
private CodeScanner mCodeScanner;
private CodeScannerView mCodeScannerView;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_code_scanner);
mCodeScannerView = (CodeScannerView) findComponentById(ResourceTable.Id_scanner_view);
mCodeScanner = new CodeScanner(this, mCodeScannerView);
mCodeScanner.setDecodeCallback(result -> getUITaskDispatcher().syncDispatch(() ->
new CommonDialog(getContext())
.setTitleText("Scan result")
.setTitleSubText("Format: " + result.getBarcodeFormat())
.setContentText(result.getText())
.setAlignment(LayoutAlignment.CENTER)
.show()));
mCodeScanner.setErrorCallback(error -> getUITaskDispatcher().syncDispatch(() ->
new ToastDialog(getContext())
.setText("Error:" + error)
.setAlignment(LayoutAlignment.CENTER)
.show()));
}
@Override
public void onActive() {
super.onActive();
mCodeScanner.startPreview();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
public void onBackground() {
super.onBackground();
}
public void onStop() {
mCodeScanner.stopPreview();
WindowManager.getInstance().getTopWindow().get().setTransparent(false);
super.onStop();
}
@Override
public void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
super.onOrientationChanged(displayOrientation);
mCodeScannerView.setDisplayOrientation(displayOrientation);
}
}
4.效果展示
5.相关资料
项目地址:https://gitee.com/openneusoft/code-scanner
IDE官方下载地址:https://developer.harmonyos.com/cn/develop/deveco-studio
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
赞
3
收藏 2
回复
相关推荐
扫二维码可以说是日常生活必不可少的一环了。
很实用