The requirement is to get the list of barcodes and its associated type , data ,etc. along with its positioning in a PDF.I am using open-source Apache PDFBox and Zxing Java based libraries for the implementation and not allowed to implement using any paid libraries.
The following response is being returned for barcode co-ordinates/position , which is beyond the range of PDF. Values such as Point: (4791.0, 589.5) and Point: (4791.0, 1001.5) which can't be plotted in a PDF. The barcode text and format is being correctly extracted using the following code.
Any idea on how to get the correct Barcode positions from the following code / any alternate source code ?
Output from the source code
Barcode text: Barcode format: QR_CODE Point: (4689.0, 3745.0) Point: (4689.0, 3569.0) Point: (4865.0, 3569.0) Point: (4841.0, 3721.0)
Barcode text: 28852 Barcode format: CODE_128 Point: (4791.0, 589.5) Point: (4791.0, 1001.5)
Barcode text: 08179018 Barcode format: UPC_E Point: (1500.5, 438.0) Point: (906.5, 438.0)
Barcode text: 147089001 Barcode format: CODE_128 Point: (4867.0, 1522.5) Point: (4867.0, 2072.5)
Barcode text: OPS Barcode format: CODE_128 Point: (4867.0, 2603.5) Point: (4867.0, 2947.5)
public static void getBarcodePosition(String filename) throws IOException, NotFoundException {
PDDocument document = Loader.loadPDF(new File(filename));
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage image = pdfRenderer.renderImageWithDPI(page, 600, ImageType.RGB);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
GenericMultipleBarcodeReader reader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Result[] results = reader.decodeMultiple(bitmap, hints);
for (Result result : results) {
System.out.println("Barcode text: " + result.getText());
System.out.println("Barcode format: " + result.getBarcodeFormat());
for (ResultPoint point : result.getResultPoints()) {
for (ResultPoint point : result.getResultPoints()) {
System.out.println("Point: (" + point.getX() + ", " + point.getY() + ")");
}
System.out.println(" ");
}
}
}
document.close();
}