How SDK reads barcodes from color image
In This Topic
Color, palette or gray image (
the color image) is converted to black-white image before barcode reading.
Here is a list of steps performed by the image converting algorithm:
- The sum of color components (T) is calculated for each pixel by the following formula: T = R + G + B
-
Pixel color is changed to black color if T is less than Threshold value
-
Pixel color is changed to white color if T is more than or equal Threshold value
Automatic Recognition Mode
This mode allows to make barcode recognition process as simple as possible. You can set only
Expected barcodes and
Scan Interval values up.
Automatic recognition mode is preferable in case you read barcodes from images which threshold is hard to locate and when barcodes are damaged as the original image is of poor quality (contain noise, dimensional distortion etc). In certain cases this mode decrease the barcode recognition speed as the additional image manipulation, which is included in
Automatic recognition algorithm, takes some extra time.
Threshold Fine-Tuning Modes
Threshold can be:
You should use
ThresholdMode.Automatic mode if you read barcodes from many different images, e.g. images are acquired from different devices and in different light conditions.
You should use
ThresholdMode.Manual mode if you read barcodes from similar images, e.g. you use the best threshold which gives you the best recognition result for your barcode collection.
You should use
ThresholdMode.Iterations mode if you read barcodes from images where the appropriate threshold value is difficult-to-locate. By setting up the Max and Min threshold values and count of iteration steps you can get the barcode read from a rather large collection of different images.
Here is C#/VB.NET code that demonstrates how to detect barcodes in a color image with difficult-to-locate threshold.
/// <summary>
/// Reads barcodes from color image.
/// </summary>
/// <param name="filename">A path to an image file.</param>
/// <returns>A string with information about recognized barcodes.</returns>
public static string ReadBarcodesFromColorImage(string filename)
{
// create a BarcodeReader object
using (Vintasoft.Barcode.BarcodeReader barcodeReader = new Vintasoft.Barcode.BarcodeReader())
{
// specify that only Code 39, EAN, PDF417 and Micro PDF417 barcodes must be searched
barcodeReader.Settings.ScanBarcodeTypes =
Vintasoft.Barcode.BarcodeType.Code39 |
Vintasoft.Barcode.BarcodeType.EAN8 |
Vintasoft.Barcode.BarcodeType.EAN13 |
Vintasoft.Barcode.BarcodeType.PDF417 |
Vintasoft.Barcode.BarcodeType.MicroPDF417;
// specify that only image pixels of every 5 row and column must analyzed for barcode searching
barcodeReader.Settings.ScanInterval = 5;
// specify that barcode searching must be stopped when 10 barcodes are searched
barcodeReader.Settings.ExpectedBarcodes = 10;
// extract barcodes
Vintasoft.Barcode.IBarcodeInfo[] infos = barcodeReader.ReadBarcodes(filename);
// if barcodes are not detected
if (infos == null || infos.Length == 0)
return "Codes are not detected.";
// get information about extracted barcodes
System.Text.StringBuilder resultString = new System.Text.StringBuilder();
resultString.AppendLine(string.Format("Codes detected [{0}]:", infos.Length));
for (int i = 0; i < infos.Length; i++)
resultString.AppendLine(infos[i].ToString());
return resultString.ToString();
}
}
''' <summary>
''' Reads barcodes from color image.
''' </summary>
''' <param name="filename">A path to an image file.</param>
''' <returns>A string with information about recognized barcodes.</returns>
Public Shared Function ReadBarcodesFromColorImage(filename As String) As String
' create a BarcodeReader object
Using barcodeReader As New Vintasoft.Barcode.BarcodeReader()
' specify that only Code 39, EAN, PDF417 and Micro PDF417 barcodes must be searched
barcodeReader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Code39 Or Vintasoft.Barcode.BarcodeType.EAN8 Or Vintasoft.Barcode.BarcodeType.EAN13 Or Vintasoft.Barcode.BarcodeType.PDF417 Or Vintasoft.Barcode.BarcodeType.MicroPDF417
' specify that only image pixels of every 5 row and column must analyzed for barcode searching
barcodeReader.Settings.ScanInterval = 5
' specify that barcode searching must be stopped when 10 barcodes are searched
barcodeReader.Settings.ExpectedBarcodes = 10
' extract barcodes
Dim infos As Vintasoft.Barcode.IBarcodeInfo() = barcodeReader.ReadBarcodes(filename)
' if barcodes are not detected
If infos Is Nothing OrElse infos.Length = 0 Then
Return "Codes are not detected."
End If
' get information about extracted barcodes
Dim resultString As New System.Text.StringBuilder()
resultString.AppendLine(String.Format("Codes detected [{0}]:", infos.Length))
For i As Integer = 0 To infos.Length - 1
resultString.AppendLine(infos(i).ToString())
Next
Return resultString.ToString()
End Using
End Function