|
| 1 | +--- |
| 2 | +name: generate-and-scan-barcode-python |
| 3 | +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. |
| 4 | +--- |
| 5 | + |
| 6 | +# Generate and scan barcode in Python |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +The package name and import name differ. Install `aspose-barcode-cloud`, then import from `aspose_barcode_cloud`. |
| 11 | + |
| 12 | +## Quick start |
| 13 | + |
| 14 | +Use these imports in most Python examples: |
| 15 | + |
| 16 | +```python |
| 17 | +from aspose_barcode_cloud import ( |
| 18 | + ApiClient, |
| 19 | + Configuration, |
| 20 | + GenerateApi, |
| 21 | + RecognizeApi, |
| 22 | + ScanApi, |
| 23 | +) |
| 24 | +``` |
| 25 | + |
| 26 | +Prefer constructing API classes from one shared `ApiClient`: |
| 27 | + |
| 28 | +```python |
| 29 | +config = Configuration( |
| 30 | + client_id=client_id, |
| 31 | + client_secret=client_secret, |
| 32 | +) |
| 33 | +api_client = ApiClient(configuration=config) |
| 34 | + |
| 35 | +generate_api = GenerateApi(api_client=api_client) |
| 36 | +recognize_api = RecognizeApi(api_client=api_client) |
| 37 | +scan_api = ScanApi(api_client=api_client) |
| 38 | +``` |
| 39 | + |
| 40 | +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`. |
| 41 | + |
| 42 | +## Authentication |
| 43 | + |
| 44 | +Use one of these two patterns: |
| 45 | + |
| 46 | +1. Let the SDK fetch an access token lazily from client credentials. |
| 47 | + |
| 48 | +```python |
| 49 | +config = Configuration( |
| 50 | + client_id=client_id, |
| 51 | + client_secret=client_secret, |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +2. Inject a pre-fetched bearer token. |
| 56 | + |
| 57 | +```python |
| 58 | +config = Configuration( |
| 59 | + access_token=token, |
| 60 | + host="https://api.aspose.cloud/v4.0", |
| 61 | +) |
| 62 | +``` |
| 63 | + |
| 64 | +`Configuration.access_token` fetches a token automatically when `_access_token` is empty but `client_id`, `client_secret`, and `token_url` are present. |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +`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. |
| 69 | + |
| 70 | +## Choose the right API shape |
| 71 | + |
| 72 | +Pick the operation first: |
| 73 | + |
| 74 | +- `GenerateApi`: create a barcode image. |
| 75 | +- `RecognizeApi`: decode one or more known barcode types and optionally tune recognition. |
| 76 | +- `ScanApi`: auto-detect barcode types with the smallest API surface. |
| 77 | + |
| 78 | +Then pick the transport variant based on what the caller has: |
| 79 | + |
| 80 | +- Public internet URL to an image: use `recognize()` or `scan()`. `file_url` must be a public URL, not a local path. |
| 81 | +- Local file object or stream: use `recognize_multipart()` or `scan_multipart()`. |
| 82 | +- Raw bytes already in memory: base64-encode them yourself and use `recognize_base64()` or `scan_base64()`. |
| 83 | +- Simple text plus query parameters for generation: use `generate()`. |
| 84 | +- Structured generate payload: use `generate_body()`. |
| 85 | +- Multipart-form generation: use `generate_multipart()` when the caller explicitly needs that shape. |
| 86 | + |
| 87 | +Key method names: |
| 88 | + |
| 89 | +- `generate` |
| 90 | +- `generate_body` |
| 91 | +- `generate_multipart` |
| 92 | +- `recognize` |
| 93 | +- `recognize_base64` |
| 94 | +- `recognize_multipart` |
| 95 | +- `scan` |
| 96 | +- `scan_base64` |
| 97 | +- `scan_multipart` |
| 98 | + |
| 99 | +## Non-obvious SDK rules |
| 100 | + |
| 101 | +1. Install `aspose-barcode-cloud`, but import from `aspose_barcode_cloud`. |
| 102 | +2. Construct APIs as `GenerateApi(ApiClient(Configuration(...)))` or by reusing a shared `ApiClient`. Passing `Configuration` directly to an API class is the wrong shape for this SDK. |
| 103 | +3. `generate()`, `generate_body()`, and `generate_multipart()` return an `ApiResponse`. The image bytes are in `response.data`, not in a model object or file path. |
| 104 | +4. Recognize and scan methods return `BarcodeResponseList`. Iterate `result.barcodes` and read `barcode_value`, `type`, `region`, and `checksum`. |
| 105 | +5. `recognize_base64()` and `scan_base64()` expect a base64 string in the request model. The SDK does not call `base64.b64encode()` for you. |
| 106 | +6. `recognize()` and `recognize_multipart()` take one `DecodeBarcodeType`, but `RecognizeBase64Request.barcode_types` accepts a list. |
| 107 | +7. `ScanApi` auto-detects barcode types and does not take a barcode-type filter or the recognition tuning parameters exposed by `RecognizeApi`. |
| 108 | +8. GET-based recognize and scan methods only work with remote files reachable by URL. For local files, use multipart or base64. |
| 109 | +9. `ApiClient` supports header override through `header_name` and `header_value`; tests use this to customize `x-aspose-client`. |
| 110 | +10. Set `config.debug = True` when you need SDK and `urllib3` request logging. |
| 111 | + |
| 112 | +## Common patterns |
| 113 | + |
| 114 | +Generate and save a QR code: |
| 115 | + |
| 116 | +```python |
| 117 | +from aspose_barcode_cloud import ( |
| 118 | + ApiClient, |
| 119 | + CodeLocation, |
| 120 | + Configuration, |
| 121 | + EncodeBarcodeType, |
| 122 | + GenerateApi, |
| 123 | +) |
| 124 | + |
| 125 | +config = Configuration(client_id=client_id, client_secret=client_secret) |
| 126 | +api = GenerateApi(ApiClient(config)) |
| 127 | + |
| 128 | +response = api.generate( |
| 129 | + EncodeBarcodeType.QR, |
| 130 | + "hello from Python", |
| 131 | + text_location=CodeLocation.NONE, |
| 132 | +) |
| 133 | + |
| 134 | +with open("qr.png", "wb") as fh: |
| 135 | + fh.write(response.data) |
| 136 | +``` |
| 137 | + |
| 138 | +Recognize a local file stream: |
| 139 | + |
| 140 | +```python |
| 141 | +from aspose_barcode_cloud import ApiClient, Configuration, DecodeBarcodeType, RecognizeApi |
| 142 | + |
| 143 | +config = Configuration(client_id=client_id, client_secret=client_secret) |
| 144 | +api = RecognizeApi(ApiClient(config)) |
| 145 | + |
| 146 | +with open("qr.png", "rb") as fh: |
| 147 | + result = api.recognize_multipart(DecodeBarcodeType.QR, fh) |
| 148 | + |
| 149 | +for barcode in result.barcodes: |
| 150 | + print(barcode.barcode_value) |
| 151 | +``` |
| 152 | + |
| 153 | +Auto-scan bytes already in memory: |
| 154 | + |
| 155 | +```python |
| 156 | +import base64 |
| 157 | + |
| 158 | +from aspose_barcode_cloud import ApiClient, Configuration, ScanApi, ScanBase64Request |
| 159 | + |
| 160 | +config = Configuration(client_id=client_id, client_secret=client_secret) |
| 161 | +api = ScanApi(ApiClient(config)) |
| 162 | + |
| 163 | +with open("unknown.png", "rb") as fh: |
| 164 | + payload = base64.b64encode(fh.read()).decode("utf-8") |
| 165 | + |
| 166 | +result = api.scan_base64(ScanBase64Request(file_base64=payload)) |
| 167 | +``` |
| 168 | + |
| 169 | +## Working in this repo |
| 170 | + |
| 171 | +Read `references/repo-workflow.md` when the task changes SDK source, tests, snippets, package metadata, or generated code in `submodules/python`. |
| 172 | + |
| 173 | +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. |
| 174 | + |
| 175 | +## Final checklist |
| 176 | + |
| 177 | +1. Use the correct package and import pair: `aspose-barcode-cloud` for install, `aspose_barcode_cloud` for imports. |
| 178 | +2. Build API classes from `ApiClient(Configuration(...))`. |
| 179 | +3. Choose GET only for public URLs, multipart for local files, and base64 request models for bytes already in memory. |
| 180 | +4. Base64-encode request payloads yourself before calling `recognize_base64()` or `scan_base64()`. |
| 181 | +5. Treat generate responses as `response.data` bytes and recognize/scan responses as `result.barcodes`. |
| 182 | +6. When changing the repo, validate with the submodule workflow in `references/repo-workflow.md`. |
0 commit comments