0

I've an activity in my app which Either picks an image from gallery to scan for a barcode and then displays the result in a TextView and picked image in an ImageView OR starts the camera to scan one and displays the result in the respective TextView.

I'm using Google Mobile Vision API (indirectly through a Library just to skip the boiler plate code) and the scanning works impressively fast.

I've tried my best to find a way before asking this question through searching the sample project of the library and Google Mobile Vision API and the internet but failed to find one.

How can I pass the actual scanned barcode to an ImageView (or an Activity)? (seems impossible directly)

My code is basically this (just with some intents etc)

@Override
    public void onRetrieved(final Barcode barcode) {
        Log.d(TAG, "Barcode read: " + barcode.displayValue);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("code retrieved")
                        .setMessage(barcode.displayValue);
                builder.show();
            }
        });


    }

    // for multiple callback
    @Override
    public void onRetrievedMultiple(final Barcode closetToClick, final List<BarcodeGraphic> barcodeGraphics) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String message = "Code selected : " + closetToClick.displayValue + "\n\nother " +
                        "codes in frame include : \n";
                for (int index = 0; index < barcodeGraphics.size(); index++) {
                    Barcode barcode = barcodeGraphics.get(index).getBarcode();
                    message += (index + 1) + ". " + barcode.displayValue + "\n";
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("code retrieved")
                        .setMessage(message);
                builder.show();
            }
        });

    }

    @Override
    public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
        for (int i = 0; i < sparseArray.size(); i++) {
        Barcode barcode = sparseArray.valueAt(i);
        Log.e("value", barcode.displayValue);
        }
    }

    @Override
    public void onRetrievedFailed(String reason) {
        // in case of failure
    }
}

Above code works great but is there a way possible to use that SparseArray for obtaining the actual scanned barcode?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hardik sharma
  • 313
  • 2
  • 15

1 Answers1

0

You can use ZXing library to generate Bitmap from barcode value. Add gradle dependencies:

implementation 'com.google.zxing:core:3.2.1'
implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

Then use the next code:

public void generateBarcode(Barcode barcode) {

    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

    String barcodeNumber = barcode.displayValue;
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(barcodeNumber, BarcodeFormat.UPC_A, 200, 200);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);

        imageView.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
Denysole
  • 3,903
  • 1
  • 20
  • 28
  • @Hardiksharma, but `Google Vision API` doesn't provide way to generate barcode image – Denysole Jan 04 '18 at 13:54
  • I admit my silly mistake, yes, I'm using a library based on Zxing library for generating barcodes but here, I don't want to regenerate a QR/Bar code instead I want to take a snap/picture of the barcode and want to display it in an ImageView. And Do note that a QR/bar code is different than a picture of QR/bar code as the picture contains real world objects (a paper or a book or whatever on which the code is printed). Get My point? – Hardik sharma Jan 04 '18 at 14:10