0

I'm trying to implement a customization of the barcode reader, the library that i'm using is the following:

implementation 'com.google.android.gms:play-services-vision:20.1.3'

Currently i'm able to detect every single barcode and read the value (see the image below)

enter image description here

Now I should implement a kind of "filter" that show / highlight only a particular barcode (e.g. 3908181) set in the app when starting the activity. The main purpose of the app should be a kind of finder of articles in a warehouse.

This is the code of my main activity:

public final class BarcodeCaptureActivity extends Activity implements BarcodeGraphicTracker.BarcodeUpdateListener {
    private static final int RC_HANDLE_GMS = 9001;
    private String fromWhere = null;
    private CameraSource mCameraSource;
    private CameraSourcePreview mPreview;
    private GraphicOverlay<BarcodeGraphic> mGraphicOverlay;
    private Context context;
    private HashMap<String, String> codes;
    private ArrayList<String> bboxCodes;
    private int counter = 0;
    private DbBboxCheck dbBboxCheck = null;
    private String bboxImei = "", codeToCheck = "";


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.barcode_capture);

        codes = new HashMap<>();
        bboxCodes = new ArrayList<>();

        mPreview = findViewById(R.id.preview);
        mGraphicOverlay = findViewById(R.id.graphicOverlay);
        dbBboxCheck = new DbBboxCheck(getApplicationContext());

        Intent intent = getIntent();
        fromWhere = intent.getStringExtra("fromWhere");
        codeToCheck = intent.getStringExtra("codeToCheck");
        createCameraSource();
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    @SuppressLint("InlinedApi")
    private void createCameraSource() {
        context = getApplicationContext();
        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
        BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, this);
        barcodeDetector.setProcessor(new MultiProcessor.Builder<>(barcodeFactory).build());

        CameraSource.Builder builder = new CameraSource.Builder(context, barcodeDetector);
        builder.setFacing(CameraSource.CAMERA_FACING_BACK);
        builder.setRequestedPreviewSize(1024, 768);
        builder.setRequestedFps(15.0f);
        builder = builder.setFocusMode(FOCUS_MODE_CONTINUOUS_PICTURE);
        mCameraSource = builder.setFlashMode(FLASH_MODE_AUTO).build();
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    @Override
    protected void onResume() {
        super.onResume();
        startCameraSource();
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    @Override
    protected void onPause() {
        super.onPause();
        if (mPreview != null) {
            mPreview.stop();
        }
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPreview != null) {
            mPreview.stop();
        }
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    private void startCameraSource() throws SecurityException {
        int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
        if (code != ConnectionResult.SUCCESS) {
            Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(this, code, RC_HANDLE_GMS);
            dialog.show();
        }

        if (mCameraSource != null) {
            try {
                mPreview.start(mCameraSource, mGraphicOverlay);
            } catch (IOException e) {
                mCameraSource.release();
                mCameraSource = null;
            }
        }
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    @Override
    public void onBarcodeDetected(Barcode barcode) {
        String barCode = barcode.displayValue.trim();
        switch (fromWhere) {
            case "findInWarehouse":
                if(barCode.compareToIgnoreCase(codeToCheck) == 0){
                    MediaPlayer.create(this, R.raw.success).start();
                    //mCameraSource.stop();
                    //mPreview.stop();
                }
                break;

            default:
                MediaPlayer.create(this, R.raw.success).start();
                closeDetection(barCode, "warehouse", false);
                break;
        }
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    private void collectBboxCodes(String currentBarCode) {
        boolean additionalExtra = false;
        if (fromWhere.compareToIgnoreCase("disassembly") == 0) {
            additionalExtra = true;
            if (!bboxCodes.contains(currentBarCode)) {
                if (currentBarCode.length() > 12) {
                    bboxImei = currentBarCode;
                }
                bboxCodes.add(currentBarCode);
            }
        } else {
            if (!bboxCodes.contains(currentBarCode)) {
                bboxCodes.add(currentBarCode);
                if (dbBboxCheck.checkCode(currentBarCode, context)) {
                    MediaPlayer.create(this, R.raw.success).start();
                    closeDetection(currentBarCode, fromWhere, false);
                }
            }
        }

        if (counter == 5) {
            if (fromWhere.compareToIgnoreCase("disassembly") == 0) {
                MediaPlayer.create(this, R.raw.success).start();
                closeDetection(bboxImei, fromWhere, additionalExtra);
            } else {
                MediaPlayer.create(this, R.raw.failure).start();
                closeDetection("", fromWhere, additionalExtra);
            }
        }
        counter++;
    }

    /* *************************************** *************************************** */
    //
    /* *************************************** *************************************** */
    private void closeDetection(String returnData, String fieldType, boolean additionalExtra) {
        Intent intent = new Intent();
        intent.putExtra("code", returnData);
        intent.putExtra("fieldType", fieldType);
        if (additionalExtra) {
            intent.putExtra("listOfBbox", bboxCodes);
        }
        //setResult(CommonStatusCodes.SUCCESS, intent);
        //finish();
    }
}

Currently I don't understand how to implement this feature, any suggesiton ?

Mattia
  • 1,057
  • 2
  • 17
  • 33
  • All three barcodes appear to be the same type which if different could have been one distinguishing feature, so unless you know beforehand what are the PN strings (only 7 digits), you may just need to create a [tracking window](https://stackoverflow.com/q/64285580/295004). – Morrison Chang Jun 27 '22 at 09:24
  • Maybe I have omitted this detail, so for sure which code to find, but I am interested in "highlighting" only that and not the others – Mattia Jun 27 '22 at 09:50

0 Answers0