Hi.
I’m reading barcodes inside a pdf file (see attachment)
aspose_pdf_test.pdf (61,1 KB)
I’m using these versions of aspose library :
<aspose-barcode.version>20.7</aspose-barcode.version>
<aspose-pdf.version>23.3</aspose-pdf.version>
And this is my Java class example :
public class AsposeTest {
public static void main(String[] args){
String file = "C:\\tmp\\aspose_pdf_test.pdf";
var start = Instant.now();
// Load a PDF document
com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file);
// Proceed all PDF pages starting from page 1
for (int i = 1; i <= pdfDoc.getPages().size(); ++i) {
var startPage = Instant.now();
// Render PDF page to the stream
Page page = pdfDoc.getPages().get_Item(i);
System.out.println("************* Page nr : " + i);
PageInfo pageInfo = page.getPageInfo();
System.out.println(
"pageInfo - getHeight : " + pageInfo.getHeight()
+ " - getWidth : " + pageInfo.getWidth()
+ " - isLandscape : " + pageInfo.isLandscape());
Rectangle pageRect = page.getPageRect(true);
System.out.println(
"pageRect - getHeight : " + pageRect.getHeight()
+ " - getWidth : " + pageRect.getWidth());
System.out.println();
byte[] ms = page.convertToPNGMemoryStream();
InputStream stream = new ByteArrayInputStream(ms);
// Recognize barcodes from the page stream
BarCodeReader reader = new BarCodeReader(stream);
// Show results
for (BarCodeResult result : reader.readBarCodes()) {
Quadrangle quadrangle = result.getRegion().getQuadrangle();
java.awt.Rectangle boundingRectangle = quadrangle.getBoundingRectangle();
System.out.println(
"boundingRectangle - getHeight : " + boundingRectangle.getHeight()
+ " - getWidth : " + boundingRectangle.getWidth());
System.out.println(result);
System.out.println(quadrangle);
System.out.println();
}
var stopPage = Instant.now();
var diffPage = ChronoUnit.SECONDS.between(startPage, stopPage);
System.out.println("Page nr " + i + " - Time to read barcodes : " + diffPage + " seconds");
System.out.println(("-------------------------------"));
System.out.println();
}
var stop = Instant.now();
var diff = ChronoUnit.SECONDS.between(start, stop);
System.out.println("Total time to read pdf barcodes : " + diff + " seconds");
}
}
I obtain this result :
************* Page nr : 1
pageInfo - getHeight : 842.0 - getWidth : 595.0 - isLandscape : false
pageRect - getHeight : 842.0 - getWidth : 596.0
boundingRectangle - getHeight : 626.0 - getWidth : 626.0
CodeType = QR, CodeText = "https://www.aspose.com/", Confidence = 100, ReadingQuality = 100
LeftTop = java.awt.Point[x=358,y=755], RightTop = java.awt.Point[x=983,y=755], RightBottom = java.awt.Point[x=982,y=1380], LeftBottom = java.awt.Point[x=358,y=1380]
boundingRectangle - getHeight : 164.0 - getWidth : 1237.0
CodeType = Code128, CodeText = "https://www.aspose.com/", Confidence = 80, ReadingQuality = 94.8224029541016
LeftTop = java.awt.Point[x=938,y=2303], RightTop = java.awt.Point[x=2174,y=2303], RightBottom = java.awt.Point[x=2174,y=2466], LeftBottom = java.awt.Point[x=938,y=2466]
Page nr 1 - Time to read barcodes : 15 seconds
-------------------------------
************* Page nr : 2
pageInfo - getHeight : 842.0 - getWidth : 595.0 - isLandscape : false
pageRect - getHeight : 596.0 - getWidth : 842.0
boundingRectangle - getHeight : 1238.0 - getWidth : 164.0
CodeType = Code128, CodeText = "https://www.aspose.com/", Confidence = 80, ReadingQuality = 94.9283905029297
LeftTop = java.awt.Point[x=1206,y=937], RightTop = java.awt.Point[x=1206,y=2174], RightBottom = java.awt.Point[x=1043,y=2174], LeftBottom = java.awt.Point[x=1043,y=937]
boundingRectangle - getHeight : 626.0 - getWidth : 627.0
CodeType = QR, CodeText = "https://www.aspose.com/", Confidence = 100, ReadingQuality = 100
LeftTop = java.awt.Point[x=2755,y=357], RightTop = java.awt.Point[x=2755,y=982], RightBottom = java.awt.Point[x=2129,y=981], LeftBottom = java.awt.Point[x=2130,y=357]
Page nr 2 - Time to read barcodes : 7 seconds
-------------------------------
************* Page nr : 3
pageInfo - getHeight : 842.0 - getWidth : 595.0 - isLandscape : false
pageRect - getHeight : 842.0000000000001 - getWidth : 596.0000000000001
boundingRectangle - getHeight : 165.0 - getWidth : 1238.0
CodeType = Code128, CodeText = "https://www.aspose.com/", Confidence = 80, ReadingQuality = 94.9281463623047
LeftTop = java.awt.Point[x=1547,y=1206], RightTop = java.awt.Point[x=310,y=1206], RightBottom = java.awt.Point[x=310,y=1042], LeftBottom = java.awt.Point[x=1547,y=1042]
boundingRectangle - getHeight : 627.0 - getWidth : 626.0
CodeType = QR, CodeText = "https://www.aspose.com/", Confidence = 100, ReadingQuality = 100
LeftTop = java.awt.Point[x=2128,y=2755], RightTop = java.awt.Point[x=1503,y=2755], RightBottom = java.awt.Point[x=1503,y=2129], LeftBottom = java.awt.Point[x=2128,y=2130]
Page nr 3 - Time to read barcodes : 6 seconds
-------------------------------
Total time to read pdf barcodes : 30 seconds
I have some questions :
- Why ‘isLandscape’ is always false? Second page orientation is different
- Why LeftTop, RightTop (and so on) are bigger than page width and height? Different unit measure?
- Why is so slow? 30 seconds to parse a 3 pages pdf?
- Am I doing it right or is there a better way (I mean fast way) to read barcodes?
With result.getRegion().getQuadrangle() then I need to draw a rectangle around barcodes on a browser pdf preview.
Thanks and sorry for any spelling mistakes. English is not my native language