Android: recognize multiple barcodes in a single image

Skip to first unread message

Alessandro Agostini

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 (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 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 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.TryInverted = True
'reader.AutoRotate = False

reader.Options.TryHarder = True

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
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) {
Bitmap img = BitmapFactory.decodeStream(is);
try {
} catch (IOException e) {

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?



Lachezar Dobrev

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?


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
For more options, visit

Alessandro Agostini

Jun 27, 2016, 11:36:31 AM6/27/16
to zxing,
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 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.


Deepak Duvedi

Aug 3, 2016, 1:56:54 AM8/3/16
to zxing,


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

Reply all
Reply to author
0 new messages