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
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.
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.
Hi
For "java.lang.OutOfMemoryError" either compress the image or use "largeHeap: true" in your manifest.