https://www.jianshu.com/p/af63c25eeafe
在前两篇文章中,我们讲述了二维码的生成、保存和分享的内容,本次我们对于二维码的扫描的内容进行说明,实现一个简易的Android扫一扫功能。
系列文章:
Android二维码的基础使用(一):利用Zxing生成自定义二维码
Android二维码的基础使用(二):保存和分享
Android二维码的基础使用(三):二维码扫描Demo实现
一、lib库说明
扫描二维码同样需要引入zxing库,但是由于原生的zxing库使用在自定义特性上支持一般,于是在此我选择引入了辅助库:
implementation ‘com.king.zxing:zxing-lite:1.1.9-androidx’
zxing-lite库实际上是一个Zxing的精简辅助库,对于扫码功能进行了优化,同时内置了闪光灯等功能,对于代码集成上也是更加简单。
其GitHub地址如下:
zxing-lite Github地址
一、布局实现
首先我们先添加一个布局activity_q_r_scanner.xml,包括有扫码框,打开闪光灯的开关,代码如下:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<SurfaceView
android:id="@+id/sv_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.king.zxing.V