How to read barcodes from color image with poor quality?
In This Topic
There are 2 ways possible:
Here is C#/VB.NET code that demonstrates how to detect barcodes in a color image with difficult-to-locate threshold.
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