VintaSoft Barcode .NET SDK 15.0: Руководство для .NET разработчика
В этом разделе
    Распознавание штрих-кодов в изображении из файла
    В этом разделе
    SDK позволяет загружать растровое изображение из файла или потока и распознавать штрих-коды в загруженном растровом изображении.


    Vintasoft.Barcode.dll позволяет загружать растровые изображения из файлов BMP, JPEG, PNG, используя встроенные кодеки изображений.

    Vintasoft.Barcode.dll + Vintasoft.Barcode.Gdi.dll позволяет загружать растровые изображения из файлов BMP, GIF, JPEG, PNG, TIFF с использованием кодеков изображений из библиотеки System.Drawing.

    Vintasoft.Barcode.dll + Vintasoft.Barcode.Wpf.dll позволяет загружать растровые изображения из файлов BMP, GIF, JPEG, PNG, TIFF с использованием кодеков изображений из WPF.

    Vintasoft.Barcode.dll + Vintasoft.Barcode.ImageSharp.dll allows позволяет загружать растровые изображения из файлов BMP, JPEG, PBM, PNG, TGA, TIFF, WEBP с использованием кодеков изображений из библиотеки SixLabors.ImageSharp.

    Vintasoft.Barcode.dll + Vintasoft.Barcode.SkiaSharp.dll позволяет загружать растровые изображения из файлов BMP, DNG, GIF, HEIF, JPEG, PNG, WEBP с использованием кодеков изображений из библиотеки SkiaSharp.

    Пример: Вот пример, который показывает, как распознать штрих-коды из файла изображения.
    /// <summary>
    /// Allows to read barcodes from image file.
    /// </summary>
    public static class ImageFileBarcodeReader
    {
        /// <summary>
        /// Initializes the <see cref="ImageFileBarcodeReader"/> class.
        /// </summary>
        static ImageFileBarcodeReader()
        {
            // Vintasoft.Barcode.dll allows to load bitmap from BMP, JPEG, PNG files using built-in image codecs
            
            // Uncomment initialization of platform extension below, if necessary:
    
            // Vintasoft.Barcode.Gdi.dll allows to load bitmap from BMP, GIF, JPEG, PNG, TIFF files using image codecs from System.Drawing library
            //Vintasoft.Barcode.GdiAssembly.Init();
    
            // Vintasoft.Barcode.Wpf.dll allows to load bitmap from BMP, GIF, JPEG, PNG, TIFF files using image codecs from WPF
            //Vintasoft.Barcode.WpfAssembly.Init();
    
            // Vintasoft.Barcode.ImageSharp.dll allows to load bitmap from BMP, JPEG, PBM, PNG, TGA, TIFF, WEBP files using image codecs from SixLabors.ImageSharp library
            //Vintasoft.Barcode.ImageSharpAssembly.Init();
    
            // Vintasoft.Barcode.SkiaSharp.dll allows to load bitmap from BMP, DNG, GIF, HEIF, JPEG, PNG, WEBP files using image codecs from SkiaSharp library
            //Vintasoft.Barcode.SkiaSharpAssembly.Init();
        }
    
        /// <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();
                    }
                }
            }
        }
    }
    
    ''' <summary>
    ''' Allows to read barcodes from image file.
    ''' </summary>
    Public NotInheritable Class ImageFileBarcodeReader
        Private Sub New()
        End Sub
    
        ''' <summary>
        ''' Initializes the <see cref="ImageFileBarcodeReader"/> class.
        ''' </summary>
                ' Vintasoft.Barcode.dll allows to load bitmap from BMP, JPEG, PNG files using built-in image codecs
    
                ' Uncomment initialization of platform extension below, if necessary:
    
                ' Vintasoft.Barcode.Gdi.dll allows to load bitmap from BMP, GIF, JPEG, PNG, TIFF files using image codecs from System.Drawing library
                'Vintasoft.Barcode.GdiAssembly.Init();
    
                ' Vintasoft.Barcode.Wpf.dll allows to load bitmap from BMP, GIF, JPEG, PNG, TIFF files using image codecs from WPF
                'Vintasoft.Barcode.WpfAssembly.Init();
    
                ' Vintasoft.Barcode.ImageSharp.dll allows to load bitmap from BMP, JPEG, PBM, PNG, TGA, TIFF, WEBP files using image codecs from SixLabors.ImageSharp library
                'Vintasoft.Barcode.ImageSharpAssembly.Init();
    
                ' Vintasoft.Barcode.SkiaSharp.dll allows to load bitmap from BMP, DNG, GIF, HEIF, JPEG, PNG, WEBP files using image codecs from SkiaSharp library
                'Vintasoft.Barcode.SkiaSharpAssembly.Init();
        Shared Sub New()
        End Sub
    
        ''' <summary>
        ''' Reads barcodes from an image file.
        ''' </summary>
        ''' <param name="filename">A path to an image file.</param>
        Public Shared Sub ReadBarcodesFromImage(filename As String)
            ' create barcode reader
            Using reader As New Vintasoft.Barcode.BarcodeReader()
                ' specify that reader must search for Code39, Code39Extended,
                ' Code128, SSCC18 and DataMatrix barcodes
                reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Code39 Or Vintasoft.Barcode.BarcodeType.Code128 Or 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 Or Vintasoft.Barcode.ScanDirection.Vertical
    
                ' use Automatic Recognition
                reader.Settings.AutomaticRecognition = True
    
                ' read barcodes from image file
                Dim infos As Vintasoft.Barcode.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 Vintasoft.Barcode.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
    End Class