|
| 1 | +--- |
| 2 | +name: generate-and-scan-barcode-dotnet |
| 3 | +description: Write or update C#/.NET code that uses the Aspose.BarCode Cloud SDK (`Aspose.BarCode.Cloud.Sdk.*` namespaces; NuGet 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 C#/.NET, touches files under `submodules/dotnet`, or mentions `GenerateApi`, `RecognizeApi`, `ScanApi`, `Configuration`, `GenerateParams`, `RecognizeBase64Request`, or `ScanBase64Request`. The SDK has several easy-to-miss idioms - `Stream` return values, `JwtToken` switching auth mode, GET methods requiring a public `fileUrl`, `RecognizeBase64Async`/`ScanBase64Async` naming, and the `(clientSecret, clientId)` convenience-constructor order - so consult this skill instead of guessing. |
| 4 | +--- |
| 5 | + |
| 6 | +# Generate and scan barcode in .NET |
| 7 | + |
| 8 | +The .NET SDK is a thin generated client over the Aspose BarCode Cloud REST API. Most tasks boil down to choosing the right API class (`GenerateApi`, `RecognizeApi`, `ScanApi`), choosing the right transport variant (GET, body/base64, or multipart), and setting up `Configuration` correctly. |
| 9 | + |
| 10 | +The NuGet package name and code namespaces differ. Install `Aspose.BarCode-Cloud`, then import namespaces under `Aspose.BarCode.Cloud.Sdk.*`. |
| 11 | + |
| 12 | +## Quick start |
| 13 | + |
| 14 | +Use these namespaces in most C# examples: |
| 15 | + |
| 16 | +```csharp |
| 17 | +using Aspose.BarCode.Cloud.Sdk.Api; |
| 18 | +using Aspose.BarCode.Cloud.Sdk.Interfaces; |
| 19 | +using Aspose.BarCode.Cloud.Sdk.Model; |
| 20 | +``` |
| 21 | + |
| 22 | +Prefer constructing APIs from a `Configuration` instance: |
| 23 | + |
| 24 | +```csharp |
| 25 | +var config = new Configuration |
| 26 | +{ |
| 27 | + ClientId = clientId, |
| 28 | + ClientSecret = clientSecret |
| 29 | +}; |
| 30 | + |
| 31 | +var generateApi = new GenerateApi(config); |
| 32 | +var recognizeApi = new RecognizeApi(config); |
| 33 | +var scanApi = new ScanApi(config); |
| 34 | +``` |
| 35 | + |
| 36 | +If the task is repo maintenance inside `submodules/dotnet`, read `references/repo-workflow.md`. If the task needs style-matching examples or snippet locations, read `references/snippet-map.md`. |
| 37 | + |
| 38 | +## Authentication |
| 39 | + |
| 40 | +Use one of these two patterns: |
| 41 | + |
| 42 | +1. Let the SDK fetch JWT tokens for you. |
| 43 | + |
| 44 | +```csharp |
| 45 | +var config = new Configuration |
| 46 | +{ |
| 47 | + ClientId = clientId, |
| 48 | + ClientSecret = clientSecret |
| 49 | +}; |
| 50 | +``` |
| 51 | + |
| 52 | +2. Inject a pre-fetched bearer token. |
| 53 | + |
| 54 | +```csharp |
| 55 | +var config = new Configuration |
| 56 | +{ |
| 57 | + JwtToken = token |
| 58 | +}; |
| 59 | +``` |
| 60 | + |
| 61 | +Setting `Configuration.JwtToken` changes `AuthType` from JWT to external bearer-token mode. Do not also expect `ClientId` and `ClientSecret` to be used after that. |
| 62 | + |
| 63 | +Prefer `new GenerateApi(config)` and the equivalent `RecognizeApi`/`ScanApi` constructors. Convenience constructors exist, but their parameter order is `new GenerateApi(clientSecret, clientId)` and the same pattern applies to the other API classes. That order is easy to reverse, so avoid it unless there is a strong reason. |
| 64 | + |
| 65 | +Inside this repo, `TestsBase` populates `Configuration` from `Tests/Configuration.json` or `TEST_CONFIGURATION_*` environment variables. Examples use `TEST_CONFIGURATION_JWT_TOKEN`; many snippets still use `TEST_CONFIGURATION_ACCESS_TOKEN`. Treat `Configuration.JwtToken` as the stable API surface and mirror the surrounding file when editing existing examples. |
| 66 | + |
| 67 | +## Choose the right API shape |
| 68 | + |
| 69 | +Pick the operation first: |
| 70 | + |
| 71 | +- `GenerateApi`: create a barcode image. |
| 72 | +- `RecognizeApi`: decode a known or limited set of barcode types and optionally tune recognition. |
| 73 | +- `ScanApi`: auto-detect any barcode types with the smallest API surface. |
| 74 | + |
| 75 | +Then pick the transport variant based on what the user has: |
| 76 | + |
| 77 | +- Public internet URL to an image: use `RecognizeAsync` or `ScanAsync`. The `fileUrl` must be a public URL, not a local path. |
| 78 | +- Local file or stream: use `RecognizeMultipartAsync` or `ScanMultipartAsync`. |
| 79 | +- Raw bytes already in memory: base64-encode them yourself and use `RecognizeBase64Async` or `ScanBase64Async`. |
| 80 | +- Small text plus simple query parameters for barcode generation: use `GenerateAsync`. |
| 81 | +- Structured generate payload or larger data: use `GenerateBodyAsync`. |
| 82 | +- Multipart form generation: use `GenerateMultipartAsync` when the caller explicitly needs multipart. |
| 83 | + |
| 84 | +Key method names: |
| 85 | + |
| 86 | +- `GenerateAsync` |
| 87 | +- `GenerateBodyAsync` |
| 88 | +- `GenerateMultipartAsync` |
| 89 | +- `RecognizeAsync` |
| 90 | +- `RecognizeBase64Async` |
| 91 | +- `RecognizeMultipartAsync` |
| 92 | +- `ScanAsync` |
| 93 | +- `ScanBase64Async` |
| 94 | +- `ScanMultipartAsync` |
| 95 | + |
| 96 | +## Non-obvious SDK rules |
| 97 | + |
| 98 | +1. `GenerateAsync`, `GenerateBodyAsync`, and `GenerateMultipartAsync` return `Stream`, not `byte[]` or a file path. Save the stream or pass it onward, and dispose it with `using` or `await using`. |
| 99 | +2. `RecognizeBase64Async` and `ScanBase64Async` expect a base64 string in the request model. The SDK does not call `Convert.ToBase64String` for you. |
| 100 | +3. `RecognizeBase64Request.BarcodeTypes` accepts multiple `DecodeBarcodeType` values. `RecognizeAsync` and `RecognizeMultipartAsync` take a single `DecodeBarcodeType`. |
| 101 | +4. `ScanApi` does not take a barcode type or recognition-quality knobs. Use it when the caller wants auto-detection. |
| 102 | +5. GET-based recognize and scan methods only work with remote files reachable by URL. For local files on disk, do not pass a local path to `fileUrl`; use multipart or base64. |
| 103 | +6. `BarcodeResponseList` may contain multiple results. Iterate `response.Barcodes` and read `BarcodeValue`, `Type`, `Region`, and `Checksum`. |
| 104 | +7. API failures throw `Aspose.BarCode.Cloud.Sdk.Api.ApiException` with an `ErrorCode` HTTP status. Turn on `Configuration.DebugMode = true` when request or response logging would help. |
| 105 | +8. The repo has a tested end-to-end pattern where a generated `Stream` is passed directly into `ScanMultipartAsync` without saving to disk first. Reuse that pattern when it fits. |
| 106 | + |
| 107 | +## Common patterns |
| 108 | + |
| 109 | +Generate and save a QR code: |
| 110 | + |
| 111 | +```csharp |
| 112 | +var config = new Configuration { ClientId = clientId, ClientSecret = clientSecret }; |
| 113 | +var api = new GenerateApi(config); |
| 114 | + |
| 115 | +await using Stream generated = await api.GenerateAsync( |
| 116 | + EncodeBarcodeType.QR, |
| 117 | + "hello from .NET", |
| 118 | + imageFormat: BarcodeImageFormat.Png, |
| 119 | + textLocation: CodeLocation.None); |
| 120 | + |
| 121 | +await using FileStream file = File.Create("qr.png"); |
| 122 | +await generated.CopyToAsync(file); |
| 123 | +``` |
| 124 | + |
| 125 | +Recognize specific barcode types from bytes already in memory: |
| 126 | + |
| 127 | +```csharp |
| 128 | +var bytes = await File.ReadAllBytesAsync("many-types.png"); |
| 129 | +var request = new RecognizeBase64Request |
| 130 | +{ |
| 131 | + BarcodeTypes = new List<DecodeBarcodeType> |
| 132 | + { |
| 133 | + DecodeBarcodeType.QR, |
| 134 | + DecodeBarcodeType.Code128 |
| 135 | + }, |
| 136 | + FileBase64 = Convert.ToBase64String(bytes) |
| 137 | +}; |
| 138 | + |
| 139 | +BarcodeResponseList result = await new RecognizeApi(config).RecognizeBase64Async(request); |
| 140 | +``` |
| 141 | + |
| 142 | +Auto-scan a local stream without specifying the barcode type: |
| 143 | + |
| 144 | +```csharp |
| 145 | +await using Stream image = File.OpenRead("unknown.png"); |
| 146 | +BarcodeResponseList result = await new ScanApi(config).ScanMultipartAsync(image); |
| 147 | +``` |
| 148 | + |
| 149 | +## Working in this repo |
| 150 | + |
| 151 | +Read `references/repo-workflow.md` when the task changes SDK source, tests, snippets, package metadata, or generated code in `submodules/dotnet`. |
| 152 | + |
| 153 | +Read `references/snippet-map.md` when the task needs example code, README-aligned snippets, or the closest existing pattern for a generate, recognize, or scan scenario. |
| 154 | + |
| 155 | +## Final checklist |
| 156 | + |
| 157 | +1. Use the right package and namespace pair: NuGet `Aspose.BarCode-Cloud`, namespaces `Aspose.BarCode.Cloud.Sdk.*`. |
| 158 | +2. Prefer `Configuration`-based construction and avoid swapping `clientSecret` and `clientId`. |
| 159 | +3. Choose GET only for public URLs, multipart for local streams, and base64 or body variants for bytes or structured payloads. |
| 160 | +4. Base64-encode body payloads yourself before calling `RecognizeBase64Async` or `ScanBase64Async`. |
| 161 | +5. Dispose generated or opened streams. |
| 162 | +6. Iterate `response.Barcodes` instead of assuming a single result. |
| 163 | +7. When changing the repo, validate with the submodule workflow in `references/repo-workflow.md`. |
0 commit comments