Android: recognize multiple barcodes in a single image

709 views
Skip to first unread message

Alessandro Agostini

unread,
Jun 24, 2016, 6:51:19 AM6/24/16
to zxing
Hi everyone, here's my problem.
I'm trying to create an android app to recognize multiple barcodes from a single image. The image has to be chosen from the gallery.

I tried to create a similart thing on a desktop application in vb.net (using
zxing.dll) and it worked pretty well.

So I tried to replicate the same thing on Android. I got core-3.2.1.jar in my project and tried to figure out what classes to use (since there are some differences from vb.net and Android/java) by searching on the examples provided with the library.

My problem is no barcode is found and my result array is always null.

Here is my working vb.net code:

Private Sub MultipleTest(_path as String)
Dim res As String = ""

If _path = "" Then Return

Dim img As Bitmap = New Bitmap(_path)

Dim source As ZXing.LuminanceSource = New BitmapLuminanceSource(img)

Dim bmp As BinaryBitmap = New BinaryBitmap(New GlobalHistogramBinarizer(source))

Dim reader As BarcodeReader = New BarcodeReader()

reader.Options.PossibleFormats = New List(Of BarcodeFormat)
reader.Options.PossibleFormats.Add(BarcodeFormat.ITF)

'reader.TryInverted = True
'reader.AutoRotate = False

reader.Options.TryHarder = True


Try
Dim results() As Result = reader.DecodeMultiple(img)

If Not IsNothing(results) Then
For Each r As Result In results
res &= r.BarcodeFormat().ToString & " - " & r.Text & vbNewLine
Next
End If
Catch ex As Exception
res = "Error:" & ex.Message
End Try
If res = "" Then res = "No barcode found"

txtResult.Text = res

and here's my Android code (notee


public void DecodeMultipleBarcodes(Uri path) {

InputStream is = null;
try {
is = getContentResolver().openInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap img = BitmapFactory.decodeStream(is);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}

int width = img.getWidth();
int height = img.getHeight();


int[] pixels = new int[width * height];
img.getPixels(pixels, 0, width, 0, 0, width, height);

LuminanceSource source = new RGBLuminanceSource(width, height, pixels);


BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(source));


Reader reader = new MultiFormatReader();
MultipleBarcodeReader greader = new GenericMultipleBarcodeReader(reader);
Result[] result = null;

Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));


try {
result = greader.decodeMultiple(bmp, decodeHints);
} catch (NotFoundException e) {

} catch (NullPointerException e) {

}

if (result != null) {
String res = "";

// TODO: Add barcodes to proper list
for (int i = 0; i< result.length; i++)
{
res += result[i].getText() + "\n";
}
showAlertDialog(MainMenu.this, "Results:",
res, false);
}

}


Can anybody help me to understand what I'm doing wrong?

Thanks

Alessandro

Lachezar Dobrev

unread,
Jun 27, 2016, 10:48:01 AM6/27/16
to Alessandro Agostini, zxing
  1. In VB variant you're using the GlobalHistogramBinarizer, while in Java/Android you're using HybridBinarizer, which might affect the detection process.

  2. You're swallowing the possible exceptions, which might point you to what went wrong.
     put this in the (currently blank) catch clauses:
     android.util.Log.e("BarCode", "Detection/Decoding failed", e);

  3. Please attach/publish an example image that you're trying to decode.

  4. You might want to split the process in steps. It might be the image reading that fails?


Alessandro

--
You received this message because you are subscribed to the Google Groups "zxing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zxing+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alessandro Agostini

unread,
Jun 27, 2016, 11:36:31 AM6/27/16
to zxing, agostiniale...@gmail.com
Him first of all thanks for the help.

1. You are right, I tried with GlobalHistogramBinarizer already before changing it to HybridBinarizer because in the examples the developer uses that one. By the way I changed it back to GlobalHistogramBinarizer to have the same class I use in VB.

Other differences are BarcodeReader/GenericMultipleBarcodeReader since in the Android library there's no such class and BitmapLuminanceSource/RGBLuminanceSource for the same reason.

2. In Android studio the Android monitor should tell me if an exception is being thrown. I tried to add the code to handle the exception and actually com.google.zxing.NotFoundException is thrown.

3. Attached now

4. I tried to debug it and I can actually watch the value of img... the preview is ok so I guess that's not the problem.
While debugging I can't see the value of bmp as it says Method threw "java.lang.OutOfMemoryError" exception when I initialize it, but I'm not sure if it's an Android studio error (he can't show me the preview) or not since that line is not in a try-catch and it should make the app crash.

4.jpg
5.jpg

Deepak Duvedi

unread,
Aug 3, 2016, 1:56:54 AM8/3/16
to zxing, agostiniale...@gmail.com

Hi

For "java.lang.OutOfMemoryError" either compress the image or use "largeHeap: true" in your manifest.

Reply all
Reply to author
Forward
0 new messages