Как распознать штрих-коды в цветном изображении плохого качества?
В этом разделе
Если у Вас есть цветное изображение плохого качества, то Вы можете выбрать один из двух путей:
- Можно использовать режим автоматического рапозавания (свойство Vintasoft.Barcode.ReaderSettings.AutomaticRecognition)
- Можно использовать итерационный процесс для определения цветового порога при котором штрих-код хорошо виден на изображении
Пример: Вот пример, который показывает, как распознать штрих-коды в цветном изображении с трудным для обнаружения цветовым порогом.
public static void TestScanWithIterationsCode128orDataMatrix(string fileName)
{
// recognize one Code 128 or DataMatrix barcode
ScanWithIterations(fileName, Vintasoft.Barcode.BarcodeType.Code128 | Vintasoft.Barcode.BarcodeType.DataMatrix, 1, 20, 300, 600);
}
public static void ScanWithIterations(
string fileName,
Vintasoft.Barcode.BarcodeType barcodes,
int expectedBarcodes,
int iterationCount,
int minThreshold,
int maxThreshold)
{
// create a BarcodeReader object
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// set barcode reader settings
reader.Settings.ScanBarcodeTypes = barcodes;
// read barcodes
Vintasoft.Barcode.IBarcodeInfo[] barcodesInfo = reader.ReadBarcodes(fileName);
// 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 i = 0; i < barcodesInfo.Length; i++)
{
Vintasoft.Barcode.IBarcodeInfo inf = barcodesInfo[i];
System.Console.WriteLine(string.Format("[{0}] {1} (Threshold: {2})", inf.BarcodeType,
inf.Value, inf.Threshold));
}
}
}
}
Public Shared Sub TestScanWithIterationsCode128orDataMatrix(fileName As String)
' recognize one Code 128 or DataMatrix barcode
ScanWithIterations(fileName, Vintasoft.Barcode.BarcodeType.Code128 Or Vintasoft.Barcode.BarcodeType.DataMatrix, 1, 20, 300, 600)
End Sub
Public Shared Sub ScanWithIterations(fileName As String, barcodes As Vintasoft.Barcode.BarcodeType, expectedBarcodes As Integer, iterationCount As Integer, minThreshold As Integer, maxThreshold As Integer)
' create a BarcodeReader object
Using reader As New Vintasoft.Barcode.BarcodeReader()
' set barcode reader settings
reader.Settings.ScanBarcodeTypes = barcodes
' read barcodes
Dim barcodesInfo As Vintasoft.Barcode.IBarcodeInfo() = reader.ReadBarcodes(fileName)
' 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 i As Integer = 0 To barcodesInfo.Length - 1
Dim inf As Vintasoft.Barcode.IBarcodeInfo = barcodesInfo(i)
System.Console.WriteLine(String.Format("[{0}] {1} (Threshold: {2})", inf.BarcodeType, inf.Value, inf.Threshold))
Next
End If
End Using
End Sub