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.
if possible Please share any example to read data matrix bar code.
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
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();
}
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 "";
}
}
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.