VintaSoft Barcode .NET SDK 12.4
In This Topic
    Read barcodes from image file
    In This Topic

    The SDK allows to read barcodes from image file or stream. Supported are the following image formats: BMP, GIF, JPEG, PNG, TIFF.

    Example: Here is an example that shows how to read barcodes from an image file.

    Public Shared Sub ReadBarcodesFromImage(filename 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)
    
            ' specify that reader must search for horizontal and vertical barcodes only
            reader.Settings.ScanDirection = ScanDirection.Horizontal Or ScanDirection.Vertical
    
            ' use Automatic Recognition
            reader.Settings.AutomaticRecognition = True
    
            ' read barcodes from image file
            Dim infos As IBarcodeInfo() = reader.ReadBarcodes(filename)
    
            ' if barcodes are not detected
            If infos.Length = 0 Then
                Console.WriteLine("No barcodes found.")
            Else
                ' if barcodes are detected
                ' get information about extracted barcodes
    
                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 + 1, info.BarcodeType))
                    Console.WriteLine(String.Format("Value:      {0}", info.Value))
                    Console.WriteLine(String.Format("Region:     {0}", info.Region))
                    Console.WriteLine()
                Next
            End If
        End Using
    End Sub
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Barcode
    
    /// <summary>
    /// Reads barcodes from an image file.
    /// </summary>
    /// <param name="filename">A path to an image file.</param>
    public static void ReadBarcodesFromImage(string filename)
    {
        // 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 file
            Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(filename);
    
            // if barcodes are not detected
            if (infos.Length == 0)
            {
                Console.WriteLine("No barcodes found.");
            }
            // if barcodes are detected
            else
            {
                // get information about extracted barcodes
    
                Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
                Console.WriteLine();
                for (int i = 0; i < infos.Length; i++)
                {
                    Vintasoft.Barcode.IBarcodeInfo info = infos[i];
                    Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
                    Console.WriteLine(string.Format("Value:      {0}", info.Value));
                    Console.WriteLine(string.Format("Region:     {0}", info.Region));
                    Console.WriteLine();
                }
            }
        }
    }