Set Recognition Quality and Speed
When working with barcodes in applications, optimizing recognition quality and speed is essential. The RecognitionMode
setting in the Aspose.BarCode.Cloud SDK for Java offers a way to adjust these parameters, making it possible to prioritize either quality or speed during barcode recognition.
Recognition Mode Options
The RecognitionMode
parameter can be set to the following values to control the recognition process:
- Fast: Optimizes for speed but may lower the accuracy slightly. Minimal recognition timeout.
- Normal: Balances speed and quality, suitable for most cases. Medium recognition timeout.
- Excellent: Prioritizes accuracy over speed, ideal for challenging barcode images. Maximum recognition timeout.
Usage in API Requests
The RecognizeApi
interface provides methods to recognize barcodes from files using different HTTP request types. Below are the available methods:
barcodeRecognizeGetAsync
: Recognizes barcode from a file on the server using aGET
request.barcodeRecognizeBodyPostAsync
: Recognizes barcode from a file provided in the request body using aPOST
request with JSON or XML format.barcodeRecognizeMultipartPostAsync
: Recognizes barcode from a file provided in the request body using aPOST
request withmultipart/form-data
.
Example Implementations
Example 1: Using barcodeRecognizeGetAsync
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.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.recognitionMode = RecognitionMode.FAST;
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);
}
}
}
Example 2: Using 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);
}
}
}
Example 3: Using barcodeRecognizeMultipartPostAsync
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.CLEAR_IMAGE;
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
Choosing the appropriate RecognitionMode
is essential in tailoring barcode recognition to the specific needs of your application. Whether prioritizing speed or accuracy, the examples provided demonstrate how to use the RecognizeApi
interface to adjust settings for optimal performance. By fine-tuning RecognitionMode
and RecognitionImageKind
, you can enhance both efficiency and accuracy, making your barcode processing solution robust across various use cases.