Unable to render Data Matrix barcode

211 views
Skip to first unread message

kumar pothula

unread,
May 15, 2017, 1:09:10 PM5/15/17
to zxing
Hi Team,

I am new to barcode reading.

I am using online API(https://zxing.org/w/decode.jspx) to read data matrix barcode and it's not recognizing the barcode.

using the PDFBox I am getting BufferedImage and reading the bar code using the below code.

LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);

System.out.println("Barcode text is " + result.getText());


is any thing wrong with the above code.


when I call reader.decode(bitmap) -> throws com.google.zxing.NotFoundException exception.

Please help me.

soc.pdf.0.png

kumar pothula

unread,
May 15, 2017, 1:10:45 PM5/15/17
to zxing


if possible Please share any example to read data matrix bar code.


Lachezar Dobrev

unread,
May 16, 2017, 6:02:41 AM5/16/17
to kumar pothula, zxing
Data Matrix reader has a quirk: it will only read a Data Matrix that
is in the center of the scanned image. Try and crop the zone where the
code is to be expected, or switch to QR Code.
> --
> 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.

Zorawar B.

unread,
May 18, 2017, 9:23:48 AM5/18/17
to zxing

Try using the API with decoding hints.
https://zxing.github.io/zxing/apidocs/com/google/zxing/DecodeHintType.html

Set it up as,
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);//maybe can be skipped
hints.put(POSSIBLE_FORMATS, EnumSet.of(BarcodeFormat.DATA_MATRIX));
boolean pureBarcode = true; // or false as your case is
hints.put(DecodeHintType.PURE_BARCODE, pureBarcode);
Result result = reader.decode(bitmap, hints);

For PURE_BARCODE hint, use true if you get handle to the actual image or are able to crop and get to it as Lachezar Dobrev suggests.


Regards,
Zorawar

kumar pothula

unread,
May 19, 2017, 12:59:59 PM5/19/17
to zxing
Hi,

I tried but it's working.Please find below my code as well and also find the bar code as an attachment.

Zxing online decode is working for the attached gif file.

I appreciate for your help.

public static String readDataMatrixBarCode(String filePath)
throws Exception{



Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);//maybe can be skipped

hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.of(BarcodeFormat.DATA_MATRIX));
boolean pureBarcode = false; // or false as your case is
hints.put(DecodeHintType.PURE_BARCODE, hints);

InputStream barCodeInputStream = new FileInputStream(""+filePath);
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);


BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();

Result result = reader.decode(bitmap,hints);

System.out.println("Barcode text is " + result.getText());


return result.getText();
}

4.gif

kumar pothula

unread,
May 19, 2017, 3:10:21 PM5/19/17
to zxing
the below code is reading 4.gif but it is not working for soc.pdf.o.png(initial post attachment) bar code.

public static String decodeDataMatrix(String filePath) {
Map hints = new HashMap();
// Some options to fiddle with
// hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.TRY_HARDER, BarcodeFormat.DATA_MATRIX);

try {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result res = new DataMatrixReader().decode(binaryBitmap, hints);
System.out.println("Barcode text is " + res.getText());
return res.getText();
} catch (NotFoundException|ChecksumException|FormatException|IOException e) {
return "";
}
}

Zorawar B.

unread,
May 23, 2017, 3:43:44 PM5/23/17
to zxing
On Friday, May 19, 2017 at 3:10:21 PM UTC-4, kumar pothula wrote:
> the below code is reading 4.gif but it is not working for soc.pdf.o.png(initial post attachment) bar code.

Please read again.
Now that the code to decode barcode works, follow Lachezar Dobrev's suggestion. Try to extract the barcode image from the pdf, either by cropping the pdf image to just have the barcode or by extracting it from the pdf using a pdf manipulation library like iText.
http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-15#562-extractimages.java

Try to find out how the barcode image is placed on the pdf in the first place. That should give you a hint to what is appropriate for your situation.

Reply all
Reply to author
Forward
0 new messages