Set Target Barcode Types
This guide provides an overview of setting target barcode types using the Aspose.BarCode.Cloud SDK for .NET. 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 IRecognizeApi
The IRecognizeApi
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 BarcodeRecognizeGetAsync
This method sends a GET request with the file URL and barcode type in the query parameters.
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace RecgonizeSnippets;
internal static class Program
{
private static Configuration MakeConfiguration()
{
var config = new Configuration();
string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_ACCESS_TOKEN");
if (string.IsNullOrEmpty(envToken))
{
config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
}
else
{
config.JwtToken = envToken;
}
return config;
}
public static async Task Main(string[] args)
{
var recognizeApi = new RecognizeApi(MakeConfiguration());
var result = await recognizeApi.RecognizeAsync(DecodeBarcodeType.MostCommonlyUsed,
"https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png");
Console.WriteLine($"File recognized, result: '{result.Barcodes[0].BarcodeValue}'");
}
}
In this example, most commonly used barcode symbologies will be recognized from the provided image URL.
Using BarcodeRecognizeBodyPostAsync
with multiple Barcode Types
For base64-encoded images, use the BarcodeRecognizeBodyPostAsync
method, which allows for flexible recognition across multiple barcode types.
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace RecognizeSnippets;
internal static class Program
{
private static Configuration MakeConfiguration()
{
var config = new Configuration();
string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_ACCESS_TOKEN");
if (string.IsNullOrEmpty(envToken))
{
config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
}
else
{
config.JwtToken = envToken;
}
return config;
}
public static async Task Main(string[] args)
{
var recognizeApi = new RecognizeApi(MakeConfiguration());
string fileName = Path.GetFullPath(Path.Join("Tests", "test_data",
"ManyTypes.png"
));
byte[] imageBytes = await File.ReadAllBytesAsync(fileName);
string imageBase64 = Convert.ToBase64String(imageBytes);
var request = new RecognizeBase64Request
{
BarcodeTypes = new List<DecodeBarcodeType> { DecodeBarcodeType.QR, DecodeBarcodeType.Code128 },
FileBase64 = imageBase64
};
BarcodeResponseList response = await recognizeApi.RecognizeBase64Async(request);
Console.WriteLine($"File '{fileName}' recognized, results: ");
foreach (var result in response.Barcodes)
{
Console.WriteLine($"Value: '{result.BarcodeValue}', type: {result.Type}");
}
}
}
In this example, the API will target both QR and Pdf417 barcode types in the base64 image string.
Using BarcodeRecognizeMultipartPostAsync
For recognizing barcodes from files in a form upload, use BarcodeRecognizeMultipartPostAsync
. This method supports setting a single barcode type.
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace RecognizeSnippets;
internal static class Program
{
private static Configuration MakeConfiguration()
{
var config = new Configuration();
string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_ACCESS_TOKEN");
if (string.IsNullOrEmpty(envToken))
{
config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
}
else
{
config.JwtToken = envToken;
}
return config;
}
public static async Task Main(string[] args)
{
var recognizeApi = new RecognizeApi(MakeConfiguration());
string fileName = Path.GetFullPath(Path.Join("Tests", "test_data",
"Pdf417.png"
));
using var fileStream = new FileStream(fileName, FileMode.Open);
var result = await recognizeApi.RecognizeMultipartAsync(DecodeBarcodeType.Pdf417,
fileStream);
Console.WriteLine($"File '{fileName}' recognized, result: '{result.Barcodes[0].BarcodeValue}'");
}
}
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 IRecognizeApi
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.