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).

Examples

Example 1: Generating a Barcode with GET Request

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

    response = generate_api.generate(
        EncodeBarcodeType.QR,
        "Aspose.BarCode.Cloud",
        image_format=BarcodeImageFormat.PNG,
        foreground_color="Black",
        background_color="White",
        resolution=300,
        image_height=200,
        image_width=200,
        text_location=CodeLocation.BELOW,
    )

    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,
)
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", "code39.jpeg"))

    # Set up the generation parameters
    generate_params = GenerateParams(
        barcode_type=EncodeBarcodeType.CODE39,
        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,
        ),
    )

    # 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, CodeLocation, 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.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",
        text_location=CodeLocation.ABOVE,
        image_format=BarcodeImageFormat.SVG,
    )

    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