彩票走势图

如何使用Aspose.BarCode for Java从PDF文档中识别条码

原创|其它|编辑:郝浩|2011-09-20 10:41:01.000|阅读 1208 次

概述:在这篇文章中,我将会通过实例来解释如何使用Aspose.BarCode for Java来识别PDF文档中的条码。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  在这篇文章中,我将会通过实例来解释如何使用Aspose.BarCode for Java来识别PDF文档中的条形码

生成条码并插入PDF文档

  首先,我将启用Aspose.BarCode for Java并生成一个条码图像。然后,将这个图像保存到磁盘上的文件夹。接下来,我将使用Aspose.Words for Java 来创建一个Adobe PDF文档,并将生成的条码插入到PDF文档中。

从PDF文档中提取图像并读出条码

  对于识别部分,首先我会使用Aspose.Words for Java从PDF文档中提取图像。一旦提取图像,我将会把这些图像传递到Aspose.BarCode for Java中以便进行条码识别。

  以下代码是一个完整的Java程序,可从Adobe PDF文档生成和识别条码:

[Java]

import com.aspose.barcode.*;

import com.aspose.barcoderecognition.BarCodeReadType;

import com.aspose.barcoderecognition.BarCodeReader;

import com.aspose.pdf.elements.Pdf;

import com.aspose.pdf.elements.Section;

import com.aspose.pdf.kit.License;

import com.aspose.pdf.kit.PdfExtractor;

import java.awt.Toolkit;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

 

public class RecognitionFromPdf

{

public static void main(String[] args)

{

try

{

// set license

License licPdfKit = new License();

FileInputStream stream = new FileInputStream("AsposeLicense.lic");

licPdfKit.setLicense(stream);

 

com.aspose.barcode.License licBarCode = new com.aspose.barcode.License();

licBarCode.setLicense("AsposeLicense.lic");

 

String strPdfDoc = " output1.pdf";

String strBarCodeImage = "";

 

// generate barcode and add to pdf file

BarCodeBuilder builder = new BarCodeBuilder();

builder.setSymbology(Symbology.CODE39STANDARD);

builder.setCodeText("test-123");

String strBarCodeImageSave = " img.jpg";

builder.save(strBarCodeImageSave);

//Instantiate a Pdf object by calling its empty constructor

Pdf pdf1 = new Pdf();

//Create a section in the Pdf object

Section sec1 = pdf1.getSections().add();

//Create an image object in the section

com.aspose.pdf.elements.Image img1 = new com.aspose.pdf.elements.Image(sec1);

//Add image object into the Paragraphs collection of the section

sec1.getParagraphs().add(img1);

//Set the path of image file

img1.getImageInfo().setFile(strBarCodeImageSave);

img1.getImageInfo().setTitle("JPEG image");

//Save the Pdf

FileOutputStream out = new FileOutputStream(new File(strPdfDoc));

pdf1.save(out);

 

//Instantiate PdfExtractor object

PdfExtractor extractor = new PdfExtractor();

 

//Bind the input PDF document to extractor

extractor.bindPdf(strPdfDoc);

 

//Extract images from the input PDF document

extractor.extractImage();

String suffix = ".jpg";

int imageCount = 1;

while (extractor.hasNextImage()) {

System.out.println("Extracting image " + imageCount);

strBarCodeImage = "tmpbarcode" + imageCount + suffix;

extractor.getNextImage(strBarCodeImage);

 

// recognize barcode from image

BarCodeReader reader = new BarCodeReader
(Toolkit.getDefaultToolkit().getImage(strBarCodeImage),
BarCodeReadType.Code39Standard);

while (reader.read())

{

System.out.println("codetext: " + reader.getCodeText());

}

imageCount++;

}

}

catch(Exception ex)

{

System.out.println(ex.getMessage());

}

}

}

下载《Aspose.BarCode for Java》

 


(慧都控件网版权所有,转载请注明出处,否则追究法律责任)
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP