Set Barcode Source

To perform barcode recognition using the Aspose.BarCode.Cloud SDK for Python, you need to set the source of the barcode image. Methods of this SDK support loading barcode images from multiple sources, such as file URL, file, and base64 encoded data. This guide provides detailed examples on how to set barcode image source through different API methods.

Supported Barcode Image Formats

  • JPEG
  • TIFF
  • PNG
  • BMP
  • GIF

Supported Barcode Source Formats

  • File URL: Link directly to an image URL.
  • file: The image file for processing.
  • Base64 Encoded Image: Convert the image to a base64 encoded string for secure transfer.

Examples for ScanApi

  • Using GET Request with File URL
import os
from aspose_barcode_cloud import ScanApi, ApiClient, Configuration


def make_configuration():
    """
    Creates and returns the configuration for the Aspose Barcode API.
    """
    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():

    config = make_configuration()
    scan_api = ScanApi(ApiClient(config))

    barcode_image_url = "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"

    result = scan_api.scan(file_url=barcode_image_url)

    if result.barcodes and len(result.barcodes) > 0:
        print(f"Recognized barcode value: '{result.barcodes[0].barcode_value}'")
    else:
        print("No barcodes recognized.")


if __name__ == "__main__":
    main()
  • Using POST Request with Base64 Encoded Image
import os
import base64
from aspose_barcode_cloud import ScanApi, ApiClient, Configuration, ScanBase64Request


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


def main():
    scan_api = ScanApi(ApiClient(make_configuration()))

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

    with open(file_name, "rb") as file:
        image_bytes = file.read()
    image_base64 = base64.b64encode(image_bytes).decode("utf-8")

    request = ScanBase64Request(file_base64=image_base64)
    result = scan_api.scan_base64(request)

    print(f"File '{file_name}' recognized, result: '{result.barcodes[0].barcode_value}'")
  • Using POST Request with File Stream
import os
from pathlib import Path

from aspose_barcode_cloud import ApiClient, Configuration, ScanApi


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


def main():
    scan_api = ScanApi(ApiClient(make_configuration()))

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

    with open(file_name, "rb") as file_stream:
        result = scan_api.scan_multipart(file=file_stream)

    print(f"File '{file_name}' recognized, result: '{result.barcodes[0].barcode_value}'")


if __name__ == "__main__":
    main()

Examples for RecognizeApi

  • Using GET Request with File URL
from aspose_barcode_cloud import ApiClient, Configuration, RecognizeApi, DecodeBarcodeType
import os


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

    # Call the API to recognize the barcode
    result = recognize_api.recognize(
        barcode_type=DecodeBarcodeType.QR, file_url="https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"
    )

    # Output the result
    if result.barcodes:
        print(f"File recognized, result: '{result.barcodes[0].barcode_value}'")
    else:
        print("No barcodes were recognized.")


if __name__ == "__main__":
    main()
  • Using POST Request with Base64 Encoded Image
import os
import base64
from aspose_barcode_cloud import (
    RecognizeApi,
    ApiClient,
    Configuration,
    DecodeBarcodeType,
    RecognizeBase64Request,
)


def make_configuration():
    access_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
    if access_token:
        config = Configuration(access_token=access_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():
    config = make_configuration()
    recognize_api = RecognizeApi(ApiClient(config))

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

    with open(file_name, "rb") as file:
        image_bytes = file.read()
        image_base64 = base64.b64encode(image_bytes).decode("utf-8")

    request = RecognizeBase64Request(barcode_types=[DecodeBarcodeType.QR], file_base64=image_base64)

    result = recognize_api.recognize_base64(request)

    print(f"File '{file_name}' recognized, result: '{result.barcodes[0].barcode_value}'")


if __name__ == "__main__":
    main()
  • Using POST Request with File Stream
import os
from aspose_barcode_cloud import (
    RecognizeApi,
    ApiClient,
    Configuration,
    DecodeBarcodeType,
)


# Function to create 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():

    recognize_api = RecognizeApi(ApiClient(make_configuration()))

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

    with open(file_path, "rb") as file_stream:
        result = recognize_api.recognize_multipart(barcode_type=DecodeBarcodeType.QR, file=file_stream)

    if result.barcodes:
        print(f"File '{file_path}' recognized, result: '{result.barcodes[0].barcode_value}'")
    else:
        print(f"No barcodes found in '{file_path}'.")


if __name__ == "__main__":
    main()

Conclusion

Using the Aspose.BarCode.Cloud SDK for .NET, you can easily load and recognize barcode images in file URL, file, and base64 encoded formats.

Optimize your barcode recognition workflows by selecting the most appropriate method based on your specific image format and application requirements.