VintaSoft Barcode .NET SDK 15.1: Документация для Веб разработчика
В этом разделе
    Классы JavaScript для распознавания штрих-кодов
    В этом разделе
    Файл Vintasoft.Barcode.js содержит классы, которые позволяют распознавать штрих-коды из изображения либо image-ресурса PDF-документа:
    Файл Vintasoft.Barcode.d.ts является модулем TypeScript для файла Vintasoft.Barcode.js и содержит определения классов и перечислений для генерации штрих-кодов на TypeScript.


    Важно: Файл Vintasoft.Barcode.js содержит ссылку на файл Vintasoft.Shared.js.
    Важно: Файл Vintasoft.Barcode.d.ts должен использоваться вместе с файлами Vintasoft.Barcode.js, Vintasoft.Shared.js и Vintasoft.Shared.d.ts.


    Для распознавания штрих-кодов необходимо:

    Пример: Вот код JavaScript, который демонстрирует, как распознавать штрих-коды Code39 из изображения:
    <script type="text/javascript">
    
        /**
         * Barcodes are recognized successfully.
         */
        function __readBarcodes_success(data) {
            if (data.success) {
                // get the barcode recognition result
                var barcodeRecognitionResults = data.results;
    
                var htmlMarkup = '';
                // if no barcodes found
                if (barcodeRecognitionResults.length == 0) {
                    htmlMarkup = 'No barcodes found.';
                }
                // if barcodes are found
                else {
                    htmlMarkup = barcodeRecognitionResults.length.toString() + ' barcodes are found.<br />';
                    htmlMarkup += '<br />';
    
                    // for each recognized barcode
                    for (var i = 0; i < barcodeRecognitionResults.length; i++) {
                        // get the barcode recognition result
                        var barcodeRecognitionResult = barcodeRecognitionResults[i];
    
                        // output information about recognized barcode
                        htmlMarkup += '[' + (i + 1) + ':' + barcodeRecognitionResult.barcodeType + ']<br />';
                        htmlMarkup += '  Value: ' + barcodeRecognitionResult.value + '<br />';
                        htmlMarkup += '  Confidence: ' + barcodeRecognitionResult.confidence + '<br />';
                        htmlMarkup += '  Reading quality: ' + barcodeRecognitionResult.readingQuality.toFixed(2) + '<br />';
                        htmlMarkup += '  Threshold: ' + barcodeRecognitionResult.threshold + '<br />';
                        htmlMarkup += '  Region: ' +
                            'LT=(' + barcodeRecognitionResult.region.leftTop.x + ',' + barcodeRecognitionResult.region.leftTop.y + '); ' +
                            'RT=(' + barcodeRecognitionResult.region.rightTop.x + ',' + barcodeRecognitionResult.region.rightTop.y + '); ' +
                            'LB=(' + barcodeRecognitionResult.region.leftBottom.x + ',' + barcodeRecognitionResult.region.leftBottom.y + '); ' +
                            'RB=(' + barcodeRecognitionResult.region.rightBottom.x + ',' + barcodeRecognitionResult.region.rightBottom.y + '); ' +
                            'Angle=' + barcodeRecognitionResult.region.angle.toFixed(1) + '°<br />';
    
                        htmlMarkup += '<br />';
                    }
                }
    
                var barcodeInformationElement = document.getElementById("barcodeInformation");
                barcodeInformationElement.innerHTML = htmlMarkup;
            }
        }
    
        /**
         * Barcode recognition is failed.
         */
        function __readBarcodes_fail(data) {
            // show information about error
            alert(data.errorMessage);
        }
    
    
        // set the session identifier
        Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
    
        // create service that allows to recognize barcodes
        var barcodeService = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
    
        // create the barcode reader
        var barcodeReader = new Vintasoft.Barcode.WebBarcodeReaderJS(barcodeService);
        // specify that Code39 barcode must be searched
        barcodeReader.get_Settings().set_BarcodeType(new Vintasoft.Barcode.WebBarcodeTypeEnumJS("Code39"));
    
        // create web image that references to a file "AllSupportedBarcodes.png" in directory "/UploadedImageFiles/SessionID/"
        var imageSource = new Vintasoft.Shared.WebImageSourceJS("AllSupportedBarcodes.png");
        var image = new Vintasoft.Shared.WebImageJS(imageSource, 0);
    
        // send an asynchronous request for barcode recognition
        barcodeReader.readBarcodes(image, this.__readBarcodes_success, this.__readBarcodes_fail);
    </script>
    
    


    Пример: Вот код JavaScript, который демонстрирует, как распознавать штрих-коды Code39, Code128, EAN13 и UPCA из изображения и получать результаты проверки качества печати для всех распознанных штрих-кодов:
    <script type="text/javascript">
    
        /**
         * Barcodes are recognized successfully.
         */
        function __readBarcodes_success(data) {
            if (data.success) {
                // get the barcode recognition result
                var barcodeRecognitionResults = data.results;
    
                var htmlMarkup = '';
                // if no barcodes found
                if (barcodeRecognitionResults.length == 0) {
                    htmlMarkup = 'No barcodes found.';
                }
                // if barcodes are found
                else {
                    htmlMarkup = barcodeRecognitionResults.length.toString() + ' barcodes are found.<br />';
                    htmlMarkup += '<br />';
    
                    // for each recognized barcode
                    for (var i = 0; i < barcodeRecognitionResults.length; i++) {
                        // get the barcode recognition result
                        var barcodeRecognitionResult = barcodeRecognitionResults[i];
    
                        // output information about recognized barcode
                        htmlMarkup += '[' + (i + 1) + ':' + barcodeRecognitionResult.barcodeType + ']<br />';
                        htmlMarkup += '  Value: ' + barcodeRecognitionResult.value + '<br />';
                        htmlMarkup += '  Confidence: ' + barcodeRecognitionResult.confidence + '<br />';
                        htmlMarkup += '  Reading quality: ' + barcodeRecognitionResult.readingQuality.toFixed(2) + '<br />';
                        htmlMarkup += '  Threshold: ' + barcodeRecognitionResult.threshold + '<br />';
                        htmlMarkup += '  Region: ' +
                            'LT=(' + barcodeRecognitionResult.region.leftTop.x + ',' + barcodeRecognitionResult.region.leftTop.y + '); ' +
                            'RT=(' + barcodeRecognitionResult.region.rightTop.x + ',' + barcodeRecognitionResult.region.rightTop.y + '); ' +
                            'LB=(' + barcodeRecognitionResult.region.leftBottom.x + ',' + barcodeRecognitionResult.region.leftBottom.y + '); ' +
                            'RB=(' + barcodeRecognitionResult.region.rightBottom.x + ',' + barcodeRecognitionResult.region.rightBottom.y + '); ' +
                            'Angle=' + barcodeRecognitionResult.region.angle.toFixed(1) + '°<br />';
    
                        // if barcode print quality test exists
                        if (barcodeRecognitionResult.printQualityTest != undefined) {
                          // output information about print quality test result
                          htmlMarkup += 'Barcode print quality test is available.<br />';
                        }
    
                        htmlMarkup += '<br />';
                    }
                }
    
                var barcodeInformationElement = document.getElementById("barcodeInformation");
                barcodeInformationElement.innerHTML = htmlMarkup;
            }
        }
    
        /**
         * Barcode recognition is failed.
         */
        function __readBarcodes_fail(data) {
            // show information about error
            alert(data.errorMessage);
        }
    
    
        // set the session identifier
        Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
    
        // create service that allows to recognize barcodes
        var barcodeService = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
    
        // create the barcode reader
        var barcodeReader = new Vintasoft.Barcode.WebBarcodeReaderJS(barcodeService);
        // specify that Code39, Code128, EAN13 and UPCA barcode must be searched
        var barcodeTypesToSearch = new Vintasoft.Barcode.WebBarcodeTypeEnumJS("Code39");
        barcodeTypesToSearch = barcodeTypesToSearch.add("Code128");
        barcodeTypesToSearch = barcodeTypesToSearch.add("EAN13");
        barcodeTypesToSearch = barcodeTypesToSearch.add("UPCA");
        barcodeReader.get_Settings().set_BarcodeType(barcodeTypesToSearch);
        // specify that the print quality test information must be collected for all recognized barcodes
        barcodeReader.get_Settings().set_CollectTestInformation(true);
    
        // create web image that references to a file "AllSupportedBarcodes.png" in directory "/UploadedImageFiles/SessionID/"
        var imageSource = new Vintasoft.Shared.WebImageSourceJS("AllSupportedBarcodes.png");
        var image = new Vintasoft.Shared.WebImageJS(imageSource, 0);
    
        // send an asynchronous request for barcode recognition
        barcodeReader.readBarcodes(image, this.__readBarcodes_success, this.__readBarcodes_fail);
    </script>
    
    


    Пример: Вот код TypeScript компонента Angular, который демонстрирует, как распознавать штрих-коды Code39 из изображения:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'barcode-reader-demo',
      templateUrl: './barcode-reader-demo.component.html'
    })
    export class BarcodeReaderDemoComponent {
    
      ngOnInit() {
        // set the session identifier
        Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
    
        // create service that allows to recognize barcodes
        let barcodeService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
    
        // create the barcode reader
        let barcodeReader: Vintasoft.Barcode.WebBarcodeReaderJS = new Vintasoft.Barcode.WebBarcodeReaderJS(barcodeService);
        // specify that Code39 barcode must be searched
        barcodeReader.get_Settings().set_BarcodeType(new Vintasoft.Barcode.WebBarcodeTypeEnumJS("Code39"));
    
        // create web image, where barcodes must be recognized
        let imageSource: Vintasoft.Shared.WebImageSourceJS = new Vintasoft.Shared.WebImageSourceJS("AllSupportedBarcodes.png");
        let image: Vintasoft.Shared.WebImageJS = new Vintasoft.Shared.WebImageJS(imageSource, 0);
    
        // send an asynchronous request for barcode recognition
        barcodeReader.readBarcodes(image, this.__readBarcodes_success, this.__readBarcodes_fail);
      }
    
      /**
       * Barcodes are recognized successfully.
       * @param {object} data Object with information about recognized barcodes.
       */
      private __readBarcodes_success(data: Vintasoft.Barcode.WebBarcodeReadResponseParamsJS) {
        if (data.success) {
          // get the barcode recognition result
          let barcodeRecognitionResults: Vintasoft.Barcode.WebBarcodeRecognitionResultJS[] = data.results;
    
          let htmlMarkup: string = '';
          // if no barcodes found
          if (barcodeRecognitionResults.length == 0) {
            htmlMarkup = 'No barcodes found.';
          }
          // if barcodes are found
          else {
            htmlMarkup = barcodeRecognitionResults.length.toString() + ' barcodes are found.<br />';
            htmlMarkup += '<br />';
    
            // for each recognized barcode
            for (let i: number = 0; i < barcodeRecognitionResults.length; i++) {
              // get the barcode recognition result
              let barcodeRecognitionResult: Vintasoft.Barcode.WebBarcodeRecognitionResultJS = barcodeRecognitionResults[i];
    
              // output information about recognized barcode
              htmlMarkup += '[' + (i + 1) + ':' + barcodeRecognitionResult.barcodeType + ']<br />';
              htmlMarkup += '  Value: ' + barcodeRecognitionResult.value + '<br />';
              htmlMarkup += '  Confidence: ' + barcodeRecognitionResult.confidence + '<br />';
              htmlMarkup += '  Reading quality: ' + barcodeRecognitionResult.readingQuality.toFixed(2) + '<br />';
              htmlMarkup += '  Threshold: ' + barcodeRecognitionResult.threshold + '<br />';
              htmlMarkup += '  Region: ' +
                'LT=(' + barcodeRecognitionResult.region.leftTop.x + ',' + barcodeRecognitionResult.region.leftTop.y + '); ' +
                'RT=(' + barcodeRecognitionResult.region.rightTop.x + ',' + barcodeRecognitionResult.region.rightTop.y + '); ' +
                'LB=(' + barcodeRecognitionResult.region.leftBottom.x + ',' + barcodeRecognitionResult.region.leftBottom.y + '); ' +
                'RB=(' + barcodeRecognitionResult.region.rightBottom.x + ',' + barcodeRecognitionResult.region.rightBottom.y + '); ' +
                'Angle=' + barcodeRecognitionResult.region.angle.toFixed(1) + '°<br />';
              htmlMarkup += '<br />';
            }
          }
    
          let barcodeInformationElement: HTMLDivElement = document.getElementById("barcodeInformation") as HTMLDivElement;
          barcodeInformationElement.innerHTML = htmlMarkup;
        }
      }
    
      /**
       * Barcode recognition is failed.
       * @param {object} data Object with information about error.
       */
      private __readBarcodes_fail(data) {
        // show information about error
        alert(data.errorMessage);
      }
    }
    
    


    Пример: Вот код TypeScript компонента Angular, который демонстрирует, как распознавать штрих-коды Code39, Code128, EAN13 и UPCA из изображения и получать результаты проверки качества печати для всех распознанных штрих-кодов:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'barcode-reader-demo',
      templateUrl: './barcode-reader-demo.component.html'
    })
    export class BarcodeReaderDemoComponent {
    
      ngOnInit() {
        // set the session identifier
        Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
    
        // create service that allows to recognize barcodes
        let barcodeService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
    
        // create the barcode reader
        let barcodeReader: Vintasoft.Barcode.WebBarcodeReaderJS = new Vintasoft.Barcode.WebBarcodeReaderJS(barcodeService);
        // specify that Code39, Code128, EAN13 and UPCA barcode must be searched
        let barcodeTypesToSearch: Vintasoft.Barcode.WebBarcodeTypeEnumJS = new Vintasoft.Barcode.WebBarcodeTypeEnumJS("Code39");
        barcodeTypesToSearch = barcodeTypesToSearch.add("Code128");
        barcodeTypesToSearch = barcodeTypesToSearch.add("EAN13");
        barcodeTypesToSearch = barcodeTypesToSearch.add("UPCA");
        barcodeReader.get_Settings().set_BarcodeType(barcodeTypesToSearch);
        // specify that the print quality test information must be collected for all recognized barcodes
        barcodeReader.get_Settings().set_CollectTestInformation(true);
    
        // create web image, where barcodes must be recognized
        let imageSource: Vintasoft.Shared.WebImageSourceJS = new Vintasoft.Shared.WebImageSourceJS("AllSupportedBarcodes.png");
        let image: Vintasoft.Shared.WebImageJS = new Vintasoft.Shared.WebImageJS(imageSource, 0);
    
        // send an asynchronous request for barcode recognition
        barcodeReader.readBarcodes(image, this.__readBarcodes_success, this.__readBarcodes_fail);
      }
    
      /**
       * Barcodes are recognized successfully.
       * @param {object} data Object with information about recognized barcodes.
       */
      private __readBarcodes_success(data: Vintasoft.Barcode.WebBarcodeReadResponseParamsJS) {
        if (data.success) {
          // get the barcode recognition result
          let barcodeRecognitionResults: Vintasoft.Barcode.WebBarcodeRecognitionResultJS[] = data.results;
    
          let htmlMarkup: string = '';
          // if no barcodes found
          if (barcodeRecognitionResults.length == 0) {
            htmlMarkup = 'No barcodes found.';
          }
          // if barcodes are found
          else {
            htmlMarkup = barcodeRecognitionResults.length.toString() + ' barcodes are found.<br />';
            htmlMarkup += '<br />';
    
            // for each recognized barcode
            for (let i: number = 0; i < barcodeRecognitionResults.length; i++) {
              // get the barcode recognition result
              let barcodeRecognitionResult: Vintasoft.Barcode.WebBarcodeRecognitionResultJS = barcodeRecognitionResults[i];
    
              // output information about recognized barcode
              htmlMarkup += '[' + (i + 1) + ':' + barcodeRecognitionResult.barcodeType + ']<br />';
              htmlMarkup += '  Value: ' + barcodeRecognitionResult.value + '<br />';
              htmlMarkup += '  Confidence: ' + barcodeRecognitionResult.confidence + '<br />';
              htmlMarkup += '  Reading quality: ' + barcodeRecognitionResult.readingQuality.toFixed(2) + '<br />';
              htmlMarkup += '  Threshold: ' + barcodeRecognitionResult.threshold + '<br />';
              htmlMarkup += '  Region: ' +
                'LT=(' + barcodeRecognitionResult.region.leftTop.x + ',' + barcodeRecognitionResult.region.leftTop.y + '); ' +
                'RT=(' + barcodeRecognitionResult.region.rightTop.x + ',' + barcodeRecognitionResult.region.rightTop.y + '); ' +
                'LB=(' + barcodeRecognitionResult.region.leftBottom.x + ',' + barcodeRecognitionResult.region.leftBottom.y + '); ' +
                'RB=(' + barcodeRecognitionResult.region.rightBottom.x + ',' + barcodeRecognitionResult.region.rightBottom.y + '); ' +
                'Angle=' + barcodeRecognitionResult.region.angle.toFixed(1) + '°<br />';
    
              // if barcode print quality test exists
              if (barcodeRecognitionResult.printQualityTest != undefined) {
                // output information about print quality test result
                htmlMarkup += 'Barcode print quality test is available.<br />';
              }
    
              htmlMarkup += '<br />';
            }
          }
    
          let barcodeInformationElement: HTMLDivElement = document.getElementById("barcodeInformation") as HTMLDivElement;
          barcodeInformationElement.innerHTML = htmlMarkup;
        }
      }
    
      /**
       * Barcode recognition is failed.
       * @param {object} data Object with information about error.
       */
      private __readBarcodes_fail(data) {
        // show information about error
        alert(data.errorMessage);
      }
    }