Zxing est une excellente bibliothèque pour effectuer la numérisation et la génération de code Qr.
L'implémentation suivante utilise la bibliothèque Zxing pour scanner l'image du code QR N'oubliez pas d'ajouter la dépendance suivante dans le build.gradle
implementation 'me.dm7.barcodescanner:zxing:1.9'
Activité du scanner de code :
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();
}
}