欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

让 Android 与华为扫码库对接,达成类似微信级别的扫码体验

最编程 2024-02-22 08:13:04
...

上一篇文章我们介绍了如何接入微信开源的扫码库,来提升扫码识别率和扫码速度。
但是由于接入微信扫码库,需要用到OpenCV和JNI相关的一些知识,所以还是有一定门槛的。

这篇文章,我们来接入华为的扫码库,相比接入微信扫码库要简单很多,正对扫码弱光、弯曲等场景下作了特殊的优化,在某些实际扫码场景上,比微信还要强 !

华为扫码库有提供两种SDK,可以根据需求选择合适的
在这里插入图片描述

特别要注意的是
如果在非华为手机使用多码能力接口,请使用Scan SDK-Plus,否则会影响识别。

具体接入请看 华为统一扫码服务 官方文档 ,扫码的效果真的很棒,建议实际体验一下。

华为的扫码库接一下是很简单的,但是它只是一个扫码解析库,想要自定义扫码界面的UI,还需要自己搞一套,为了解决这个问题,我创建了一个第三方库,将将 BGAQRCode-AndroidZXingLite 中的扫码UI独立抽离取来,使其能独立使用,从而可以对接上华为的扫码库。

添加依赖

allprojects {
	repositories {
		...
		maven { url 'https://www.jitpack.io' }
	}
}
dependencies {
    implementation 'com.github.EthanCo:QrCodeScanUI:v1.0.0'
}

如何使用
有两个扫码UI类可以选择,ViewfinderView或ScanBoxView,这两个类都可以实现相似的效果,可自行进行选择。
ViewfinderView

<com.heiko.scan.ViewfinderView
        android:id="@+id/viewfinderView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cornerColor="@color/colorYellow"
        app:cornerRectHeight="25dp"
        app:cornerRectWidth="5dp"
        app:frameColor="@color/transparent"
        app:frameGravity="center"
        app:frameHeight="@dimen/scan_rect_width"
        app:framePaddingBottom="10dp"
        app:frameWidth="@dimen/scan_rect_width"
        app:gridHeight="0dp"
        app:labelText=""
        app:labelTextLocation="bottom"
        app:labelTextSize="14sp"
        app:labelTextWidth="180dp"
        app:laserColor="@color/colorYellow"
        app:laserStyle="line"
        app:maskColor="#7F000000" />

ScanBoxView

<com.heiko.scan.ScanBoxView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        app:qrcv_animTime="1000"
        app:qrcv_borderColor="@android:color/white"
        app:qrcv_borderSize="1dp"
        app:qrcv_cornerColor="@color/colorYellow"
        app:qrcv_cornerLength="60dp"
        app:qrcv_customScanLineDrawable="@mipmap/icon_scan_line"
        app:qrcv_isOnlyDecodeScanBoxArea="false"
        app:qrcv_isTipTextBelowRect="true"
        app:qrcv_maskColor="#A1000000"
        app:qrcv_qrCodeTipText="请对准二维码扫描"
        app:qrcv_rectWidth="225dp"
        app:qrcv_scanLineMargin="8dp"
        app:qrcv_tipTextSize="13sp"
        app:qrcv_topOffset="130dp" />

效果如图所示
在这里插入图片描述

具体可以看我的Github QrCodeScanUI