Intelligent capture can refer to a couple different ways of capturing data from images or documents. In this post, I'll be talking about how you can intelligently capture barcodes from scanned in files. The LEADTOOLS TWAIN SDK and Barcode SDK are libraries that developers can easily use to create applications for scanning in physical documents while capturing and extracting any barcode data found.
A recent survey by AIIM states that 32% of the surveyed companies use intelligent capture to extract barcodes from PDFs as well as other digital documents.[1] With that being said, let's create a .NET desktop application that will recognize barcodes from scanned in images. For each barcode found, we will extract the data and then save that data to a text file while also saving the scanned document as a PDF.
This app will only use three buttons. One for selecting an output directory where the PDF and TXT will be saved to, one to select the scanner you wish to scan from, and one more to perform the scan and recognize the barcodes.
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;
Within Form1_Load
, add the code for setting your license, initializing a new Twain session, subscribe to the TwainSession.Acquire event, and then initialize any image processing commands.
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());
Within Form1_FormClosed
, end the TWAIN session.
// End the twain session
_twainSession.Shutdown();
Finally add the code for the Twain acquire handle.
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);
}
}
To test this with the latest version of LEADTOOLS, download the free 60 day evaluation straight from our site. If you have any comments or questions regarding this, feel free to comment on this post or contact our Support department at support@leadtools.com.
References
-
^ "Incorporating Intelligent Capture in Your Digital TransformationStrategy", page 22
IIM Best Practices Report
AIIM (2019)