春节不停更,此文正在参加「星光计划-春节更帖活动」
作者:庄茂裕
前言:
项目开发中有需要用到扫码识别的功能,这个功能在很多项目中都应该会使用到,于是找到了Zxing-Embedded的鸿蒙版,相信有安卓开发经验的同学都知道这个Zxing-Embedded框架,下面就开始使用
一、ZXing Embedded简介及使用方法
下载地址:https://gitee.com/baijuncheng-open-source/zxing-embedded
安装:
方案一:本地源集成,用户可自定义修改
1.复制zxing_embedded文件夹到project目录;
2.修改设置:在settings.Gradle文件下添加依赖项到这个模块,如下所示
3.例如,我们需要修改build.Gradle文件的入口模块下添加依赖项:
方案二:线上HAR包集成(这种方便,不能修改jar包代码)
在本地项目的build.gradled 的 dependencies 下添加
然后同步一下sync project whit Gradle files 成功后即安装完毕 (基本上大部分框架都是这样集成)
基本使用:
创建scanner.xml文件,如下
提醒:导入任何自定义文件或者第三方库的时候必须加
DecoratedBarcodeViewdde的属性简介:
参数 |
描述 |
counterclockwise |
可选。规定应该逆时针还是顺时针绘图。False = 顺时针,true = 逆时针。 |
app:zxing_preview_scaling_strategy |
预览视图的缩放策略,使用centerCrop即可 |
app:zxing_use_texture_view |
是否使用纹理视图(黑色背景) |
创建 cusstom_barcode_scammer.xml文件
创建 scannerAbility文件
扫码识别成功后会返回上个页面,扫码成功后的回调也会在 onAbilityResult里面返回。我这里用到RxBus.getDefault().send方法传递消息,是由于项目用了fraction来承载。
效果下图:

自定义扫描界面
由于Zxing框架的给扫描视图不太符合需求,因此要修改扫描的样式:
需要自定义View(继承ViewfinderView),重写onDraw方法,然后替换掉这里的ViewfinderView。
因为R.layout.zxing_barcode_scanner是源码中的布局文件,无法直接修改,所以还要重写一份布局文件给DecoratedBarcodeView加载,代码如下:
public class CustomViewfinderView extends ViewfinderView {
public static final long CUSTOME_ANIMATION_DELAY = 16;
public float mLineRate = 0.1F;
public float mLineDepth = 4;
public Color mLineColor = new Color(Color.getIntColor("#1EBB81"));
public CustomViewfinderView(Context context, AttrSet attrSet) {
super(context, attrSet);
}
@Override
public void onDraw(Component component, Canvas canvas) {
super.onDraw(component, canvas);
refreshSizes();
if (framingRect == null || cameraPreview.getPreviewFramingRect() == null) {
return;
}
Rect frame = framingRect;
Rect previewFrame = cameraPreview.getPreviewFramingRect();
int width = canvas.getLocalClipBounds().getWidth();
int height = canvas.getLocalClipBounds().getHeight();
paint.setColor(mLineColor);
canvas.drawRect(frame.left, frame.top, frame.left + frame.getWidth() * mLineRate, frame.top + mLineDepth, paint);
canvas.drawRect(frame.left, frame.top, frame.left + mLineDepth, frame.top + frame.getHeight() * mLineRate, paint);
canvas.drawRect(frame.right - frame.getWidth() * mLineRate, frame.top, frame.right, frame.top + mLineDepth, paint);
canvas.drawRect(frame.right - mLineDepth, frame.top, frame.right, frame.top + frame.getHeight() * mLineRate, paint);
canvas.drawRect(frame.left, frame.bottom - mLineDepth, frame.left + frame.getWidth() * mLineRate, frame.bottom, paint);
canvas.drawRect(frame.left, frame.bottom - frame.getHeight() * mLineRate, frame.left + mLineDepth, frame.bottom, paint);
canvas.drawRect(frame.right - frame.getWidth() * mLineRate, frame.bottom - mLineDepth, frame.right, frame.bottom, paint);
canvas.drawRect(frame.right - mLineDepth, frame.bottom - frame.getHeight() * mLineRate, frame.right, frame.bottom, paint);
Color color = new Color(resultBitmap != null ? resultColor : maskColor);
paint.setColor(color);
canvas.drawRect(0, 0, width, frame.top, paint);
canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
canvas.drawRect(0, frame.bottom + 1, width, height, paint);
}
}
}
- 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.
代码简介:
(1)onDraw方法中的大部分代码Copy自ViewfinderView,笔者添加部分逻辑:是边角线的绘制。
(2)代码的核心是在onDraw方法的第5行代码:
Rect frame = framingRect;
这个矩阵记录了扫描框四个顶点的坐标,有了这个变量,各位可以发挥想象力自定义自己需要的扫描样式。
接下来,我们用CustomViewfinderView替换掉ViewfinderView(如下所示)
最后,运行结果如下(如图所示):

总结
本文简述了zxing-Embeded的鸿蒙版使用,这个库由于现在没有人维护了,有些还需要修复的,闪光灯是用不了的,自定义的扫描样式本文只是提供了一些思路,还有其他的样式修改,也可根据自定义这种方式修改,后期会继续进一步分享优化此框架的问题,欢迎各位鸿蒙开发者一起讨论与研究,共勉。
入门到精通、技巧到案例,系统化分享HarmonyOS开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
感谢大佬分享使用方法,也希望来个大佬维护一下