Read Barcodes from Multipage TIFF Images in C#
Overview
It is often required to read barcodes from multi-page TIFF files. TIFF is a common raster graphics format that allows storing several high-quality images in one document. This format is widely used to save data during automatic scanning. Aspose.BarCode enables recognizing barcodes from TIFF documents but it can only read from the first page (or “frame”). To detect barcodes in other frames, it is necessary to process them using side libraries first and then pass the obtained raster images to class BarCodeReader. The recognition process includes the following steps:
- Open the source multi-page TIFF file
- Selected the requested frame that often represents a page of the scanned document
- Read barcodes from the frame
- Repeat Steps 2 and 3 until all target pages are processed
Read Barcodes from Multi-page TIFF Document
Further in the article, you can find code examples for extracting frames from a document scanned in the TIFF format and recognizing barcodes in those frames. The sample source TIFF file can be viewed here.
Using Aspose.Imaging
This example explains how to read barcodes from multi-page TIFF documents using the Aspose.Imaging library that is fully cross-platform and the most convenient way to implement such tasks. The barcode recognition process comprises the following steps:
- Open the source multi-page TIFF document using TiffImage
- Select the required page using TiffFrame
- Process the obtained image through class BarCodeReader to read barcodes
- Repeat Steps 2 and 3 until all target pages are processed
The following code snippet illustrates how to work with this approach.
//open the required image
using (Aspose.Imaging.Image openImage = Aspose.Imaging.Image.Load($"{path}MultiPageTiffWithBarcodes.tiff"))
{
//cast it as a multi-page TIFF file
Aspose.Imaging.FileFormats.Tiff.TiffImage tiffImage = openImage as Aspose.Imaging.FileFormats.Tiff.TiffImage;
if (null == tiffImage) return;
//process each TIFF frame
foreach(Aspose.Imaging.FileFormats.Tiff.TiffFrame tiffFrame in tiffImage.Frames)
{
//save the TIFF frame to the stream as PNG
MemoryStream ms = new MemoryStream();
tiffFrame.Save(ms, new Aspose.Imaging.ImageOptions.PngOptions());
ms.Position = 0;
//recognize PDF417, QR Code, Data Matrix, and Aztec barcode types from the rendered image of the page
BarCodeReader reader = new BarCodeReader(ms, DecodeType.Pdf417, DecodeType.QR, DecodeType.DataMatrix, DecodeType.Aztec);
foreach (BarCodeResult result in reader.ReadBarCodes())
Console.WriteLine($"Barcode type:{result.CodeTypeName}, Barcode Data:{result.CodeText}");
}
}
Using System.Drawing
It is possible to use the standard library called System.Drawing to recognize barcodes from multi-page TIFF files. This library cannot be considered fully cross-platform but works efficiently on Windows-based systems. Barcode recognition includes the following steps:
- Open the source TIFF document using class Image
- Select the required page using SelectActiveFrame
- Process the obtained image using class BarCodeReader to detect and recognize barcodes
- Repeat Steps 2 and 3 until all target pages are processed
The following code sample explains how to implement this option.
//open the required TIFF image
using (System.Drawing.Image tiffImage = System.Drawing.Image.FromFile($"{path}MultiPageTiffWithBarcodes.tiff"))
{
//get the frame count
int tiffFrameCount = tiffImage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
//process each tiff frame
for (int i = 0; i < tiffFrameCount; i++)
{
//select an active frame
tiffImage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i);
//save the TIFF frame to the stream as PNG
MemoryStream ms = new MemoryStream();
tiffImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
//recognize PDF417, QR Code, Data Matrix, and Aztec barcode types from the rendered image of the page
BarCodeReader reader = new BarCodeReader(ms, DecodeType.Pdf417, DecodeType.QR, DecodeType.DataMatrix, DecodeType.Aztec);
foreach (BarCodeResult result in reader.ReadBarCodes())
Console.WriteLine($"Barcode type:{result.CodeTypeName}, Barcode Data:{result.CodeText}");
}
}