Github地址:BGAQRCode-Android
ZXing
添加依赖
dependencies {
compile 'com.google.zxing:core:3.2.1'
compile 'cn.bingoogolapple:bga-qrcodecore:1.1.7@aar'
compile 'cn.bingoogolapple:bga-zxing:1.1.7@aar'
}
添加控件
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:id="@+id/mQRCodeView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:qrcv_animTime="1000"
app:qrcv_barCodeTipText="将条码放入框内,即可自动扫描"
app:qrcv_barcodeRectHeight="140dp"
app:qrcv_borderColor="@android:color/white"
app:qrcv_borderSize="1dp"
app:qrcv_cornerColor="@color/colorPrimaryDark"
app:qrcv_cornerLength="20dp"
app:qrcv_cornerSize="3dp"
app:qrcv_customScanLineDrawable="@mipmap/scan_icon_scanline"
app:qrcv_isBarcode="false"
app:qrcv_isCenterVertical="false"
app:qrcv_isOnlyDecodeScanBoxArea="false"
app:qrcv_isScanLineReverse="true"
app:qrcv_isShowDefaultGridScanLineDrawable="false"
app:qrcv_isShowDefaultScanLineDrawable="true"
app:qrcv_isShowTipBackground="true"
app:qrcv_isShowTipTextAsSingleLine="false"
app:qrcv_isTipTextBelowRect="false"
app:qrcv_maskColor="#33FFFFFF"
app:qrcv_qrCodeTipText="将二维码/条码放入框内,即可自动扫描"
app:qrcv_rectWidth="200dp"
app:qrcv_scanLineColor="@color/colorPrimaryDark"
app:qrcv_scanLineMargin="0dp"
app:qrcv_scanLineSize="0.5dp"
app:qrcv_tipTextColor="@android:color/white"
app:qrcv_tipTextSize="12sp"
app:qrcv_toolbarHeight="56dp"
app:qrcv_topOffset="90dp"/>
在onCreate中initQRView,并在activity的各个生命周期中加以处理即可。
private fun initQRView() {
mQRCodeView.setDelegate(this)
}
override fun onStart() {
super.onStart()
mQRCodeView.startCamera()
mQRCodeView.showScanRect()
}
override fun onStop() {
mQRCodeView.stopCamera()
super.onStop()
}
/**
* 设置震动
*/
private fun vibrate() {
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(200)
}
override fun onScanQRCodeSuccess(result: String?) {
Log.i(TAG, "result:" + result)
Toast.makeText(this, result, Toast.LENGTH_SHORT).show()
vibrate()
mQRCodeView.startSpot()
}
override fun onScanQRCodeOpenCameraError() {
Log.e(TAG, "打开相机出错")
}
别忘记申请权限
待续。。。