| name | generate-and-scan-barcode-python |
|---|---|
| description | Write or update Python code that uses the Aspose.BarCode Cloud SDK (`aspose_barcode_cloud`; pip package `aspose-barcode-cloud`) to generate, recognize, or scan barcodes through Aspose's cloud REST API. Use this skill whenever the user wants barcode work in Python, touches files under `submodules/python`, or mentions `GenerateApi`, `RecognizeApi`, `ScanApi`, `ApiClient`, `Configuration`, `GenerateParams`, `RecognizeBase64Request`, or `ScanBase64Request`. The SDK has several easy-to-miss idioms, including constructing APIs from `ApiClient(Configuration(...))`, reading generate results from `.data`, using public `file_url` values for GET recognize and scan methods, base64-encoding body payloads yourself, and remembering that the install and import names differ. |
The Python SDK is a thin generated client over the Aspose BarCode Cloud REST API. Most tasks come down to choosing the right API class (GenerateApi, RecognizeApi, or ScanApi), choosing the right transport variant (GET, base64 body, or multipart), and wiring Configuration into ApiClient correctly.
The package name and import name differ. Install aspose-barcode-cloud, then import from aspose_barcode_cloud.
Use these imports in most Python examples:
from aspose_barcode_cloud import (
ApiClient,
Configuration,
GenerateApi,
RecognizeApi,
ScanApi,
)Prefer constructing API classes from one shared ApiClient:
config = Configuration(
client_id=client_id,
client_secret=client_secret,
)
api_client = ApiClient(configuration=config)
generate_api = GenerateApi(api_client=api_client)
recognize_api = RecognizeApi(api_client=api_client)
scan_api = ScanApi(api_client=api_client)If the task is repo maintenance inside submodules/python, read references/repo-workflow.md. If the task needs the closest existing example or snippet, read references/snippet-map.md.
Use one of these two patterns:
- Let the SDK fetch an access token lazily from client credentials.
config = Configuration(
client_id=client_id,
client_secret=client_secret,
)- Inject a pre-fetched bearer token.
config = Configuration(
access_token=token,
host="https://api.aspose.cloud/v4.0",
)Configuration.access_token fetches a token automatically when _access_token is empty but client_id, client_secret, and token_url are present.
Inside this repo, tests load tests/configuration.json first and then fall back to TEST_CONFIGURATION_* environment variables. Snippets usually check TEST_CONFIGURATION_ACCESS_TOKEN first, then fall back to client credentials. Mirror the surrounding file when editing existing repo code.
Configuration defaults to host="https://api.aspose.cloud/v4.0" and token_url="https://id.aspose.cloud/connect/token". Some older repo artifacts still mention the v3.0 host; prefer the runtime default unless you are intentionally matching older behavior in that file.
Pick the operation first:
GenerateApi: create a barcode image.RecognizeApi: decode one or more known barcode types and optionally tune recognition.ScanApi: auto-detect barcode types with the smallest API surface.
Then pick the transport variant based on what the caller has:
- Public internet URL to an image: use
recognize()orscan().file_urlmust be a public URL, not a local path. - Local file object or stream: use
recognize_multipart()orscan_multipart(). - Raw bytes already in memory: base64-encode them yourself and use
recognize_base64()orscan_base64(). - Simple text plus query parameters for generation: use
generate(). - Structured generate payload: use
generate_body(). - Multipart-form generation: use
generate_multipart()when the caller explicitly needs that shape.
Key method names:
generategenerate_bodygenerate_multipartrecognizerecognize_base64recognize_multipartscanscan_base64scan_multipart
- Install
aspose-barcode-cloud, but import fromaspose_barcode_cloud. - Construct APIs as
GenerateApi(ApiClient(Configuration(...)))or by reusing a sharedApiClient. PassingConfigurationdirectly to an API class is the wrong shape for this SDK. generate(),generate_body(), andgenerate_multipart()return anApiResponse. The image bytes are inresponse.data, not in a model object or file path.- Recognize and scan methods return
BarcodeResponseList. Iterateresult.barcodesand readbarcode_value,type,region, andchecksum. recognize_base64()andscan_base64()expect a base64 string in the request model. The SDK does not callbase64.b64encode()for you.recognize()andrecognize_multipart()take oneDecodeBarcodeType, butRecognizeBase64Request.barcode_typesaccepts a list.ScanApiauto-detects barcode types and does not take a barcode-type filter or the recognition tuning parameters exposed byRecognizeApi.- GET-based recognize and scan methods only work with remote files reachable by URL. For local files, use multipart or base64.
ApiClientsupports header override throughheader_nameandheader_value; tests use this to customizex-aspose-client.- Set
config.debug = Truewhen you need SDK andurllib3request logging.
Generate and save a QR code:
from aspose_barcode_cloud import (
ApiClient,
CodeLocation,
Configuration,
EncodeBarcodeType,
GenerateApi,
)
config = Configuration(client_id=client_id, client_secret=client_secret)
api = GenerateApi(ApiClient(config))
response = api.generate(
EncodeBarcodeType.QR,
"hello from Python",
text_location=CodeLocation.NONE,
)
with open("qr.png", "wb") as fh:
fh.write(response.data)Recognize a local file stream:
from aspose_barcode_cloud import ApiClient, Configuration, DecodeBarcodeType, RecognizeApi
config = Configuration(client_id=client_id, client_secret=client_secret)
api = RecognizeApi(ApiClient(config))
with open("qr.png", "rb") as fh:
result = api.recognize_multipart(DecodeBarcodeType.QR, fh)
for barcode in result.barcodes:
print(barcode.barcode_value)Auto-scan bytes already in memory:
import base64
from aspose_barcode_cloud import ApiClient, Configuration, ScanApi, ScanBase64Request
config = Configuration(client_id=client_id, client_secret=client_secret)
api = ScanApi(ApiClient(config))
with open("unknown.png", "rb") as fh:
payload = base64.b64encode(fh.read()).decode("utf-8")
result = api.scan_base64(ScanBase64Request(file_base64=payload))Read references/repo-workflow.md when the task changes SDK source, tests, snippets, package metadata, or generated code in submodules/python.
Read references/snippet-map.md when the task needs example code, README-aligned snippets, or the closest existing pattern for generate, recognize, or scan scenarios.
- Use the correct package and import pair:
aspose-barcode-cloudfor install,aspose_barcode_cloudfor imports. - Build API classes from
ApiClient(Configuration(...)). - Choose GET only for public URLs, multipart for local files, and base64 request models for bytes already in memory.
- Base64-encode request payloads yourself before calling
recognize_base64()orscan_base64(). - Treat generate responses as
response.databytes and recognize/scan responses asresult.barcodes. - When changing the repo, validate with the submodule workflow in
references/repo-workflow.md.