ReadBarcodes() Метод (BarcodeReader)
В этом разделе
Распознает штрих-коды в изображении, которое было указано ранее с помощью метода
SetImage(Stream).
Синтаксис
Return Value
Массив объектов
IBarcodeInfo, который содержит информацию о распознанных штрих-кодах.
Пример
Вот С#/VB.NET код, демонстрирующий, как обнаруживать штрих-коды на 2 изображениях.
Imports Vintasoft.Barcode
Imports Vintasoft.Barcode.SymbologySubsets
''' <summary>
''' Test that shows how to read barcodes from image,
''' the barcode reader searches barcodes using 2 different scan intervals.
''' </summary>
Class ReadBarcodesFromImageFileExample
''' <summary>
''' Runs the test.
''' </summary>
Public Shared Sub Test()
ReadBarcodes("test1.jpg", "test2.png")
End Sub
''' <summary>
''' Reads barcodes from images, which are stored in files.
''' Barcode reader will try to recognize barcodes using 2 different scan intervals.
''' </summary>
Private Shared Sub ReadBarcodes(ParamArray filenames As String())
' create barcode reader
Using reader As New BarcodeReader()
' specify that reader must search for Code39, Code39Extended,
' Code128, SSCC18 and DataMatrix barcodes
reader.Settings.ScanBarcodeTypes = BarcodeType.Code39 Or BarcodeType.Code128 Or BarcodeType.DataMatrix
reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended)
reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18)
' for each filename
For Each filename As String In filenames
Console.Write(String.Format("{0}: ", filename))
' set the reader image
reader.SetImage(filename)
' read barcodes from image, scan interval is set to 5
Dim infos As IBarcodeInfo() = ReadBarcodes(reader, 5)
' if barcodes are not found
If infos.Length = 0 Then
' read barcodes from image, scan interval is set to 1
infos = ReadBarcodes(reader, 1)
End If
' clear reader image
reader.ClearImage()
' show barcode recognition results
If infos.Length = 0 Then
Console.WriteLine("No barcodes found.")
Else
Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))
Console.WriteLine()
For i As Integer = 0 To infos.Length - 1
Dim info As IBarcodeInfo = infos(i)
Console.WriteLine(String.Format("[{0}:{1}]", i, info.BarcodeType))
Console.WriteLine(String.Format("Value: {0}", info.Value))
Console.WriteLine(String.Format("Confidence: {0}%", Math.Round(info.Confidence)))
Console.WriteLine(String.Format("Threshold: {0}", info.Threshold))
Console.WriteLine(String.Format("Region: {0}", info.Region))
Console.WriteLine()
Next
End If
Next
End Using
End Sub
''' <summary>
''' Reads barcodes from image, which is stored in barcode reader.
''' </summary>
Private Shared Function ReadBarcodes(reader As BarcodeReader, scanInterval As Integer) As IBarcodeInfo()
reader.Settings.ScanInterval = scanInterval
Return reader.ReadBarcodes()
End Function
End Class
using System;
using Vintasoft.Barcode;
using Vintasoft.Barcode.SymbologySubsets;
/// <summary>
/// Test that shows how to read barcodes from image,
/// the barcode reader searches barcodes using 2 different scan intervals.
/// </summary>
class ReadBarcodesFromImageFileExample
{
/// <summary>
/// Runs the test.
/// </summary>
public static void Test()
{
ReadBarcodes("test1.jpg", "test2.png");
}
/// <summary>
/// Reads barcodes from images, which are stored in files.
/// Barcode reader will try to recognize barcodes using 2 different scan intervals.
/// </summary>
static void ReadBarcodes(params string[] filenames)
{
// create barcode reader
using (BarcodeReader reader = new BarcodeReader())
{
// specify that reader must search for Code39, Code39Extended,
// Code128, SSCC18 and DataMatrix barcodes
reader.Settings.ScanBarcodeTypes =
BarcodeType.Code39 |
BarcodeType.Code128 |
BarcodeType.DataMatrix;
reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended);
reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18);
// for each filename
foreach (string filename in filenames)
{
Console.Write(string.Format("{0}: ", filename));
// set the reader image
reader.SetImage(filename);
// read barcodes from image, scan interval is set to 5
IBarcodeInfo[] infos = ReadBarcodes(reader, 5);
// if barcodes are not found
if (infos.Length == 0)
// read barcodes from image, scan interval is set to 1
infos = ReadBarcodes(reader, 1);
// clear reader image
reader.ClearImage();
// show barcode recognition results
if (infos.Length == 0)
{
Console.WriteLine("No barcodes found.");
}
else
{
Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
Console.WriteLine();
for (int i = 0; i < infos.Length; i++)
{
IBarcodeInfo info = infos[i];
Console.WriteLine(string.Format("[{0}:{1}]", i, info.BarcodeType));
Console.WriteLine(string.Format("Value: {0}", info.Value));
Console.WriteLine(string.Format("Confidence: {0}%", Math.Round(info.Confidence)));
Console.WriteLine(string.Format("Threshold: {0}", info.Threshold));
Console.WriteLine(string.Format("Region: {0}", info.Region));
Console.WriteLine();
}
}
}
}
}
/// <summary>
/// Reads barcodes from image, which is stored in barcode reader.
/// </summary>
private static IBarcodeInfo[] ReadBarcodes(BarcodeReader reader, int scanInterval)
{
reader.Settings.ScanInterval = scanInterval;
return reader.ReadBarcodes();
}
}
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также