Reject a barcode

Motivation

When a barcode is scanned, then the phone will be vibrating/beeping, and the recognized code might be visually highlighted depending on the GUI style used. However, you might want to apply your own logic (i.e. validate that the code is in your database) before accepting a code.

Code Rejection

In order to be able to accept only a subset of the codes, it is necessary to enable code rejection.

public void initializeAndStartBarcodeScanning() {
ScanSettings settings = ScanSettings.create();
// Enable the relevant symbologies.
...
// Enable code rejection
settings.setCodeRejectionEnabled(true);

Once this is done, you can use rejectCode(Barcode) method to reject individual codes. Here you can find an example where we iterate through all the recognized codes and possibly reject a few of them after calling shouldRejectCode() which is the method containing the app logic used to decide whether to reject the code or not.

@Override
public void didScan(ScanSession session) {
for (Barcode code : session.getNewlyRecognizedCodes()) {
if (shouldRejectCode(code)) {
session.rejectCode(code);
}
...
}
}

Please note that rejectCode() must be called from the session queue.