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

    Файл 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.


    Для генерации изображения 1D-штрих-кода необходимо:
    Для генерации изображения 2D-штрих-кода необходимо:

    Пример: Вот код JavaScript, который демонстрирует, как генерировать изображение штрих-кода QR Code:
    <script type="text/javascript">
        /**
         * Generates 1D barcode image.
         */
        function generate1dBarcodeImage(barcodeType, barcodeValue) {
            // create service that allows to generate barcode
            var barcodeService = new Vintasoft.Shared.WebServiceControllerJS("/vintasoft/api/MyVintasoftBarcodeApi");
    
            // create the barcode writer
            var barcodeWriter = new Vintasoft.Barcode.WebBarcodeWriterJS(barcodeService);
    
            // create the barcode writer settings for generating 1D barcode
            var barcodeWriterSettings = new Vintasoft.Barcode.Web1DBarcodeWriterSettingsJS();
            // specify that barcode writer must generate QR barcode image
            barcodeWriterSettings.set_BarcodeType(new Vintasoft.Barcode.Web1DBarcodeTypeEnumJS(barcodeType));
            // specify the Code128 barcode value
            barcodeWriterSettings.set_Value(barcodeValue);
    
            // specify settings for barcode writer
            barcodeWriter.set_Settings(barcodeWriterSettings);
    
            // send an asynchronous request for getting barcode image as Base64 string
            barcodeWriter.getBarcodeAsBase64Image(__writeBarcode_success, __writeBarcode_failed);
        }
    
        /**
         * Generates 2D barcode image.
         */
        function generate2dBarcodeImage(barcodeType, barcodeValue) {
            // create web service that allows to generate barcode
            var barcodeService = new Vintasoft.Shared.WebServiceControllerJS("/vintasoft/api/MyVintasoftBarcodeApi");
    
            // create the barcode writer
            var barcodeWriter = new Vintasoft.Barcode.WebBarcodeWriterJS(barcodeService);
    
            // create the barcode writer settings for generating 2D barcode
            var barcodeWriterSettings = new Vintasoft.Barcode.Web2DBarcodeWriterSettingsJS();
            // specify that barcode writer must generate QR barcode image
            barcodeWriterSettings.set_BarcodeType(new Vintasoft.Barcode.Web2DBarcodeTypeEnumJS(barcodeType));
            // specify the QR barcode value
            barcodeWriterSettings.set_Value(barcodeValue);
    
            // specify settings for barcode writer
            barcodeWriter.set_Settings(barcodeWriterSettings);
    
            // send an asynchronous request for getting barcode image as Base64 string
            barcodeWriter.getBarcodeAsBase64Image(__writeBarcode_success, __writeBarcode_failed);
        }
    
        /**
         * Barcode is generated successfully.
         */
        function __writeBarcode_success(data) {
            if (data.success) {
                var barcodeImage = data.barcodeImage;
                document.getElementById("barcodeImage").src = barcodeImage;
            }
            else {
                alert(data.errorMessage);
            }
        }
    
        /**
         * Barcode generation is failed.
         */
        function __writeBarcode_failed(data) {
            // show information about error
            alert(data.errorMessage);
        }
    
    
        // set the session identifier
        Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
        // generate image of QR barcode with value "12345"
        generate2dBarcodeImage("QR", "12345");
    </script>
    
    


    Пример: Вот код TypeScript компонента Angular, который демонстрирует, как генерировать изображение штрих-кода QR Code:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'barcode-generator-demo',
      templateUrl: './barcode-generator-demo.component.html'
    })
    export class BarcodeGeneratorDemoComponent {
    
      ngOnInit() {
        // generate image of QR barcode with value "12345"
        this.generate2dBarcodeImage("QR", "12345");
      }
    
      /**
       * Generates 1D barcode image.
       * @param barcodeType Barcode type.
       * @param barcodeValue Barcode value.
      */
      public generate1dBarcodeImage(barcodeType: string, barcodeValue: string) {
        // set the session identifier
        Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
    
        // create web service that allows to generate barcode
        let barcodeService: Vintasoft.Shared.WebServiceJS = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
    
        // create the barcode writer
        let barcodeWriter: Vintasoft.Barcode.WebBarcodeWriterJS = new Vintasoft.Barcode.WebBarcodeWriterJS(barcodeService);
    
        // create the barcode writer settings for generating 2D barcode
        let barcodeWriterSettings: Vintasoft.Barcode.Web1DBarcodeWriterSettingsJS = new Vintasoft.Barcode.Web1DBarcodeWriterSettingsJS();
        // specify that barcode writer must generate QR barcode image
        barcodeWriterSettings.set_BarcodeType(new Vintasoft.Barcode.WebBarcodeTypeEnumJS(barcodeType));
        // specify the QR barcode value
        barcodeWriterSettings.set_Value(barcodeValue);
    
        // specify settings for barcode writer
        barcodeWriter.set_Settings(barcodeWriterSettings);
    
        // send an asynchronous request for getting barcode image as Base64 string
        barcodeWriter.getBarcodeAsBase64Image(this.__writeBarcode_success, this.__writeBarcode_failed);
      }
    
      /**
       * Generates 2D barcode image.
       * @param barcodeType Barcode type.
       * @param barcodeValue Barcode value.
      */
      public generate2dBarcodeImage(barcodeType: string, barcodeValue: string) {
        // set the session identifier
        Vintasoft.Shared.WebImagingEnviromentJS.set_SessionId("SessionID");
    
        // create web service that allows to generate barcode
        let barcodeService: Vintasoft.Shared.WebServiceJS = new Vintasoft.Shared.WebServiceControllerJS("vintasoft/api/MyVintasoftBarcodeApi");
    
        // create the barcode writer
        let barcodeWriter: Vintasoft.Barcode.WebBarcodeWriterJS = new Vintasoft.Barcode.WebBarcodeWriterJS(barcodeService);
    
        // create the barcode writer settings for generating 2D barcode
        let barcodeWriterSettings: Vintasoft.Barcode.Web2DBarcodeWriterSettingsJS = new Vintasoft.Barcode.Web2DBarcodeWriterSettingsJS();
        // specify that barcode writer must generate QR barcode image
        barcodeWriterSettings.set_BarcodeType(new Vintasoft.Barcode.WebBarcodeTypeEnumJS(barcodeType));
        // specify the QR barcode value
        barcodeWriterSettings.set_Value(barcodeValue);
    
        // specify settings for barcode writer
        barcodeWriter.set_Settings(barcodeWriterSettings);
    
        // send an asynchronous request for getting barcode image as Base64 string
        barcodeWriter.getBarcodeAsBase64Image(this.__writeBarcode_success, this.__writeBarcode_failed);
      }
    
      /**
       * Barcode is generated successfully.
       * @param data Object that stores response from barcode service.
       */
      private __writeBarcode_success(data: Vintasoft.Barcode.WebBarcodeWriteResponseParamsJS) {
        if (data.success) {
          let barcodeImage: string = data.barcodeImage;
          let barcodeImageElement: HTMLImageElement = document.getElementById("barcodeImage") as HTMLImageElement;
          barcodeImageElement.src = barcodeImage;
        }
        else {
          alert(data.errorMessage);
        }
      }
    
      /**
       * Barcode generation is failed.
       * @param data Object with information about error.
       */
      private __writeBarcode_failed(data) {
        // show information about error
        alert(data.errorMessage);
      }
    
    }