Set Barcode Image Kind

This documentation provides an overview of how to use the Aspose.BarCode.Cloud SDK for Java 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 Java 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.

import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.RecognizeApi;
import com.aspose.barcode.cloud.model.BarcodeResponseList;
import com.aspose.barcode.cloud.model.DecodeBarcodeType;
import com.aspose.barcode.cloud.model.RecognitionImageKind;
import com.aspose.barcode.cloud.requests.RecognizeRequestWrapper;

import java.net.URI;

public class RecognizeGet {
    public static void main(String[] args) {
        String accessToken = System.getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
        ApiClient client;

        if (accessToken != null && !accessToken.isEmpty()) {
            client = new ApiClient(accessToken);
        } else {
            client =
                    new ApiClient(
                            "Client Id from https://dashboard.aspose.cloud/applications",
                            "Client Secret from https://dashboard.aspose.cloud/applications");
        }

        RecognizeApi recognizeApi = new RecognizeApi(client);

        try {

            RecognizeRequestWrapper request =
                    new RecognizeRequestWrapper(
                            DecodeBarcodeType.MOST_COMMONLY_USED,
                            new URI(
                                    "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"));

            request.recognitionImageKind = RecognitionImageKind.PHOTO;

            BarcodeResponseList response = recognizeApi.recognize(request);

            System.out.print("Barcode value: ");
            System.out.println(response.getBarcodes().get(0).getBarcodeValue());
        } catch (Exception e) {
            System.err.println("Error");
            e.printStackTrace();
            System.exit(1);
        }
    }
}

Using barcodeRecognizeBodyPostAsync

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

import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.RecognizeApi;
import com.aspose.barcode.cloud.model.BarcodeResponseList;
import com.aspose.barcode.cloud.model.DecodeBarcodeType;
import com.aspose.barcode.cloud.model.RecognitionMode;
import com.aspose.barcode.cloud.model.RecognizeBase64Request;
import com.aspose.barcode.cloud.requests.RecognizeBase64RequestWrapper;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;

public class RecognizeBody {
    public static void main(String[] args) {
        String accessToken = System.getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
        ApiClient client;

        if (accessToken != null && !accessToken.isEmpty()) {
            client = new ApiClient(accessToken);
        } else {
            client =
                    new ApiClient(
                            "Client Id from https://dashboard.aspose.cloud/applications",
                            "Client Secret from https://dashboard.aspose.cloud/applications");
        }

        RecognizeApi recognizeApi = new RecognizeApi(client);
        Path currentRelativePath = Paths.get("");
        String currentPath = currentRelativePath.toAbsolutePath().toString();
        String testDataFolderPath = String.valueOf(Paths.get(currentPath, "test_data"));

        try {

            File file = new File(String.valueOf(Paths.get(testDataFolderPath, "ManyTypes.png")));
            byte[] fileContent = Files.readAllBytes(file.toPath());
            String encodedString = Base64.getEncoder().encodeToString(fileContent);

            RecognizeBase64Request request =
                    new RecognizeBase64Request(
                            Arrays.asList(DecodeBarcodeType.QR, DecodeBarcodeType.PDF417),
                            encodedString);

            request.setRecognitionMode(RecognitionMode.NORMAL);

            BarcodeResponseList response =
                    recognizeApi.recognizeBase64(new RecognizeBase64RequestWrapper(request));

            System.out.print("Barcode value: ");
            System.out.println(response.getBarcodes().get(0).getBarcodeValue());
        } catch (Exception e) {
            System.err.println("Error");
            e.printStackTrace();
            System.exit(1);
        }
    }
}

Using barcodeRecognizeMultipartPostAsync

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

import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.RecognizeApi;
import com.aspose.barcode.cloud.model.BarcodeResponseList;
import com.aspose.barcode.cloud.model.DecodeBarcodeType;
import com.aspose.barcode.cloud.model.RecognitionImageKind;
import com.aspose.barcode.cloud.requests.RecognizeMultipartRequestWrapper;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class RecognizeMultipart {
    public static void main(String[] args) {
        String accessToken = System.getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
        ApiClient client;

        if (accessToken != null && !accessToken.isEmpty()) {
            client = new ApiClient(accessToken);
        } else {
            client =
                    new ApiClient(
                            "Client Id from https://dashboard.aspose.cloud/applications",
                            "Client Secret from https://dashboard.aspose.cloud/applications");
        }

        RecognizeApi recognizeApi = new RecognizeApi(client);
        Path currentRelativePath = Paths.get("");
        String currentPath = currentRelativePath.toAbsolutePath().toString();
        String testDataFolderPath = String.valueOf(Paths.get(currentPath, "test_data"));

        try {

            File file = new File(String.valueOf(Paths.get(testDataFolderPath, "Pdf417.png")));

            RecognizeMultipartRequestWrapper request =
                    new RecognizeMultipartRequestWrapper(DecodeBarcodeType.PDF417, file);

            request.recognitionImageKind = RecognitionImageKind.SCANNED_DOCUMENT;

            BarcodeResponseList response = recognizeApi.recognizeMultipart(request);

            System.out.print("Barcode value: ");
            System.out.println(response.getBarcodes().get(0).getBarcodeValue());
        } catch (Exception e) {
            System.err.println("Error");
            e.printStackTrace();
            System.exit(1);
        }
    }
}

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.