原創(chuàng)|其它|編輯:郝浩|2011-09-16 10:38:33.000|閱讀 1440 次
概述:在這篇文章中,我將會通過實例來解釋如何使用Aspose.BarCode for Java來識別word文檔中的條碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在這篇文章中,我將會通過實例來解釋如何使用Aspose.BarCode for Java來識別word文檔中的條碼。
生成條碼并插入Word文檔
首先,我將啟用Aspose.BarCode for Java并生成一個條碼圖像。然后,將這個圖像保存到磁盤上的文件夾。接下來,我將使用Aspose.Words for Java 來創(chuàng)建一個Microsoft Word文檔,并將生成的條碼插入到word文檔中。
從word文檔中提取圖像并讀出條碼
對于識別部分,首先我會使用Aspose.Words for Java從word文檔中提取圖像。一旦提取圖像,我將會把這些圖像傳遞到Aspose.BarCode for Java中以便進行條碼識別。
以下代碼是一個完整的Java程序,可從Microsoft Word文檔生成和識別條碼:
【Java】
import com.aspose.barcode.*;
import com.aspose.barcoderecognition.BarCodeReadType;
import com.aspose.barcoderecognition.BarCodeReader;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImageType;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Shape;
import java.awt.Toolkit;
import java.text.MessageFormat;
public class RecognitionFromWord {
public static void main(String[] args)
{
try
{
// Generate barcode image
BarCodeBuilder builder = new BarCodeBuilder();
builder.setSymbology(Symbology.CODE39STANDARD);
builder.setCodeText("test-123");
String strBarCodeImageSave = "img.jpg";
builder.save(strBarCodeImageSave);
// add this image to word doc
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
docBuilder.insertImage(strBarCodeImageSave);
String strWordFile = "docout.doc";
doc.save(strWordFile);
// recognition part
// extract image from word document
NodeCollection<Shape> shapes =
doc.getChildNodes(NodeType.SHAPE, true, false);
int imageIndex = 0;
for(Shape shape : shapes)
{
if (shape.hasImage())
{
// if this shape is an image, extract image to file
String extension = ImageTypeToExtension(shape.getImageData().getImageType());
String imageFileName = MessageFormat.format
("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
String strBarCodeImageExtracted = "" + imageFileName;
shape.getImageData().save(strBarCodeImageExtracted);
// recognize barcode from this image
BarCodeReader reader =
new BarCodeReader(Toolkit.getDefaultToolkit().getImage
(strBarCodeImageExtracted),BarCodeReadType.Code39Standard);
while (reader.read())
{
System.out.println("codetext: " + reader.getCodeText());
}
imageIndex++;
}
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
private static String ImageTypeToExtension(int imageType) throws Exception
{
switch (imageType)
{
case ImageType.BMP:
return "bmp";
case ImageType.EMF:
return "emf";
case ImageType.JPEG:
return "jpeg";
case ImageType.PICT:
return "pict";
case ImageType.PNG:
return "png";
case ImageType.WMF:
return "wmf";
default:
throw new Exception("Unknown image type.");
}
}
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)