How to stop continuous scanning using zxing-android-embedded in Android

I am using Zxing-android-embedded ( https://github.com/journeyapps/zxing-android-embedded ) to scan QR codes. I imported the library from github. When the application starts, the camera scans the code several times while the camera is placed on the barcode. I want to stop the scan (but not the camera preview) after detecting the barcode and displaying a dialog box with the "Confirm", "Cancel" button and the input field. When the user clicks the Confirm or Cancel button, he must start scanning again.

I called barcodeView.pause(); at the beginning of the decode () method, which pauses the camera’s preview. In addition, barcodeView.resume(); inside the onClick method is "dialogConfirmClick" and "dialogCancelClick". But the barcodeView.pause(); method barcodeView.pause(); pauses scanning as well as camera preview.

Here is my class -

 public class MyScanActivity extends Activity { private static final String TAG = MyScanActivity.class.getSimpleName(); private CompoundBarcodeView barcodeView; private BeepManager beepManager; private DialogInterface.OnClickListener dialogCanselClick; private AlertDialog dialog; private BarcodeCallback callback = new BarcodeCallback() { @Override public void barcodeResult(BarcodeResult result) { if (result.getText() != null) { handleDecode(result); } } @Override public void possibleResultPoints(List<ResultPoint> resultPoints) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.continuous_scan); barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner); barcodeView.decodeContinuous(callback); beepManager = new BeepManager(this); dialogCancelClick = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { barcodeView.resume();//Resume scanning dialog.dismiss(); } }; } public void handleDecode(BarcodeResult rawResult) { barcodeView.pause();//Pause preview String result = rawResult.getText(); beepManager.playBeepSoundAndVibrate(); DialogInterface.OnClickListener dialogOkClick = new DialogInterface.OnClickListener() { // OK // button @Override public void onClick(DialogInterface dialog, int which) { if (writeNote) { EditText txtNote = (EditText) promptsView.findViewById(R.id.txt_dialog_note); //code to merge value of txtNote with result } dialog.dismiss(); barcodeView.resume();//Resume scanning after pressing confirm button Toast.makeText(MyScanActivity.this, R.string.dialog_save_qr_alert, Toast.LENGTH_SHORT).show(); } }; AlertDialog dialog = DialogHelper.CreateDialog(this, DialogHelper.SAVE_QR_CODE, result, dialogOkClick, dialogCancelClick, promptsView); dialog.show(); } @Override protected void onResume() { super.onResume(); barcodeView.resume(); } @Override protected void onPause() { super.onPause(); barcodeView.pause(); } public void pause(View view) { barcodeView.pause(); } public void resume(View view) { barcodeView.resume(); } public void triggerScan(View view) { barcodeView.decodeSingle(callback); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); } } 
+5
source share
1 answer

I have a solution. I am posting this so that it helps others who may have the same doubts.

Instead of using barcodeView.decodeContinuous(callback); inside the onCreate method use barcodeView.decodeSingle(callback); . Once the QR code is found, it will stop scanning. Call barcodeView.decodeSingle(callback); inside the Confirm and Cancel button to activate the scan again.

+6
source

All Articles