翻譯|使用教程|編輯:胡濤|2022-10-11 11:11:07.000|閱讀 221 次
概述:在本文中,我們將學習如何使用 Java 從 PDF 文檔中讀取條形碼,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF 是一款高級PDF處理API,可以在跨平臺應用程序中輕松生成,修改,轉換,呈現,保護和打印文檔。無需使用Adobe Acrobat。此外,API提供壓縮選項,表創建和處理,圖形和圖像功能,廣泛的超鏈接功能,圖章和水印任務,擴展的安全控件和自定義字體處理。。本文將為你介紹如何在 C++ 中將PDF轉換為Doc 、Docx 。
發票、收據或報告等PDF文檔可能包含條形碼形式的編碼信息。我們可以以編程方式檢測、識別和讀取嵌入在 PDF 文檔中的條形碼。在本文中,我們將學習如何使用 Java 從 PDF 文檔中讀取條形碼。此外,我們將學習如何在 Java 中從 PDF 文檔中提取條形碼圖像。
要從 PDF 文檔中讀取條形碼,我們將遵循兩步過程。首先,我們將使用Aspose.PDF for Java API 加載 PDF 文檔并將其頁面呈現為光柵圖像。之后,我們將使用Aspose.BarCode for Java API 從渲染圖像中讀取條形碼。
請下載 API 的 JAR或在基于 Maven 的 Java 應用程序中添加以下pom.xml配置。
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>//repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-barcode</artifactId> <version>22.8</version> </dependency>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>22.8</version> </dependency>
Aspose.PDF API 提供了表示 PDF 文檔的API的方法將 PDF 頁面呈現為byte[]數組中的圖像流。Aspose.BarCode API 提供類,使我們能夠執行操作來檢測條形碼。BarCodeResult類存儲檢測的條碼信息,例如條碼類型、代碼文本、區域和其他參數。
我們可以按照以下步驟讀取嵌入在 PDF 文檔任何頁面上的條形碼圖像:
以下代碼示例展示了如何使用 Java 從 PDF 文檔中讀取條形碼。
// This code example demonstrates how to read a barcode from a PDF document using Java. // The path to the document String file = "C:\\Files\\BarCode\\sample-PDF-with-Barcodes.pdf"; // 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) { // Render PDF page to the stream byte[] ms = pdfDoc.getPages().get_Item(i).convertToPNGMemoryStream(); InputStream stream = new ByteArrayInputStream(ms); // Recognize barcodes from the page stream BarCodeReader reader = new BarCodeReader(stream); // Show results for (BarCodeResult result : reader.readBarCodes()) { System.out.println("CodeText: " + result.getCodeText()); System.out.println("Symbology type: " + result.getCodeType()); System.out.println(("-------------------------------")); } } CodeText: Aspose.Barcode Pdf417 Example Symbology type: Pdf417 ------------------------------- CodeText: Aspose.Barcode QR Example Symbology type: QR ------------------------------- CodeText: Aspose.Barcode DataMatrix Example Symbology type: DataMatrix
請下載本博文中使用的
我們還可以通過將 PDF 頁面轉換為圖像來從 PDF 文檔中讀取條形碼。API的類允許將 PDF 文件的每一頁轉換為圖像。之后,我們將從轉換后的圖像中讀取條形碼信息。
我們可以按照以下步驟從轉換后的 PDF 頁面中讀取條形碼:
以下代碼示例展示了如何使用 Java 將 PDF 頁面轉換為圖像并讀取條形碼。
// The following code example shows how to convert PDF pages into images with PDF Convertor and read barcodes. // The path to the document String folderPath = "C:\\Files\\BarCode\\"; // Input file path String file = folderPath + "sample-PDF-with-Barcodes.pdf"; // Load a PDF document com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file); // Initialize a PdfConvertor com.aspose.pdf.facades.PdfConverter pdfConverter = new com.aspose.pdf.facades.PdfConverter(pdfDoc); // Set barcode optimization pdfConverter.getRenderingOptions().setBarcodeOptimization(true); // Set page resolution // 300 dpi is standard resolution pdfConverter.setResolution(new com.aspose.pdf.devices.Resolution(300)); // Set all pages to render into images pdfConverter.setStartPage(1); //starts from page 1 pdfConverter.setEndPage(pdfConverter.getDocument().getPages().size()); // Render selected pages into the images pdfConverter.doConvert(); int imageCount = 1; while (pdfConverter.hasNextImage()) { // Render current page to image String strBarCodeImage = folderPath + imageCount + ".jpg"; pdfConverter.getNextImage(strBarCodeImage); // Recognize barcodes from the rendered image of the page BarCodeReader reader = new BarCodeReader(strBarCodeImage); // Show results for (BarCodeResult result : reader.readBarCodes()) { System.out.println("CodeText: " + result.getCodeText()); System.out.println("Symbology type: " + result.getCodeType()); System.out.println(("-------------------------------")); } }
同樣,我們也可以使用類識別嵌入在 PDF 頁面上的條形碼圖像。它允許從 PDF 中提取圖像,然后我們將從提取的圖像中讀取條形碼信息。
我們可以按照以下步驟從提取的圖像中讀取條形碼:
以下代碼示例展示了如何使用 Java 從 PDF 文檔中提取和讀取條形碼圖像。
// The following code example shows how to convert PDF pages into images with PdfExtractor and read barcodes. // The path to the document String folderPath = "C:\\Files\\BarCode\\"; // Input File String file = folderPath + "sample-PDF-with-Barcodes.pdf"; // Bind a PDF document com.aspose.pdf.facades.PdfExtractor pdfExtractor = new com.aspose.pdf.facades.PdfExtractor(); pdfExtractor.bindPdf(file); // Set page range for image extraction pdfExtractor.setStartPage(1); pdfExtractor.setEndPage(3); // Extract the images pdfExtractor.extractImage(); int imageCount = 1; // Save images to stream in a loop while (pdfExtractor.hasNextImage()) { // Save image String strBarCodeImage = folderPath + imageCount + ".jpg"; pdfExtractor.getNextImage(strBarCodeImage); // Recognize the barcodes from the image BarCodeReader reader = new BarCodeReader(strBarCodeImage); for (BarCodeResult result : reader.readBarCodes()) { System.out.println("CodeText: " + result.getCodeText()); System.out.println("Symbology type: " + result.getCodeType()); System.out.println(("-------------------------------")); } }
類將 PDF 文檔的頁面轉換為 PNG 圖像來讀取條形碼。它提供了將頁面轉換為PNG并保存在輸出流中的該類的 processToBufferedImage(Page page) 方法將頁面轉換為BufferedImage
我們可以按照以下步驟將轉換后的 PDF 頁面中的條形碼讀取為 PNG 圖像:
以下代碼示例展示了如何使用 Java 轉換 PDF 頁面和讀取條形碼。
// The following code example shows how to convert PDF pages into images with PngDevice and read barcodes. // The path to the document String file = "C:\\Files\\BarCode\\sample-PDF-with-Barcodes.pdf"; // Load a PDF document com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file); // Create PNG device with 300 dpi standard resolution com.aspose.pdf.devices.PngDevice pngDevice = new com.aspose.pdf.devices.PngDevice(new com.aspose.pdf.devices.Resolution(300)); // Proceed all the PDF pages starting from page 1 for (int i = 1; i <= pdfDoc.getPages().size(); ++i) { // Render PDF page to the buffered image BufferedImage img = pngDevice.processToBufferedImage(pdfDoc.getPages().get_Item(i)); // Recognize barcode from the rendered image of the page BarCodeReader reader = new BarCodeReader(img); // Show results for (BarCodeResult result : reader.readBarCodes()) { System.out.println("CodeText: " + result.getCodeText()); System.out.println("Symbology type: " + result.getCodeType()); System.out.println(("-------------------------------")); } }
類從 PDF 文檔中查找和提取條形碼圖像。它執行圖像使用搜索并通過 ImagePlacements 集合提供對搜索結果的訪問。此方法允許識別具有原始分辨率的條形碼。唯一的缺點是它可能無法正確識別矢量格式。
我們可以按照以下步驟從 PDF 文檔中查找和讀取條形碼:
以下代碼示例展示了如何使用 Java 從 PDF 中查找和讀取條形碼圖像。
// This code example demonstrates how to read a barcode from a PDF document using ImagePlacementAbsorber. // The path to the document String file = "C:\\Files\\BarCode\\sample-PDF-with-Barcodes.pdf"; // Load a PDF document com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file); // Initialize ImagePlacementAbsorber com.aspose.pdf.ImagePlacementAbsorber imagePlacementAbsorber = new com.aspose.pdf.ImagePlacementAbsorber(); // Process all PDF pages in the document starting from page 1 for (int i = 1; i <= pdfDoc.getPages().size(); ++i) { // Visit the page create an image extractor imagePlacementAbsorber.visit(pdfDoc.getPages().get_Item(i)); // Extract all images from the PDF page for (com.aspose.pdf.ImagePlacement imagePlacement : imagePlacementAbsorber.getImagePlacements()) { // Render PDF page to the stream byte[] ms = pdfDoc.getPages().get_Item(i).convertToPNGMemoryStream(); InputStream stream = new ByteArrayInputStream(ms); // Recognize barcode from the page stream BarCodeReader reader = new BarCodeReader(stream); // Show results for (BarCodeResult result : reader.readBarCodes()) { System.out.println("CodeText: " + result.getCodeText()); System.out.println("Symbology type: " + result.getCodeType()); System.out.println(("-------------------------------")); } } }
以上便是如何用 Java 從 PDF 讀取條形碼詳細步驟 ,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn