Set Target Barcode Types
This guide provides an overview of setting target barcode types using the Aspose.BarCode.Cloud SDK for Python. Using this SDK, you can specify the barcode types you want to recognize when sending requests, including formats like Aztec, QR, Pdf417, and many others.
Overview of RecognizeApi
The RecognizeApi
provides methods for recognizing barcodes from various data inputs. You can choose to send barcode recognition requests using either a file URL, a base64 encoded image, or a multipart form request. By specifying the barcode type and recognition settings, you can optimize the API to target the specific barcode formats your application needs.
Supported Barcode Types
The DecodeBarcodeType
enum allows for a variety of barcode types, including:
- Aztec
- QR
- Code128
- DataMatrix
- Pdf417
- EAN13
- …and many more.
For full supported barcode list see the decode type github page
Example: Specifying Target Barcode Types
Using recognize
This method sends a GET request with the file URL and barcode type in the query parameters.
import os
from aspose_barcode_cloud import (
RecognizeApi,
ApiClient,
Configuration,
DecodeBarcodeType,
)
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_url = "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"
result = recognize_api.recognize(DecodeBarcodeType.MOSTCOMMONLYUSED, file_url=file_url)
print(f"File '{file_url}' recognized, result: '{result.barcodes[0].barcode_value}'")
if __name__ == "__main__":
main()
In this example, most commonly used barcode symbologies will be recognized from the provided image URL.
Using recognize_body
with multiple Barcode Types
For base64-encoded images, use the recognize_body
method, which allows for flexible recognition across multiple barcode types.
import os
import base64
from aspose_barcode_cloud import (
RecognizeApi,
ApiClient,
Configuration,
DecodeBarcodeType,
RecognizeBase64Request,
BarcodeResponseList,
)
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_and_code128.png"))
with open(file_name, "rb") as file:
image_bytes = file.read()
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
recognize_base64_request = RecognizeBase64Request(
barcode_types=[DecodeBarcodeType.QR, DecodeBarcodeType.CODE128], file_base64=image_base64
)
result: BarcodeResponseList = recognize_api.recognize_base64(recognize_base64_request)
print(f"File '{file_name}' recognized, results: ")
for barcode in result.barcodes:
print(f"Value: '{barcode.barcode_value}', type: {barcode.type}")
if __name__ == "__main__":
main()
In this example, the API will target both QR and Pdf417 barcode types in the base64 image string.
Using recognize_multipart
For recognizing barcodes from files in a form upload, use recognize_multipart
. This method supports setting a single barcode type.
import os
from aspose_barcode_cloud import (
RecognizeApi,
ApiClient,
Configuration,
DecodeBarcodeType,
)
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", "pdf417.png"))
with open(file_name, "rb") as file_stream:
result = recognize_api.recognize_multipart(DecodeBarcodeType.PDF417, file=file_stream)
print(f"File '{file_name}' recognized, result: '{result.barcodes[0].barcode_value}'")
if __name__ == "__main__":
main()
This example sets the target type to Pdf417, recognizing only this type in the uploaded image file.
Conclusion
By specifying target barcode types in your RecognizeApi
requests, you can optimize performance and precision based on your application’s needs. Setting a DecodeBarcodeType
for your recognition tasks ensures that only specific barcode formats are detected, improving accuracy and efficiency.