Set Target Barcode Types
This guide provides an overview of setting target barcode types using the Aspose.BarCode.Cloud SDK for Java. 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 RecognizeApi
The RecognizeApi
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.
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.MOST_COMMONLY_USED,
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);
}
}
}
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.
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.CODE128),
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);
}
}
}
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.
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, "Pdf417.png")));
RecognizeMultipartRequestWrapper request =
new RecognizeMultipartRequestWrapper(DecodeBarcodeType.PDF417, 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);
}
}
}
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 RecognizeApi
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.