在本文中,我們將講解如何使用Aspose.BarCode for .NET 和 Aspose.Pdf for .NET從Pdf文檔中識別條碼。
步驟如下:
1 使用 Aspose.Pdf for .NET從Pdf文檔中提取圖像
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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
try { // set the license for Aspose.BarCode for .NET and Aspose.Pdf for .NET components
Aspose.BarCodeRecognition.License licenceBarCodeRecognition =
new Aspose.BarCodeRecognition.License();
licenceBarCodeRecognition.SetLicense( @"Aspose.Total.Product.Family.lic" );
Aspose.Pdf.License licensePdf =
new Aspose.Pdf.License();
licensePdf.SetLicense( @"Aspose.Total.Product.Family.lic" );
// bind the pdf document
Aspose.Pdf.Facades.PdfExtractor pdfExtractor =
new Aspose.Pdf.Facades.PdfExtractor();
pdfExtractor.BindPdf( @"document.pdf" );
// set page range for image extraction
pdfExtractor.StartPage = 1;
pdfExtractor.EndPage = 1;
// extract the images
Console.WriteLine( "Extracting images....." );
pdfExtractor.ExtractImage();
// save images to stream in a loop
while
(pdfExtractor.HasNextImage()) {
Console.WriteLine( "Getting next image...." );
// save image to stream
MemoryStream imageStream =
new MemoryStream();
pdfExtractor.GetNextImage(imageStream);
imageStream.Position = 0;
Console.WriteLine( "Recognizing barcode...." );
// recognize the barcode from the image stream above
Aspose.BarCodeRecognition.BarCodeReader barcodeReader =
new Aspose.BarCodeRecognition.BarCodeReader(imageStream, BarCodeReadType.Code39Standard);
while
(barcodeReader.Read()) {
Console.WriteLine( "Codetext found: "
+ barcodeReader.GetCodeText() + ", Symbology: "
+ barcodeReader.GetReadType().ToString()); }
// close the reader
barcodeReader.Close();
}
} catch
(Exception ex) { Console.WriteLine(ex.Message);
} |
[VB.NET]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
Try ' set the license for Aspose.BarCode for .NET and Aspose.Pdf for .NET components
Dim
licenceBarCodeRecognition As
New Aspose.BarCodeRecognition.License()
licenceBarCodeRecognition.SetLicense( "Aspose.Total.Product.Family.lic" )
Dim
licensePdf As
New Aspose.Pdf.License() licensePdf.SetLicense( "Aspose.Total.Product.Family.lic" )
' bind the pdf document
Dim
pdfExtractor As
New Aspose.Pdf.Facades.PdfExtractor() pdfExtractor.BindPdf( "docuemnt.pdf" )
' set page range for image extraction
pdfExtractor.StartPage = 1
pdfExtractor.EndPage = 1
' extract the images
Console.WriteLine( "Extracting images....." )
pdfExtractor.ExtractImage()
' save images to stream in a loop
Do
While pdfExtractor.HasNextImage()
Console.WriteLine( "Getting next image...." )
' save image to stream
Dim
imageStream As
New MemoryStream() pdfExtractor.GetNextImage(imageStream)
imageStream.Position = 0
Console.WriteLine( "Recognizing barcode...." )
' recognize the barcode from the image stream above
Dim
barcodeReader As
New Aspose.BarCodeRecognition.BarCodeReader(imageStream, BarCodeReadType.Code39Standard)
Do
While barcodeReader.Read()
Console.WriteLine( "Codetext found: "
& barcodeReader.GetCodeText() & ", Symbology: "
& barcodeReader.GetReadType().ToString()) Loop ' close the reader
barcodeReader.Close()
Loop Catch
ex As Exception
Console.WriteLine(ex.Message)
End
Try |