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


    Распознавание штрих-кодов в изображении-ресурсе PDF документа

    Для распознавания штрих-кодов в изображениях-ресурсах PDF документа необходимо извлечь изображения-ресурсы из PDF документа и распознать штрих-коды в извлеченных изображениях.

    VintaSoft Barcode .NET SDK предоставляет класс Vintasoft.Barcode.PdfImageViewer, который позволяет извлекать изображения-ресурсы из PDF документа. Класс может извлечь изображение-ресурс из PDF документа, если изображение-ресурс сжат с помощью алгоритма компрессии ZIP, JPEG, CCITT3, CCITT4, LZW, Run Length и данные изображения-ресурса хранятся в цветовом пространстве DeviceGray, DeviceRGB, DeviceCMYK или Indexed.

    VintaSoft Imaging .NET SDK с Плагинами позволяет извлекать любые (любое сжатие данных и любое цветовое пространство для данных изображения) изображения-ресурсы из PDF документа.


    Пример: Вот пример, который показывает, как распознать штрих-коды в изображениях-ресурсах PDF документа с помощью класса Vintasoft.Barcode.PdfImageViewer из VintaSoft Barcode .NET SDK:
    /// <summary>
    /// Reads barcodes from image-resources of PDF document.
    /// </summary>
    /// <param name="fileName">A path to a PDF document.</param>
    /// <param name="barcodeTypes">The types of barcodes, which must be recognized.</param>
    static void ReadBarcodesFromPdfDocument(string fileName, Vintasoft.Barcode.BarcodeType barcodeTypes)
    {
        // create the PdfImageViewer object and open PDF document
        using (Vintasoft.Barcode.PdfImageViewer pdfImageViewer = new Vintasoft.Barcode.PdfImageViewer(fileName))
        {
            // create the barcode reader
            using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
            {
                // set barcode reader settings
                reader.Settings.ScanBarcodeTypes = barcodeTypes;
    
                // for each page of PDF document
                for (int i = 0; i < pdfImageViewer.PageCount; i++)
                {
                    // get names of all image resources of page i
                    string[] imageNames = pdfImageViewer.GetImageNames(i);
                    // for each image resource of page i
                    for (int k = 0; k < imageNames.Length; k++)
                    {
                        System.Console.WriteLine(string.Format("Page {0}, image {1}: ", i, imageNames[k]));
                        Vintasoft.Imaging.VintasoftBitmap bitmap;
                        // get image of image resource
                        try
                        {
                            bitmap = pdfImageViewer.GetImage(i, imageNames[k]);
                        }
                        catch (System.Exception ex)
                        {
                            // not supported image format
                            System.Console.WriteLine(ex.Message);
                            continue;
                        }
    
                        // read barcodes from image
                        Vintasoft.Barcode.IBarcodeInfo[] barcodesInfo = reader.ReadBarcodes(bitmap);
    
                        // free image resource
                        bitmap.Dispose();
    
                        // output information about barcodes
                        if (barcodesInfo.Length == 0)
                        {
                            System.Console.WriteLine("No barcodes found.");
                        }
                        else
                        {
                            for (int j = 0; j < barcodesInfo.Length; j++)
                                System.Console.WriteLine(string.Format("[{0}] {1}", barcodesInfo[j].BarcodeType,
                                                                             barcodesInfo[j].Value));
                        }
                    }
                }
            }
        }
    }
    
    ''' <summary>
    ''' Reads barcodes from image-resources of PDF document.
    ''' </summary>
    ''' <param name="fileName">A path to a PDF document.</param>
    ''' <param name="barcodeTypes">The types of barcodes, which must be recognized.</param>
    Private Shared Sub ReadBarcodesFromPdfDocument(fileName As String, barcodeTypes As Vintasoft.Barcode.BarcodeType)
        ' create the PdfImageViewer object and open PDF document
        Using pdfImageViewer As New Vintasoft.Barcode.PdfImageViewer(fileName)
            ' create the barcode reader
            Using reader As New Vintasoft.Barcode.BarcodeReader()
                ' set barcode reader settings
                reader.Settings.ScanBarcodeTypes = barcodeTypes
    
                ' for each page of PDF document
                For i As Integer = 0 To pdfImageViewer.PageCount - 1
                    ' get names of all image resources of page i
                    Dim imageNames As String() = pdfImageViewer.GetImageNames(i)
                    ' for each image resource of page i
                    For k As Integer = 0 To imageNames.Length - 1
                        System.Console.WriteLine(String.Format("Page {0}, image {1}: ", i, imageNames(k)))
                        Dim barcodeImage As Vintasoft.Imaging.VintasoftBitmap
                        ' get image of image resource
                        Try
                            barcodeImage = pdfImageViewer.GetImage(i, imageNames(k))
                        Catch ex As System.Exception
                            ' not supported image format
                            System.Console.WriteLine(ex.Message)
                            Continue For
                        End Try
    
                        ' read barcodes from image
                        Dim barcodesInfo As Vintasoft.Barcode.IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)
    
                        ' free image resource
                        barcodeImage.Dispose()
    
                        ' output information about barcodes
                        If barcodesInfo.Length = 0 Then
                            System.Console.WriteLine("No barcodes found.")
                        Else
                            For j As Integer = 0 To barcodesInfo.Length - 1
                                System.Console.WriteLine(String.Format("[{0}] {1}", barcodesInfo(j).BarcodeType, barcodesInfo(j).Value))
                            Next
                        End If
                    Next
                Next
            End Using
        End Using
    End Sub
    



    Пример: Вот пример, который показывает, как распознать штрих-коды в изображениях-ресурсах PDF документа с помощью VintaSoft Imaging .NET SDK:
    /// <summary>
    /// Reads barcodes from image-resources of PDF document.
    /// </summary>
    /// <param name="pdfFilename">A path to a PDF document.</param>
    public static void ReadBarcodesFromPdfDocumentResources(string pdfFilename)
    {
        // open PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
        {
            // for each PDF page
            for (int i = 0; i < document.Pages.Count; i++)
            {
                // get image resources of PDF page
                Vintasoft.Imaging.Pdf.Tree.PdfImageResource[] imageResources = document.Pages[i].GetImages();
    
                // foreach image resources
                for (int j = 0; j < imageResources.Length; j++)
                {
                    // get image resource
                    using (Vintasoft.Imaging.VintasoftImage imageResource = imageResources[j].GetImage())
                    {
                        // get image of image resource
                        using (Vintasoft.Imaging.VintasoftBitmap bitmap = imageResource.GetAsVintasoftBitmap())
                        {
                            System.Console.WriteLine(string.Format("[Page {0}, resource {1}]", i + 1, j + 1));
                            // read barcodes from image
                            ReadBarcodesFromImage(bitmap);
                        }
                    }
                }
            }
        }
    }
    
    /// <summary>
    /// Reads barcodes from an image.
    /// </summary>
    /// <param name="barcodeImage">An image with barcodes.</param>
    public static void ReadBarcodesFromImage(Vintasoft.Imaging.VintasoftBitmap barcodeImage)
    {
        // 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;
    
            // expected one barcode
            reader.Settings.ExpectedBarcodes = 1;
    
            // scan interval - 5 pixels
            reader.Settings.ScanInterval = 5;
    
            // read barcodes from image
            Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage);
    
            // if barcodes are not detected
            if (infos.Length == 0)
            {
                System.Console.WriteLine("No barcodes found.");
            }
            // if barcodes are detected
            else
            {
                // get information about extracted barcodes
    
                System.Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
                System.Console.WriteLine();
                for (int i = 0; i < infos.Length; i++)
                {
                    Vintasoft.Barcode.IBarcodeInfo info = infos[i];
                    System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
                    System.Console.WriteLine(string.Format("Value:      {0}", info.Value));
                    System.Console.WriteLine(string.Format("Region:     {0}", info.Region));
                    System.Console.WriteLine();
                }
            }
        }
    }
    
    ''' <summary>
    ''' Reads barcodes from image-resources of PDF document.
    ''' </summary>
    ''' <param name="pdfFilename">A path to a PDF document.</param>
    Public Shared Sub ReadBarcodesFromPdfDocumentResources(pdfFilename As String)
        ' open PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
            ' for each PDF page
            For i As Integer = 0 To document.Pages.Count - 1
                ' get image resources of PDF page
                Dim imageResources As Vintasoft.Imaging.Pdf.Tree.PdfImageResource() = document.Pages(i).GetImages()
    
                ' foreach image resources
                For j As Integer = 0 To imageResources.Length - 1
                    ' get image resource
                    Using imageResource As Vintasoft.Imaging.VintasoftImage = imageResources(j).GetImage()
                        ' get image of image resource
                        Using bitmap As Vintasoft.Imaging.VintasoftBitmap = imageResource.GetAsVintasoftBitmap()
                            System.Console.WriteLine(String.Format("[Page {0}, resource {1}]", i + 1, j + 1))
                            ' read barcodes from image
                            ReadBarcodesFromImage(bitmap)
                        End Using
                    End Using
                Next
            Next
        End Using
    End Sub
    
    ''' <summary>
    ''' Reads barcodes from an image.
    ''' </summary>
    ''' <param name="barcodeImage">An image with barcodes.</param>
    Public Shared Sub ReadBarcodesFromImage(barcodeImage As Vintasoft.Imaging.VintasoftBitmap)
        ' 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
    
            ' expected one barcode
            reader.Settings.ExpectedBarcodes = 1
    
            ' scan interval - 5 pixels
            reader.Settings.ScanInterval = 5
    
            ' read barcodes from image
            Dim infos As Vintasoft.Barcode.IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)
    
            ' if barcodes are not detected
            If infos.Length = 0 Then
                System.Console.WriteLine("No barcodes found.")
            Else
                ' if barcodes are detected
                ' get information about extracted barcodes
    
                System.Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))
                System.Console.WriteLine()
                For i As Integer = 0 To infos.Length - 1
                    Dim info As Vintasoft.Barcode.IBarcodeInfo = infos(i)
                    System.Console.WriteLine(String.Format("[{0}:{1}]", i + 1, info.BarcodeType))
                    System.Console.WriteLine(String.Format("Value:      {0}", info.Value))
                    System.Console.WriteLine(String.Format("Region:     {0}", info.Region))
                    System.Console.WriteLine()
                Next
            End If
        End Using
    End Sub
    



    Распознавание штрих-кодов в отрендеренном изображении PDF страницы

    Для распознавания штрих-кодов в векторном контенте необходимо отрендерить PDF страницу и распознать штрих-коды в отрендеренном изображении.

    PDF страница может быть отрендерена используя VintaSoft Imaging .NET SDK с Плагинами или с помощью любого другого стороннего программного обеспечения.


    Пример: Вот пример, который показывает, как распознать штрих-коды в отрендеренной странице PDF документа используя VintaSoft Imaging .NET SDK:
    /// <summary>
    /// Reads barcodes from a vector PDF document.
    /// </summary>
    /// <param name="pdfFilename">A path to a PDF document.</param>
    public static void ReadBarcodesFromVectorPDFDocument(string pdfFilename)
    {
        // create the image collection
        using (Vintasoft.Imaging.ImageCollection pdfPages = new Vintasoft.Imaging.ImageCollection())
        {
            // add PDF document to the image collection
            pdfPages.Add(pdfFilename);
    
            // set the rendering settings to 200DPI, if necessary
            pdfPages.SetRenderingSettings(new Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(new Vintasoft.Imaging.Resolution(200, 200)));
    
            // for each PDF page
            foreach (Vintasoft.Imaging.VintasoftImage image in pdfPages)
            {
                // get page image
                using (Vintasoft.Imaging.VintasoftBitmap pageImage = image.GetAsVintasoftBitmap())
                {
                    // read barcodes from image
                    ReadBarcodesFromImage(pageImage);
                }
            }
    
            // clear image collection
            pdfPages.ClearAndDisposeItems();
        }
    }
    
    /// <summary>
    /// Reads barcodes from an image.
    /// </summary>
    /// <param name="barcodeImage">An image with barcodes.</param>
    public static void ReadBarcodesFromImage(Vintasoft.Imaging.VintasoftBitmap barcodeImage)
    {
        // create barcode reader
        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
        Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage);
    
        // if barcodes are not detected
        if (infos.Length == 0)
        {
            System.Console.WriteLine("No barcodes found.");
        }
        // if barcodes are detected
        else
        {
            // get information about extracted barcodes
    
            System.Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
            System.Console.WriteLine();
            for (int i = 0; i < infos.Length; i++)
            {
                Vintasoft.Barcode.IBarcodeInfo info = infos[i];
                System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
                System.Console.WriteLine(string.Format("Value:      {0}", info.Value));
                System.Console.WriteLine(string.Format("Region:     {0}", info.Region));
                System.Console.WriteLine();
            }
        }
    }
    
    ''' <summary>
    ''' Reads barcodes from a vector PDF document.
    ''' </summary>
    ''' <param name="pdfFilename">A path to a PDF document.</param>
    Public Shared Sub ReadBarcodesFromVectorPDFDocument(pdfFilename As String)
        ' create the image collection
        Using pdfPages As New Vintasoft.Imaging.ImageCollection()
            ' add PDF document to the image collection
            pdfPages.Add(pdfFilename)
    
            ' set the rendering settings to 200DPI, if necessary
            pdfPages.SetRenderingSettings(New Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(New Vintasoft.Imaging.Resolution(200, 200)))
    
            ' for each PDF page
            For Each image As Vintasoft.Imaging.VintasoftImage In pdfPages
                ' get page image
                Using pageImage As Vintasoft.Imaging.VintasoftBitmap = image.GetAsVintasoftBitmap()
                    ' read barcodes from image
                    ReadBarcodesFromImage(pageImage)
                End Using
            Next
    
            ' clear image collection
            pdfPages.ClearAndDisposeItems()
        End Using
    End Sub
    
    ''' <summary>
    ''' Reads barcodes from an image.
    ''' </summary>
    ''' <param name="barcodeImage">An image with barcodes.</param>
    Public Shared Sub ReadBarcodesFromImage(barcodeImage As Vintasoft.Imaging.VintasoftBitmap)
        ' create barcode reader
        Dim 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
        Dim infos As Vintasoft.Barcode.IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)
    
        ' if barcodes are not detected
        If infos.Length = 0 Then
            System.Console.WriteLine("No barcodes found.")
        Else
            ' if barcodes are detected
            ' get information about extracted barcodes
    
            System.Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))
            System.Console.WriteLine()
            For i As Integer = 0 To infos.Length - 1
                Dim info As Vintasoft.Barcode.IBarcodeInfo = infos(i)
                System.Console.WriteLine(String.Format("[{0}:{1}]", i + 1, info.BarcodeType))
                System.Console.WriteLine(String.Format("Value:      {0}", info.Value))
                System.Console.WriteLine(String.Format("Region:     {0}", info.Region))
                System.Console.WriteLine()
            Next
        End If
    End Sub