Android, How to read QR code in my application?

前端 未结 7 1988
耶瑟儿~
耶瑟儿~ 2020-11-29 16:11

In my application I need to read Qr code. I searched the net and found Zing codes however lots of developers had problem with using it and it seems it is buggy!

If i

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 16:24

    Zxing is an excellent library to perform Qr code scanning and generation. The following implementation uses Zxing library to scan the QR code image Don't forget to add following dependency in the build.gradle

    implementation 'me.dm7.barcodescanner:zxing:1.9'
    

    Code scanner activity:

        public class QrCodeScanner extends AppCompatActivity implements ZXingScannerView.ResultHandler {
            private ZXingScannerView mScannerView;
    
            @Override
            public void onCreate(Bundle state) {
                super.onCreate(state);
                // Programmatically initialize the scanner view
                mScannerView = new ZXingScannerView(this);
                // Set the scanner view as the content view
                setContentView(mScannerView);
            }
    
            @Override
            public void onResume() {
                super.onResume();
                // Register ourselves as a handler for scan results.
                mScannerView.setResultHandler(this);
                // Start camera on resume
                mScannerView.startCamera();
            }
    
            @Override
            public void onPause() {
                super.onPause();
                // Stop camera on pause
                mScannerView.stopCamera();
            }
    
            @Override
            public void handleResult(Result rawResult) {
                // Do something with the result here
                // Prints scan results
                Logger.verbose("result", rawResult.getText());
                // Prints the scan format (qrcode, pdf417 etc.)
                Logger.verbose("result", rawResult.getBarcodeFormat().toString());
                //If you would like to resume scanning, call this method below:
                //mScannerView.resumeCameraPreview(this);
                Intent intent = new Intent();
                intent.putExtra(AppConstants.KEY_QR_CODE, rawResult.getText());
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    

提交回复
热议问题