Set Barcode Image Kind

This documentation provides an overview of how to use the Aspose.BarCode.Cloud SDK for .NET to set the type of barcode image for recognition. Setting the correct RecognitionImageKind is essential for optimizing the recognition process and ensuring high accuracy, particularly when dealing with diverse barcode types or varying image qualities.

Introduction

The Aspose.BarCode.Cloud SDK for .NET allows for flexible barcode scanning and recognition from different image sources. One of the features in this API is the ability to specify the RecognitionImageKind based on the source of the barcode image. This improves accuracy by guiding the recognition engine with additional context about the image.

Usage in API Requests

The RecognitionImageKind parameter is optional and can be set in several requests within the API. It is available in the following request classes:

  • BarcodeRecognizeGetAsync
  • BarcodeRecognizeBodyPostAsync
  • BarcodeRecognizeMultipartPostAsync

Image Kind Options

The RecognitionImageKind enum provides three options:

  • Photo: Use this option when recognizing barcodes from smartphone cameras. High resolution. Barcode is a large part of the image.
  • ScannedDocument: Select this for scanned documents pages. Medium resolution. Barcode is a small part of the image.
  • ClearImage: Ideal for clean, digitally-rendered barcode images with minimal background noise or preprocessed photos. Low resolution. The barcode is almost the entire image.

Examples

Below are examples of using the RecognitionImageKind setting with various API methods.

Using BarcodeRecognizeGetAsync

The BarcodeRecognizeGetAsync method allows for barcode recognition using a GET request.

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());

        BarcodeResponseList result = await recognizeApi.RecognizeAsync(DecodeBarcodeType.QR,
         "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png",
         recognitionImageKind: RecognitionImageKind.Photo);

        Console.WriteLine($"File recognized, result: '{result.Barcodes[0].BarcodeValue}'");
    }
}

Using BarcodeRecognizeBodyPostAsync

For recognition using a POST request with JSON or XML, use BarcodeRecognizeBodyPostAsync:

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",
            "aztec.png"
        ));

        byte[] imageBytes = await File.ReadAllBytesAsync(fileName);
        string imageBase64 = Convert.ToBase64String(imageBytes);


        var request = new RecognizeBase64Request
        {
            BarcodeTypes = new List<DecodeBarcodeType> { DecodeBarcodeType.Aztec, DecodeBarcodeType.QR },
            FileBase64 = imageBase64,
            RecognitionImageKind = RecognitionImageKind.ScannedDocument
        };

        BarcodeResponseList result = await recognizeApi.RecognizeBase64Async(request);

        Console.WriteLine($"File '{fileName}' recognized, results: value: '{result.Barcodes[0].BarcodeValue}', type: {result.Barcodes[0].Type}");

    }
}

Using BarcodeRecognizeMultipartPostAsync

For file uploads with multipart form data, BarcodeRecognizeMultipartPostAsync is used:

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);

        BarcodeResponseList result = await recognizeApi.RecognizeMultipartAsync(DecodeBarcodeType.MostCommonlyUsed,
        fileStream, recognitionImageKind: RecognitionImageKind.ClearImage);

        Console.WriteLine($"File '{fileName}' recognized, result: '{result.Barcodes[0].BarcodeValue}'");
    }
}

Conclusion

The Barcode Recognition API provides flexible options to recognize barcodes in various formats including photos, scanned documents, and clear images. By setting the RecognitionImageKind, developers can enhance recognition accuracy, particularly when working with diverse image sources. Supported barcode types include popular formats like QR Code, Aztec, Pdf417, and many more. The API allows integration with GET and POST methods, supporting JSON, XML, and multipart form data formats.

For high accuracy, configure the RecognitionImageKind parameter based on your image source. This small adjustment can lead to faster processing times and better recognition results across all supported barcode types.