Set Barcode Text

This documentation covers how to set barcode text in the Aspose.BarCode Cloud SDK for Python. The Barcode API supports multiple methods to generate barcodes in different formats using GET and POST requests through the GenerateApi. Barcodes can be generated by specifying various parameters such as barcode type, data to encode, image format and display options. The data encoding process involves defining the type of data and the actual content.

GenerateApi

The GenerateApi interface includes methods for generating barcodes:

  • GET Request: Uses parameters in the URL route and query string.
  • POST Request (JSON/XML): Parameters are provided in the request body.
  • POST Request (Multipart Form): Parameters are sent as URL-encoded form data.

Methods

  • generate: Generates a barcode via GET request.
  • generate_body: Generates a barcode via POST request with JSON or XML body.
  • generate_multipart: Generates a barcode via POST request with multipart form data.

Setting Barcode Text

Properties

  • data_type: Specifies the type of data encoding (can be string, base64, or hex). If not set, the data value is used as plain text.
  • data: A string that represents the data to be encoded.

Supported Data Types

Value Description
STRINGDATA (Default value) Plain text
BASE64BYTES Base64 encoded binary data
HEXBYTES Hexadecimal encoded binary data

Barcode Generation Example

Below are examples to demonstrate barcode generation using the GenerateApi methods.

Example 1: Generating a Barcode with GET Request

import os
from aspose_barcode_cloud import (
    ApiClient,
    Configuration,
    EncodeBarcodeType,
    EncodeData,
    EncodeDataType,
    GenerateParams,
    BarcodeImageParams,
    BarcodeImageFormat,
)
from aspose_barcode_cloud.api.generate_api import GenerateApi


def make_configuration():
    env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
    if env_token:
        config = Configuration(access_token=env_token)
    else:
        config = Configuration(
            client_id="Client Id from https://dashboard.aspose.cloud/applications",
            client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
        )
    return config


def main():
    """Main function to generate a QR code."""
    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "qr.png"))

    configuration = make_configuration()
    api_client = ApiClient(configuration=configuration)
    generate_api = GenerateApi(api_client=api_client)

    generate_params = GenerateParams(
        EncodeBarcodeType.QR, EncodeData(EncodeDataType.STRINGDATA, "Aspose.BarCode.Cloud")
    )

    response = generate_api.generate_body(generate_params)

    with open(file_name, "wb") as file_stream:
        file_stream.write(response.data)

    print(f"File '{file_name}' generated.")


if __name__ == "__main__":
    main()

Result Image is: Result image

Example 2: Generating a Barcode with POST Request (JSON Body)

import os
from aspose_barcode_cloud import (
    ApiClient,
    EncodeBarcodeType,
    EncodeDataType,
    EncodeData,
    GenerateParams,
    Configuration,
)
from aspose_barcode_cloud.api.generate_api import GenerateApi


def make_configuration():
    env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
    if env_token:
        config = Configuration(access_token=env_token)
    else:
        config = Configuration(
            client_id="Client Id from https://dashboard.aspose.cloud/applications",
            client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
        )
    return config


def main():
    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "pdf417.png"))

    config = make_configuration()
    api_client = ApiClient(configuration=config)
    generate_api = GenerateApi(api_client=api_client)

    post_params = GenerateParams(
        barcode_type=EncodeBarcodeType.PDF417,
        encode_data=EncodeData(data_type=EncodeDataType.BASE64BYTES, data="QXNwb3NlLkJhckNvZGUuQ2xvdWQ="),
    )

    response = generate_api.generate_body(post_params)

    with open(file_name, "wb") as file:
        file.write(response.data)

    print(f"File '{file_name}' generated.")


if __name__ == "__main__":
    main()

Result Image is: Result image

Example 3: Generating a Barcode with POST Request (Multipart Form)

import os

from aspose_barcode_cloud import ApiClient, EncodeBarcodeType, EncodeDataType, Configuration
from aspose_barcode_cloud.api.generate_api import GenerateApi


def make_configuration():
    env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
    if env_token:
        config = Configuration(access_token=env_token)
    else:
        config = Configuration(
            client_id="Client Id from https://dashboard.aspose.cloud/applications",
            client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
        )
    return config


def main():
    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "Code128.png"))

    api_client = ApiClient(configuration=make_configuration())
    generate_api = GenerateApi(api_client=api_client)

    response = generate_api.generate_multipart(
        EncodeBarcodeType.CODE128, "4173706F73652E426172436F64652E436C6F7564", EncodeDataType.HEXBYTES
    )

    with open(file_name, "wb") as stream:
        stream.write(response.data)

    print(f"File '{file_name}' generated.")


if __name__ == "__main__":
    main()

Result Image is: Result image

This documentation covers how to use the GenerateApi to set barcodes text.