Set Barcode Source

To perform barcode recognition using the Aspose.BarCode.Cloud SDK for Java, you need to set the source of the barcode image. Methods of this SDK support loading barcode images from multiple sources, such as file URL, file, and base64 encoded data. This guide provides detailed examples on how to set barcode image source through different API methods.

Supported Barcode Image Formats

  • JPEG
  • TIFF
  • PNG
  • BMP
  • GIF

Supported Barcode Source Formats

  • File URL: Link directly to an image URL.
  • File: File for processing.
  • Base64 Encoded Image: Convert the image to a base64 encoded string for secure transfer.

Examples for ScanApi

  • Using GET Request with File URL
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.ScanApi;
import com.aspose.barcode.cloud.model.BarcodeResponseList;
import com.aspose.barcode.cloud.requests.ScanRequestWrapper;

import java.net.URI;

public class ScanGet {
    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");
        }

        ScanApi scanApi = new ScanApi(client);

        try {

            ScanRequestWrapper ScanRequestWrapper =
                    new ScanRequestWrapper(
                            new URI(
                                    "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"));

            BarcodeResponseList scanResponse = scanApi.scan(ScanRequestWrapper);

            System.out.print("Barcode value: ");
            System.out.println(scanResponse.getBarcodes().get(0).getBarcodeValue());
        } catch (Exception e) {
            System.err.println("Error");
            e.printStackTrace();
            System.exit(1);
        }
    }
}
  • Using POST Request with Base64 Encoded Image
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.ScanApi;
import com.aspose.barcode.cloud.model.BarcodeResponseList;
import com.aspose.barcode.cloud.model.ScanBase64Request;
import com.aspose.barcode.cloud.requests.ScanBase64RequestWrapper;

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

public class ScanBody {
    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");
        }

        ScanApi scanApi = new ScanApi(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, "QR_and_Code128.png")));
            byte[] fileContent = Files.readAllBytes(file.toPath());
            String encodedFile = Base64.getEncoder().encodeToString(fileContent);

            ScanBase64Request scanBase64Request = new ScanBase64Request(encodedFile);

            ScanBase64RequestWrapper ScanRequestWrapper =
                    new ScanBase64RequestWrapper(scanBase64Request);
            BarcodeResponseList scanResponse = scanApi.scanBase64(ScanRequestWrapper);

            System.out.print("Barcode value: ");
            System.out.println(scanResponse.getBarcodes().get(0).getBarcodeValue());
        } catch (Exception e) {
            System.err.println("Error");
            e.printStackTrace();
            System.exit(1);
        }
    }
}
  • Using POST Request with File Stream
import com.aspose.barcode.cloud.ApiClient;
import com.aspose.barcode.cloud.api.ScanApi;
import com.aspose.barcode.cloud.model.BarcodeResponseList;
import com.aspose.barcode.cloud.requests.ScanMultipartRequestWrapper;

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

public class ScanMultipart {
    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");
        }

        ScanApi scanApi = new ScanApi(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, "QR_and_Code128.png")));

            ScanMultipartRequestWrapper ScanRequestWrapper = new ScanMultipartRequestWrapper(file);

            BarcodeResponseList scanResponse = scanApi.scanMultipart(ScanRequestWrapper);

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

Examples for RecognizeApi

  • Using GET Request with File URL
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.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 {

            BarcodeResponseList response =
                    recognizeApi.recognize(
                            new RecognizeRequestWrapper(
                                    DecodeBarcodeType.QR,
                                    new URI(
                                            "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png")));

            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 POST Request with Base64 Encoded Image
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.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, "pdf417Sample.png")));
            byte[] fileContent = Files.readAllBytes(file.toPath());
            String encodedString = Base64.getEncoder().encodeToString(fileContent);

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

            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 POST Request with File Stream
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.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, "QR_and_Code128.png")));

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

            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

Using the Aspose.BarCode.Cloud SDK for .NET, you can easily load and recognize barcode images in file URL, file, and base64 encoded formats.

Optimize your barcode recognition workflows by selecting the most appropriate method based on your specific image format and application requirements.