Reputation: 439
I have to read a Data Matrix code inside of a PDF file, I was investigating the posibilities, and I had some questions about that:
1.- I work with Itext, I was looking for information about the posibility to read this QR barcode with this library, but I have not results about that, is possible??
2.- I discovered another library ZXING : https://github.com/zxing/zxing, but I could not find the way to read inside the PDF.
Can somebody help me with this problem?
New Information, with new Ideas, for fix the problem:
This is my code, with PDFBOX I find the image for each page, afterwards I check if is this image is a barcode for get the data, I don't know why but I can't detect a barcode with this code, the quality of the barcode is high in the PDF.
I try to use the Reader implementation but I couldn't find it, I have the version 3.1 ZXING, perhaps is in another version?
If I use directly the image from the file like this, works perfect:
Image image = ImageIO.read(new File("C:\Workarea\testBarcode.png")); BufferedImage buffered = (BufferedImage) image;
PDDocument document; try {
document3 = PDDocument.load("TEST_QR_BARCODE.pdf");
List pages = document.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
while( iter.hasNext() )
{
PDPage page = (PDPage)iter.next();
PDResources resources = page.getResources();
Map images;
images = resources.getImages();
if( images != null )
{
Iterator imageIter = images.keySet().iterator();
while( imageIter.hasNext() )
{
String key = (String)imageIter.next();
PDXObjectImage image = (PDXObjectImage)images.get( key );
BufferedImage testcojones = image.getRGBImage();
try {
LuminanceSource source;
source = new BufferedImageLuminanceSource(testcojones);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
ParsedResult parsedResult = ResultParser.parseResult(result);
System.out.println(" (format: " + result.getBarcodeFormat() + ", type: " +
parsedResult.getType() + "):\nRaw result:\n" + result.getText() + "\nParsed result:\n" +
parsedResult.getDisplayResult());
System.out.println("Found " + result.getResultPoints().length + " result points.");
for (int i = 0; i < result.getResultPoints().length; i++) {
ResultPoint rp = result.getResultPoints()[i];
if (rp != null) {
System.out.println(" Point " + i + ": (" + rp.getX() + ',' + rp.getY() + ')');
}
}
} catch (NotFoundException ignored) {
System.out.println("No barcode found!");
}
}
}
} } catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace(); }
Upvotes: 1
Views: 3809
Reputation: 66881
To use zxing, you just need to create a BufferedImage
in your Java program from the PDF. That's a separate question, but should be possible with another library. Then you can use BufferedImageLuminanceSource
in mostly the way you see done here: https://github.com/zxing/zxing/blob/master/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java#L125
Upvotes: 0