翻译|使用教程|编辑:胡涛|2022-10-11 11:11:07.000|阅读 240 次
概述:在本文中,我们将学习如何使用 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@ke049m.cn