来源:慧都控件网
在本文中,我们将使用Aspose.BarCode 和Aspose.Words这两个控件实现从Word文档中识别条码。步骤如下:
1、使用Aspose.Words for .NET从文档中提取图像
2 、以流的形式保存提取的图像
3 、将图像以流的形式传递到Aspose.BarCode for .NET中
4 、从图像中读取条码
[C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// Load the word document
Document wordDocument =
new
Document(
"Invitation.doc"
);
// get all the shapes
NodeCollection shapes = wordDocument.GetChildNodes(NodeType.Shape,
true
,
false
);
// loop through all the shapes
foreach
(Shape shape
in
shapes)
{
// check if it has an image
if
(shape.HasImage)
{
// save the image in memory stream
MemoryStream imgStream =
new
MemoryStream();
shape.ImageData.Save(imgStream);
// recognize the barcode from the image stream above
BarCodeReader reader =
new
BarCodeReader(
new
Bitmap(imgStream), BarCodeReadType.Code39Standard);
while
(reader.Read())
{
Console.WriteLine(
"Codetext found: "
+ reader.GetCodeText());
}
// close the reader
reader.Close();
}
|
[VB.NET]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
' Load the word document
Dim
wordDocument
As
Document =
New
Document(
"Invitation.doc"
)
' get all the shapes
Dim
shapes
As
NodeCollection = wordDocument.GetChildNodes(NodeType.Shape,
True
,
False
)
' loop through all the shapes
For
Each
shape
As
Shape
In
shapes
' check if it has an image
If
shape.HasImage
Then
' save the image in memory stream
Dim
imgStream
As
MemoryStream =
New
MemoryStream()
shape.ImageData.Save(imgStream)
' recognize the barcode from the image stream above
Dim
reader
As
BarCodeReader =
New
BarCodeReader(
New
Bitmap(imgStream), BarCodeReadType.Code39Standard)
Do
While
reader.Read()
Console.WriteLine(
"Codetext found: "
& reader.GetCodeText())
Loop
' close the reader
reader.Close()
End
If
Next
shape
|