Customize Barcode Appearance

This documentation explains how to customize the appearance of barcodes using the Aspose.BarCode Cloud SDK for Python. It provides details on the available methods, customization options, and examples of usage.

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.

Customization Options

When generating a barcode, you can customize various aspects of its appearance using the properties of the request classes.

Key Properties:

  • barcode_type: Specifies the type of barcode to generate (e.g. QR, Aztec, Pdf417, Code39, etc).
  • data: The data to encode in the barcode.
  • image_format: Specifies the output format of the barcode image (e.g., Png, Jpeg, Svg).
  • foreground_color: Color of the barcode bars, supports standard color names or ARGB values (e.g., AliceBlue or #FF000000).
  • background_color: Background color of the barcode image.
  • text_location: Position of the code text relative to the barcode (Below, Above, None).
  • units: Measurement units for image dimensions (Pixel, Inch, Millimeter).
  • resolution: Resolution of the image in dpi.
  • image_height and image_width: Dimensions of the barcode image.
  • rotation_angle: Rotation angle of the barcode (e.g., 0, 90, 180).

Barcode Type-Specific Parameters

In addition to the common appearance settings above, the API exposes advanced parameters that apply only to specific barcode types. They are ignored when a different barcode type is generated.

QR, MicroQR and RectMicroQR

  • qr_encode_mode: QR barcode encode mode (Auto, Extended, Binary, ECI).
  • qr_error_level: QR error correction level (LevelL, LevelM, LevelQ, LevelH).
  • qr_version: QR barcode version (Auto, Version01Version40). Auto selects the smallest version that fits the data.
  • qr_eci_encoding: ECI encoding for the QR barcode data (e.g., UTF8, ISO_8859_1).
  • qr_aspect_ratio: QR barcode aspect ratio, from 0 to 1.
  • micro_qr_version: MicroQR version (Auto, M1M4). Used when the barcode type is MicroQR.
  • rect_micro_qr_version: RectMicroQR version (Auto, R7x43R17x139). Used when the barcode type is RectMicroQR.

Code128

  • code128_encode_mode: Controls which Code 128 subset is used (Auto, CodeA, CodeB, CodeAB, CodeC, CodeAC, CodeBC).

PDF417, MacroPDF417 and MicroPDF417

  • pdf417_encode_mode: PDF417 encode mode (Auto, Binary, ECI, Extended).
  • pdf417_error_level: PDF417 error correction level (Level0Level8).
  • pdf417_eci_encoding: ECI encoding for the PDF417 barcode data.
  • pdf417_truncate: Whether to use the truncated PDF417 format (removes the right-side stop pattern).
  • pdf417_columns: Number of columns (130, or 0 for auto).
  • pdf417_rows: Number of rows (390, or 0 for automatic).
  • pdf417_aspect_ratio: Height/width ratio of the barcode module (25 for MicroPDF417; 35 for PDF417 and MacroPDF417).
  • pdf417_macro_characters: Macro character to prepend for structured append (None, Macro05, Macro06).
  • pdf417_is_reader_initialization: Whether the barcode is used for reader initialization (programming).
  • pdf417_is_linked: Whether to use linked mode (for MicroPDF417).
  • pdf417_is_code128_emulation: Whether to use Code128 emulation (for MicroPDF417).

Examples

Example 1: Generating a Barcode with GET Request

import os
from aspose_barcode_cloud import (
    ApiClient,
    EncodeBarcodeType,
    BarcodeImageFormat,
    BarcodeImageParams,
    CodeLocation,
    Configuration,
    QrParams,
    QREncodeMode,
    QRErrorLevel,
    QRVersion,
)
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():
    configuration = make_configuration()
    api_client = ApiClient(configuration=configuration)
    generate_api = GenerateApi(api_client=api_client)

    response = generate_api.generate(
        EncodeBarcodeType.QR,
        "Aspose.BarCode.Cloud",
        barcode_image_params=BarcodeImageParams(
            image_format=BarcodeImageFormat.PNG,
            foreground_color="Black",
            background_color="White",
            text_location=CodeLocation.BELOW,
            resolution=300,
            image_height=200,
            image_width=200,
        ),
        qr_params=QrParams(
            qr_encode_mode=QREncodeMode.AUTO,
            qr_error_level=QRErrorLevel.LEVELM,
            qr_version=QRVersion.AUTO,
            qr_aspect_ratio=0.75,
        ),
    )

    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "qr.png"))

    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 2: Generating a Barcode with POST Request (JSON Body)

import os
from aspose_barcode_cloud import (
    ApiClient,
    Configuration,
    EncodeBarcodeType,
    EncodeDataType,
    EncodeData,
    GenerateParams,
    BarcodeImageParams,
    BarcodeImageFormat,
    QrParams,
    QREncodeMode,
    QRErrorLevel,
    QRVersion,
)
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


# Main function to generate barcode
def main():
    # Set up the configuration and API client
    configuration = make_configuration()
    api_client = ApiClient(configuration)
    generate_api = GenerateApi(api_client=api_client)

    # Define the file path for the generated barcode
    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "qr-custom.jpeg"))

    # Set up the generation parameters
    generate_params = GenerateParams(
        barcode_type=EncodeBarcodeType.QR,
        encode_data=EncodeData(data="Aspose", data_type=EncodeDataType.STRINGDATA),
        barcode_image_params=BarcodeImageParams(
            foreground_color="#FF0000",
            background_color="#FFFF00",
            image_format=BarcodeImageFormat.JPEG,
            rotation_angle=90,
        ),
        qr_params=QrParams(
            qr_encode_mode=QREncodeMode.AUTO,
            qr_error_level=QRErrorLevel.LEVELM,
            qr_version=QRVersion.AUTO,
            qr_aspect_ratio=0.75,
        ),
    )

    # Generate barcode
    response = generate_api.generate_body(generate_params)

    # Save the generated image to a file
    with open(file_name, "wb") as stream:
        stream.write(response.data)

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


# To run the main function if this script is executed
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,
    BarcodeImageFormat,
    BarcodeImageParams,
    CodeLocation,
    Configuration,
    Pdf417Params,
    Pdf417EncodeMode,
    Pdf417ErrorLevel,
)
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.svg"))

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

    # Generate barcode
    response = generate_api.generate_multipart(
        barcode_type=EncodeBarcodeType.PDF417,
        data="Aspose.BarCode.Cloud",
        barcode_image_params=BarcodeImageParams(
            text_location=CodeLocation.ABOVE,
            image_format=BarcodeImageFormat.SVG,
        ),
        pdf417_params=Pdf417Params(
            pdf417_encode_mode=Pdf417EncodeMode.AUTO,
            pdf417_error_level=Pdf417ErrorLevel.LEVEL2,
            pdf417_aspect_ratio=3,
        ),
    )

    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