Digital image development LEADTOOLS, how to use LEADTOOLS intelligent capture bar code data extracted from the scanned document

LEADTOOLS (Lead Technology) was created by Moe Daher and Rich Little in 1990, with its headquarters in Charlotte, North Carolina. LEAD is to establish Mr. Daher in the market of digital image compression technology in the field of the invention oriented. In the course of development in 29 years, LEAD its possession in major countries worldwide market leadership in the field of digital image development tools has become an established global leader. Development and Release of LEADTOOLS LEAD is an award-winning development kit.

LEADTOOLS Barcode Pro contains the developer to be detected, read and write more than 100 different types of 1D and 2D bar codes and subtype, such as UPC, EAN, Code 128, QR Code, Data Matrix and PDF417 and so on. Compared to other similar types of barcode imaging technology on the market, LEADTOOLS Barcode Pro is the best.

Intelligent capture can mean several different ways to capture data from an image or document. In this article, we will discuss how to intelligently capture bar codes from scanned documents. LEADTOOLS TWAIN SDK and Barcode SDK, developers can easily use it to create a physical document scanning applications, as well as to capture and extract any bar code found in the database.

AIIM A recent survey showed that 32% of respondents use smart capture technology to extract barcodes from PDF and other digital documents. Having said that, let's create a .NET desktop application, it will recognize the scanned image of the bar code. For each barcode found, we will extract the data, then save the data to a text file, but the scanned document as a PDF.

This application will only use three buttons. Saving a PDF for selecting and TXT output directory, for selecting a scanner to scan, for performing another scan and recognize the barcode.

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);

Now add the following variables.

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;

In the Form1_Load, add the code to set the license, initialize the new session Twain, subscribe TwainSession.Acquire event, and then initialize any image processing command.

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);
    }
}


Guess you like

Origin blog.51cto.com/14467432/2432720