BGAQRCode-Android实践

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'
}

添加控件


在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, "打开相机出错")
    }

别忘记申请权限

待续。。。

你可能感兴趣的:(BGAQRCode-Android实践)