第一、下载从 PDF 读取条形码的 Java API
要从 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>http://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>
第二、使用 Java 从 PDF 读取条形码
Aspose.PDF API 提供了表示 PDF 文档的Document类。API的convertToPNGMemoryStream()方法将 PDF 页面呈现为byte[]数组中的图像流。Aspose.BarCode API 提供BarCodeReader类,使我们能够执行readBarCodes操作来检测条形码。BarCodeResult类存储检测到的条码信息,例如条码类型、代码文本、区域和其他参数。
我们可以按照以下步骤读取嵌入在 PDF 文档任何页面上的条形码图像:
- 首先,使用Document类加载 PDF 文档。
- 接下来,遍历所有页面并渲染到流中。
- 然后,使用流对象创建BarCodeReader类的实例。
- 之后,调用readBarCodes()方法获取BarCodeResult对象。
- 最后,显示条码信息。
以下代码示例展示了如何使用 Java 从 PDF 文档中读取条形码。
// This code example demonstrates how to read a barcode from a PDF document using Java.// The path to the documentString file = "C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";// Load a PDF documentcom.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file);// Proceed all PDF pages starting from page 1for (int i = 1; i <= pdfDoc.getPages().size(); ++i){// Render PDF page to the streambyte[] ms = pdfDoc.getPages().get_Item(i).convertToPNGMemoryStream();InputStream stream = new ByteArrayInputStream(ms);// Recognize barcodes from the page streamBarCodeReader reader = new BarCodeReader(stream);// Show resultsfor (BarCodeResult result : reader.readBarCodes()) {System.out.println("CodeText: " + result.getCodeText());System.out.println("Symbology type: " + result.getCodeType());System.out.println(("-------------------------------"));}}CodeText: Aspose.Barcode Pdf417 ExampleSymbology type: Pdf417-------------------------------CodeText: Aspose.Barcode QR ExampleSymbology type: QR-------------------------------CodeText: Aspose.Barcode DataMatrix ExampleSymbology type: DataMatrix
请下载本博文中使用的带有条形码的输入 PDF 文档。
第三、使用 Java 将 PDF 转换为图像并读取条形码
我们还可以通过将 PDF 页面转换为图像来从 PDF 文档中读取条形码。API的PdfConverter类允许将 PDF 文件的每一页转换为图像。之后,我们将从转换后的图像中读取条形码信息。
我们可以按照以下步骤从转换后的 PDF 页面中读取条形码:
- 首先,使用Document类加载 PDF 文档。
- 接下来,创建PdfConverter类的实例。
- 或者,使用setBarcodeOptimization()设置渲染选项。
- 然后,设置图像分辨率。
- 接下来,使用setStartPage()和setEndPage()指定要渲染成图像的页面范围。
- 然后,调用doConvert()方法将所选页面呈现为图像。
- 接下来,将图像保存在一个循环中。
- 然后,使用保存的图像文件路径创建BarCodeReader类的实例。
- 之后,调用readBarCodes()方法获取BarCodeResult对象。
- 最后,显示条码信息。
以下代码示例展示了如何使用 Java 将 PDF 页面转换为图像并读取条形码。
// The following code example shows how to convert PDF pages into images with PDF Convertor and read barcodes.// The path to the documentString folderPath = "C:\Files\BarCode\";// Input file pathString file = folderPath + "sample-PDF-with-Barcodes.pdf";// Load a PDF documentcom.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file);// Initialize a PdfConvertorcom.aspose.pdf.facades.PdfConverter pdfConverter = new com.aspose.pdf.facades.PdfConverter(pdfDoc);// Set barcode optimizationpdfConverter.getRenderingOptions().setBarcodeOptimization(true);// Set page resolution// 300 dpi is standard resolutionpdfConverter.setResolution(new com.aspose.pdf.devices.Resolution(300));// Set all pages to render into imagespdfConverter.setStartPage(1); //starts from page 1pdfConverter.setEndPage(pdfConverter.getDocument().getPages().size());// Render selected pages into the imagespdfConverter.doConvert();int imageCount = 1;while (pdfConverter.hasNextImage()){// Render current page to imageString strBarCodeImage = folderPath + imageCount + ".jpg";pdfConverter.getNextImage(strBarCodeImage);// Recognize barcodes from the rendered image of the pageBarCodeReader reader = new BarCodeReader(strBarCodeImage);// Show resultsfor (BarCodeResult result : reader.readBarCodes()) {System.out.println("CodeText: " + result.getCodeText());System.out.println("Symbology type: " + result.getCodeType());System.out.println(("-------------------------------"));}}
第四、使用 Java 从 PDF 中提取和读取条形码
同样,我们也可以使用PdfExtractor类识别嵌入在 PDF 页面上的条形码图像。它允许从 PDF 中提取图像,然后我们将从提取的图像中读取条形码信息。
我们可以按照以下步骤从提取的图像中读取条形码:
- 首先,创建PdfExtractor类的实例。
- 接下来,使用bindPdf()方法绑定输入的 PDF 文档。
- 然后,设置图像提取的页面范围。
- 接下来,调用extractImage()方法来提取图像。
- 然后,将图像保存在一个循环中。
- 接下来,使用图像路径创建BarCodeReader类的实例。
- 之后,调用readBarCodes()方法获取BarCodeResult对象。
- 最后,显示条码信息。
以下代码示例展示了如何使用 Java 从 PDF 文档中提取和读取条形码图像。
// The following code example shows how to convert PDF pages into images with PdfExtractor and read barcodes.// The path to the documentString folderPath = "C:\Files\BarCode\";// Input FileString file = folderPath + "sample-PDF-with-Barcodes.pdf";// Bind a PDF documentcom.aspose.pdf.facades.PdfExtractor pdfExtractor = new com.aspose.pdf.facades.PdfExtractor();pdfExtractor.bindPdf(file);// Set page range for image extractionpdfExtractor.setStartPage(1);pdfExtractor.setEndPage(3);// Extract the imagespdfExtractor.extractImage();int imageCount = 1;// Save images to stream in a loopwhile (pdfExtractor.hasNextImage()){// Save imageString strBarCodeImage = folderPath + imageCount + ".jpg";pdfExtractor.getNextImage(strBarCodeImage);// Recognize the barcodes from the imageBarCodeReader 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(("-------------------------------"));}}
第五、在 Java 中使用 PngDevice 从 PDF 读取条形码
我们还可以通过使用 API 的PngDevice类将 PDF 文档的页面转换为 PNG 图像来读取条形码。它提供了将页面转换为PNG并保存在输出流中的process(Page page, OutputStream output)方法。该类的 processToBufferedImage(Page page) 方法将页面转换为BufferedImage 。
我们可以按照以下步骤将转换后的 PDF 页面中的条形码读取为 PNG 图像:
- 首先,使用Document类加载 PDF 文档。
- 接下来,创建PngDevice类的实例。
- 然后,遍历所有页面并调用processToBufferedImage()方法渲染到图像中。
- 接下来,使用BufferedImage对象创建BarCodeReader类的实例。
- 之后,调用readBarCodes()方法获取BarCodeResult对象。
- 最后,显示条码信息。
以下代码示例展示了如何使用 Java 转换 PDF 页面和读取条形码。
// The following code example shows how to convert PDF pages into images with PngDevice and read barcodes.// The path to the documentString file = "C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";// Load a PDF documentcom.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file);// Create PNG device with 300 dpi standard resolutioncom.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 1for (int i = 1; i <= pdfDoc.getPages().size(); ++i){// Render PDF page to the buffered imageBufferedImage img = pngDevice.processToBufferedImage(pdfDoc.getPages().get_Item(i));// Recognize barcode from the rendered image of the pageBarCodeReader reader = new BarCodeReader(img);// Show resultsfor (BarCodeResult result : reader.readBarCodes()) {System.out.println("CodeText: " + result.getCodeText());System.out.println("Symbology type: " + result.getCodeType());System.out.println(("-------------------------------"));}}
第六、使用 Java 从 PDF 中查找和读取条形码图像
我们可以使用ImagePlacementAbsorber类从 PDF 文档中查找和提取条形码图像。它执行图像使用搜索并通过 ImagePlacements 集合提供对搜索结果的访问。此方法允许识别具有原始分辨率的条形码。唯一的缺点是它可能无法正确识别矢量格式。
我们可以按照以下步骤从 PDF 文档中查找和读取条形码:
- 首先,使用Document类加载 PDF 文档。
- 接下来,创建ImagePlacementAbsorber类的实例。
- 然后,在循环中为每个页面调用visit()方法。
- 接下来,遍历ImagePlacements集合中找到的所有图像。
- 然后,将图像保存到内存流中。
- 接下来,使用流对象创建BarCodeReader类的实例。
- 之后,调用readBarCodes()方法获取BarCodeResult对象。
- 最后,显示条码信息。
以下代码示例展示了如何使用 Java 从 PDF 中查找和读取条形码图像。
// This code example demonstrates how to read a barcode from a PDF document using ImagePlacementAbsorber.// The path to the documentString file = "C:\Files\BarCode\sample-PDF-with-Barcodes.pdf";// Load a PDF documentcom.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file);// Initialize ImagePlacementAbsorbercom.aspose.pdf.ImagePlacementAbsorber imagePlacementAbsorber = new com.aspose.pdf.ImagePlacementAbsorber();// Process all PDF pages in the document starting from page 1for (int i = 1; i <= pdfDoc.getPages().size(); ++i){// Visit the page create an image extractorimagePlacementAbsorber.visit(pdfDoc.getPages().get_Item(i));// Extract all images from the PDF pagefor (com.aspose.pdf.ImagePlacement imagePlacement : imagePlacementAbsorber.getImagePlacements()) {// Render PDF page to the streambyte[] ms = pdfDoc.getPages().get_Item(i).convertToPNGMemoryStream();InputStream stream = new ByteArrayInputStream(ms);// Recognize barcode from the page streamBarCodeReader reader = new BarCodeReader(stream);// Show resultsfor (BarCodeResult result : reader.readBarCodes()) {System.out.println("CodeText: " + result.getCodeText());System.out.println("Symbology type: " + result.getCodeType());System.out.println(("-------------------------------"));}}}
以上便是如何用 Java 从 PDF 读取条形码详细步骤,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
获取更多信息请咨询在线客服 或 加入Aspose技术交流群()
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!