Распознавание штрих-кодов в объекте SixLabors.ImageSharp.Image
В этом разделе
Пример: Вот пример, который показывает, как распознавать штрих-коды в изображении, которое представлено объектом SixLabors.ImageSharp.Image.
/// <summary>
/// Reads barcode from a <see cref="SixLabors.ImageSharp.Image"/>.
/// </summary>
/// <param name="bitmap">A bitmap with barcodes.</param>
public static void ReadBarcodesFromBitmap(SixLabors.ImageSharp.Image bitmap)
{
// create barcode reader
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// specify that reader must search for Code39, Code39Extended,
// Code128, SSCC18 and DataMatrix barcodes
reader.Settings.ScanBarcodeTypes =
Vintasoft.Barcode.BarcodeType.Code39 |
Vintasoft.Barcode.BarcodeType.Code128 |
Vintasoft.Barcode.BarcodeType.DataMatrix;
reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.Code39Extended);
reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.SSCC18);
// specify that reader must search for horizontal and vertical barcodes only
reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal | Vintasoft.Barcode.ScanDirection.Vertical;
// use Automatic Recognition
reader.Settings.AutomaticRecognition = true;
// read barcodes from image
Vintasoft.Barcode.IBarcodeInfo[] infos = Vintasoft.Barcode.ImageSharpExtensions.ReadBarcodes(reader, bitmap);
// if barcodes are not detected
if (infos.Length == 0)
{
System.Console.WriteLine("No barcodes found.");
}
// if barcodes are detected
else
{
// get information about extracted barcodes
System.Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
System.Console.WriteLine();
for (int i = 0; i < infos.Length; i++)
{
Vintasoft.Barcode.IBarcodeInfo info = infos[i];
System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
System.Console.WriteLine(string.Format("Value: {0}", info.Value));
System.Console.WriteLine(string.Format("Region: {0}", info.Region));
System.Console.WriteLine();
}
}
}
}
''' <summary>
''' Reads barcode from a <see cref="SixLabors.ImageSharp.Image"/>.
''' </summary>
''' <param name="bitmap">A bitmap with barcodes.</param>
Public Shared Sub ReadBarcodesFromBitmap(bitmap As SixLabors.ImageSharp.Image)
' create barcode reader
Using reader As New Vintasoft.Barcode.BarcodeReader()
' specify that reader must search for Code39, Code39Extended,
' Code128, SSCC18 and DataMatrix barcodes
reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Code39 Or Vintasoft.Barcode.BarcodeType.Code128 Or Vintasoft.Barcode.BarcodeType.DataMatrix
reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.Code39Extended)
reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.SSCC18)
' specify that reader must search for horizontal and vertical barcodes only
reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal Or Vintasoft.Barcode.ScanDirection.Vertical
' use Automatic Recognition
reader.Settings.AutomaticRecognition = True
' read barcodes from image
Dim infos As Vintasoft.Barcode.IBarcodeInfo() = Vintasoft.Barcode.ImageSharpExtensions.ReadBarcodes(reader, bitmap)
' if barcodes are not detected
If infos.Length = 0 Then
System.Console.WriteLine("No barcodes found.")
Else
' if barcodes are detected
' get information about extracted barcodes
System.Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))
System.Console.WriteLine()
For i As Integer = 0 To infos.Length - 1
Dim info As Vintasoft.Barcode.IBarcodeInfo = infos(i)
System.Console.WriteLine(String.Format("[{0}:{1}]", i + 1, info.BarcodeType))
System.Console.WriteLine(String.Format("Value: {0}", info.Value))
System.Console.WriteLine(String.Format("Region: {0}", info.Region))
System.Console.WriteLine()
Next
End If
End Using
End Sub