How to scan QRCode in android

前端 未结 7 2298
说谎
说谎 2020-12-25 14:04

I found a tutorial on how to scan a barcode. But in my application I have to scan a QR code. How can I a scan QR code in Android?

7条回答
  •  自闭症患者
    2020-12-25 14:40

    You can scan QR code easily with zxing add the following dependencies in your gradle

    compile 'com.journeyapps:zxing-android-embedded:3.1.0@aar'
    compile 'com.google.zxing:core:3.2.0'
    

    Then in your Activity or on Fragment

       IntentIntegrator scanIntegrator = new IntentIntegrator(context);
                    scanIntegrator.setPrompt("Scan");
                    scanIntegrator.setBeepEnabled(true);
                    //The following line if you want QR code
                    scanIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                    scanIntegrator.setCaptureActivity(CaptureActivityAnyOrientation.class);
                    scanIntegrator.setOrientationLocked(true);
                    scanIntegrator.setBarcodeImageEnabled(true);
                    scanIntegrator.initiateScan();
    

    And then capture the result in onActivityResult

       @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (scanningResult != null) {
                if (scanningResult.getContents() != null) {
                    scanContent = scanningResult.getContents().toString();
                    scanFormat = scanningResult.getFormatName().toString();
                }
    
                Toast.makeText(this,scanContent+"   type:"+scanFormat,Toast.LENGTH_SHORT).show();
    
            }else{
                Toast.makeText(this,"Nothing scanned",Toast.LENGTH_SHORT).show();
            }
        }
    

    Take a look at this sample project , hope it helps you .

提交回复
热议问题