二维码的基础使用一(扫码)

依赖:

implementation 'com.journeyapps:zxing-android-embedded:4.0.0'

基于语言: kotlin 

 

扫码自定义界面:

二维码的基础使用一(扫码)_第1张图片





    



    

    

class MyScanActivity : CaptureActivity() {


    override fun initializeContent(): DecoratedBarcodeView {
        setContentView(com.zhouzi.zhaoyang.R.layout.scan_activity)

        tvApBtn.setOnClickListener {
            finish()
        }

        return dbvCustom
    }




}

点击扫码:自定义扫码界面    MyScanActivity

IntentIntegrator(activity)
    .setCaptureActivity(MyScanActivity().javaClass)
    .setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES)// 扫码的类型,可选:一维码,二维码,一/二维码
    .setPrompt("请对准二维码")// 设置提示语
    .setCameraId(0)// 选择摄像头,可使用前置或者后置
    .setBeepEnabled(false)// 是否开启声音,扫完码之后会"哔"的一声
    .setBarcodeImageEnabled(true)// 扫完码之后生成二维码的图片
    .initiateScan()// 初始化扫码

扫码回调:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    var result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)

    if (result != null) {

        if (result.getContents() == null) {
            val intent = Intent(mContext, VoiceService::class.java)
            intent.putExtra("voiceStr", "扫码失败")
            startService(intent)
        } else {
           // 扫码成功,do your logic....
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

}

 

你可能感兴趣的:(第三方库)