Распознавание штрих-кодов в многостраничном TIFF файле
В этом разделе
Пример: Вот пример, который показывает, как загрузить растровые изображения из многостраничного файла TIFF с помощью библиотеки System.Drawing и распознавать штрих-коды в загруженных растровых изображениях.
/// <summary>
/// Reads barcodes from a multipage TIFF file.
/// </summary>
/// <param name="filename">A path to a multipage TIFF file.</param>
/// <param name="barcodeType">A type of barcodes, which must be recognized.</param>
public static void ReadBarcodesFromMiltipageTiffFile(string filename, Vintasoft.Barcode.BarcodeType barcodeType)
{
// open TIFF file
using (System.Drawing.Image tiffImage = System.Drawing.Image.FromFile(filename))
{
System.Guid index = tiffImage.FrameDimensionsList[0];
System.Drawing.Imaging.FrameDimension dimension = new System.Drawing.Imaging.FrameDimension(index);
int pageCount = tiffImage.GetFrameCount(dimension);
// create the barcode reader
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// set barcode reader settings
reader.Settings.ScanBarcodeTypes = barcodeType;
// read barcodes from all pages
for (int i = 0; i < pageCount; i++)
{
// select a page
tiffImage.SelectActiveFrame(dimension, i);
// read barcodes
Vintasoft.Barcode.IBarcodeInfo[] barcodesInfo = Vintasoft.Barcode.GdiExtensions.ReadBarcodes(reader, tiffImage);
// output information about barcodes
System.Console.WriteLine(string.Format("Page {0}: ", i));
// if barcodes are not detected
if (barcodesInfo.Length == 0)
{
System.Console.WriteLine("No barcodes found.");
}
// if barcodes are detected
else
{
// get information about extracted barcodes
for (int j = 0; j < barcodesInfo.Length; j++)
System.Console.WriteLine(string.Format("[{0}] {1}", barcodesInfo[j].BarcodeType,
barcodesInfo[j].Value));
}
}
}
}
}
''' <summary>
''' Reads barcodes from a multipage TIFF file.
''' </summary>
''' <param name="filename">A path to a multipage TIFF file.</param>
''' <param name="barcodeType">A type of barcodes, which must be recognized.</param>
Public Shared Sub ReadBarcodesFromMiltipageTiffFile(filename As String, barcodeType As Vintasoft.Barcode.BarcodeType)
' open TIFF file
Using tiffImage As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
Dim index As System.Guid = tiffImage.FrameDimensionsList(0)
Dim dimension As New System.Drawing.Imaging.FrameDimension(index)
Dim pageCount As Integer = tiffImage.GetFrameCount(dimension)
' create the barcode reader
Using reader As New Vintasoft.Barcode.BarcodeReader()
' set barcode reader settings
reader.Settings.ScanBarcodeTypes = barcodeType
' read barcodes from all pages
For i As Integer = 0 To pageCount - 1
' select a page
tiffImage.SelectActiveFrame(dimension, i)
' read barcodes
Dim barcodesInfo As Vintasoft.Barcode.IBarcodeInfo() = Vintasoft.Barcode.GdiExtensions.ReadBarcodes(reader, tiffImage)
' output information about barcodes
System.Console.WriteLine(String.Format("Page {0}: ", i))
' if barcodes are not detected
If barcodesInfo.Length = 0 Then
System.Console.WriteLine("No barcodes found.")
Else
' if barcodes are detected
' get information about extracted barcodes
For j As Integer = 0 To barcodesInfo.Length - 1
System.Console.WriteLine(String.Format("[{0}] {1}", barcodesInfo(j).BarcodeType, barcodesInfo(j).Value))
Next
End If
Next
End Using
End Using
End Sub