LEADTOOLS (Lead Technology)由Moe Daher and Rich Little创建于1990年,其总部设在北卡罗来纳州夏洛特。LEAD的建立是为了使Daher先生在数码图象与压缩技术领域的发明面向市场。在29年的发展历程中,LEAD以其在全世界主要国家中占有的市场领导地位,在数码图象开发工具领域中已成为既定的全球领导者。LEADTOOLS开发与发布的LEAD是屡获殊荣的开发工具包。
LEADTOOLS Barcode Pro包含了开发人员需要检测、读取和写入超过100种不同的1D和2D条形码类型和子类型,如UPC、EAN、Code 128、QR Code、Data Matrix和PDF417等等。相比于市场上其他同类型的条形码成像技术,LEADTOOLS Barcode Pro无疑是最好的。
智能捕获可以指从图像或文档捕获数据的几种不同方式。在这篇文章中,将讨论如何智能地从扫描的文件中捕获条形码。LEADTOOLS TWAIN SDK和条码SDK是开发人员可以轻松地使用它来创建物理文档的扫描应用程序,同时捕捉和提取发现的任何条形码数据库。
AIIM 最近的一项调查表明,32%的受访公司使用智能捕获技术从PDF和其他数字文档中提取条形码。说到这里,让我们创建一个.NET桌面应用程序,它将识别扫描图像中的条形码。对于找到的每个条形码,我们将提取数据,然后将该数据保存到文本文件,同时将扫描的文档另存为PDF。
这个应用程序将只使用三个按钮。一个用于选择将保存PDF和TXT的输出目录,一个用于选择要扫描的扫描仪,另一个用于执行扫描并识别条形码。
SelectDir_Click
// Change the output directory
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.ShowNewFolderButton = true;
if (dlg.ShowDialog(this) == DialogResult.OK)
_outputDirectory = System.IO.Path.GetFullPath(dlg.SelectedPath);
}
ScannerSelect_Click
// Select the scanner to use
_twainSession.SelectSource(null);
Scan_Read_Click
// Scan the new page(s)
_twainSession.Acquire(TwainUserInterfaceFlags.Show);
现在添加以下变量。
private static BarcodeEngine engine = new BarcodeEngine();
private static BarcodeReader reader = engine.Reader;
// The Twain session
private static TwainSession _twainSession;
// The output directory for saving PDF files
private static string _outputDirectory;
// The image processing commands we are going to use to clean the scanned image
private static List<RasterCommand> _imageProcessingCommands;
private static int _scanCount;
private static StringBuilder sb;
在Form1_Load中,添加用于设置许可证的代码、初始化新的Twain会话、订阅TwainSession.Acquire事件,然后初始化任何图像处理命令。
RasterSupport.SetLicense(@"", "");
_twainSession = new TwainSession();
_twainSession.Startup(this.Handle, "My Company",
"My Product",
"My Version",
"My Application",
TwainStartupFlags.None);
_twainSession.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(_twainSession_AcquirePage);
// Add as many as you like, here we will add Deskew and Despeckle
_imageProcessingCommands = new List<RasterCommand>();
_imageProcessingCommands.Add(new DeskewCommand());
_imageProcessingCommands.Add(new DespeckleCommand());
在Form1_FormClosed中,结束TWAIN会话。
// End the twain session
_twainSession.Shutdown();
最后添加Twain获取句柄的代码。
private void _twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
_scanCount++;
// We have a page
RasterImage image = e.Image;
// First, run the image processing commands on it
foreach (RasterCommand command in _imageProcessingCommands)
{
command.Run(image);
}
// Read all the barcodes in this image
BarcodeData[] barcodes = reader.ReadBarcodes(e.Image, LeadRect.Empty, 0, null);
// Print out the barcodes we found
sb = new StringBuilder();
sb.AppendLine($"Contains {barcodes.Length} barcodes");
for (int i = 0; i < barcodes.Length; i++)
{
BarcodeData barcode = barcodes[i];
sb.AppendLine($" {i + 1} - {barcode.Symbology} - {barcode.Value}");
}
// Save string builder to a text file
System.IO.File.WriteAllText($@"{_outputDirectory}\barcodes{_scanCount}.txt", sb.ToString());
// Save image as PDF
using (RasterCodecs codecs = new RasterCodecs())
{
codecs.Save(e.Image,
$@"{_outputDirectory}\ScannedImage{_scanCount}.pdf",
RasterImageFormat.RasPdf, 0);
}
}