Set Barcode Colorscheme

This documentation provides a comprehensive guide on how to set the colorscheme for barcodes using the Aspose.BarCode Cloud SDK for Python. The API allows customization of barcode appearance, including foreground and background colors, ensuring your barcodes match your design needs.

Overview

The GenerateApi interface includes methods to generate barcodes using different request formats:

  • generate: GET request with parameters in route and query string.
  • generate_body: POST request with parameters in the body (JSON or XML).
  • generate_multipart: POST request with parameters in multipart form.

Colorscheme Parameters

The foreground_color and background_color properties enable you to define the colors for the barcode:

  • foreground_color: Specifies the color of the bars and text.
  • background_color: Specifies the background color of the barcode image.

Both properties accept standard color names or ARGB values (e.g., AliceBlue or #FF000000).

Examples

Example 1: Setting Colors Using GET Request

import os
from aspose_barcode_cloud import ApiClient, EncodeBarcodeType, BarcodeImageFormat, 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():
    """Main function to generate QR code barcode."""
    configuration = make_configuration()
    api_client = ApiClient(configuration=configuration)
    generate_api = GenerateApi(api_client=api_client)

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

    response = generate_api.generate(
        EncodeBarcodeType.QR,
        "https://products.aspose.cloud/barcode/family/",
        foreground_color="DarkBlue",
        background_color="LightGray",
        image_format=BarcodeImageFormat.PNG,
    )

    # Write the response to a file
    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

Example 2: Setting Colors Using POST Request with JSON Body

import os
from aspose_barcode_cloud import (
    ApiClient,
    EncodeBarcodeType,
    EncodeDataType,
    EncodeData,
    GenerateParams,
    BarcodeImageParams,
    BarcodeImageFormat,
    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"))

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

    generate_params = GenerateParams(
        EncodeBarcodeType.PDF417,
        EncodeData(EncodeDataType.STRINGDATA, "Aspose.BarCode.Cloud"),
        BarcodeImageParams(
            foreground_color="#FF5733", background_color="#FFFFFF", image_format=BarcodeImageFormat.JPEG
        ),
    )

    response = generate_api.generate_body(generate_params)

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

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


# Run the async main function
if __name__ == "__main__":
    main()

Result Image is: Result image

Example 3: Setting Colors Using Multipart Form POST

import os
from aspose_barcode_cloud import ApiClient, GenerateApi, EncodeBarcodeType, BarcodeImageFormat, Configuration


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", "Code39.png"))

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

    response = generate_api.generate_multipart(
        barcode_type=EncodeBarcodeType.CODE39,
        data="Aspose",
        foreground_color="Green",
        background_color="Yellow",
        image_format=BarcodeImageFormat.GIF,
    )

    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

Conclusion

With GenerateApi in Aspose.BarCode Cloud SDK for Python, you can easily generate barcodes with customized colors. Whether you prefer GET, POST with JSON, or multipart form POST, the flexibility of this API ensures seamless integration and customization of barcodes in your applications.