Basic Barcode Recognition Parameters
Overview
Aspose.BarCode for JavaScript via C++ provides the BarCodeReader class for recognizing barcodes in images, supporting over 60 symbologies. To perform barcode recognition:
- Specify the source image containing the barcodes. The source can be a file or base64 bitmap string.
- Define the target symbologies using the DecodeType property. If not specified, the default AllSupportedTypes setting is used, which increases recognition time as all supported symbologies are checked.
Basic Recognition Parameters
To read barcodes from an image using Aspose.BarCode for JavaScript via C++, follow these steps:
- Specify the image source (e.g., file path or base64 bitmap string).
- Set the target symbologies for recognition. By default, DecodeType is set to AllSupportedTypes, which increases recognition time due to checking all supported barcode types.
In Aspose.BarCode for JavaScript via C++, barcode recognition is executed using the ReadBarCodes method. The recognition results, represented as BarCodeResult objects, can be accessed via the FoundCount property and FoundBarCodes method.
The following code demonstrates how to specify multiple target barcode types for recognition in a single image. In this example, only PDF417, QR Code, Code 39, and Code 128 barcodes will be recognized, while other types, such as DataMatrix and RM4SCC, will be ignored.
// Read multiple barcodes from an image with specified decode types
var reader = new BarCodeInstance.BarCodeReader(
`${path}multiple_codes.png`, "Pdf417,DataMatrix,QR,Code39Extended,Code128,RM4SCC"
);
console.log("ReadSimpleExample:");
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
const result = reader.FoundBarCodes(i);
console.log(`${result.CodeType}: ${result.CodeText}`);
}
reader.delete();
Get Recognition Results
To retrieve barcode recognition results, use the ReadBarCodes method. Access the recognition output through the FoundBarCodes method, which returns results of type BarCodeResult. To get the count of processed barcodes, use the FoundCount property.
Below is a code sample demonstrating how to load recognition results for the image mentioned above.
// Read multiple barcodes from an image and print the results with the count
var reader = new BarCodeInstance.BarCodeReader(
`${path}multiple_codes.png`,
"Pdf417,DataMatrix,QR,Code39Extended,Code128,RM4SCC"
);
// Read barcodes
reader.ReadBarCodes();
console.log("ReadFoundBarCodes:");
console.log(`BarCodes count: ${reader.FoundCount}`);
// Iterate through found barcodes
for (var i = 0; i < reader.FoundCount; i++) {
const result = reader.FoundBarCodes(i);
console.log(`${result.CodeType}: ${result.CodeText}`);
}
reader.delete();
Manual and Timeout Methods for Recognition Abortion
The BarCodeReader class provides options for aborting the recognition process when it is no longer feasible. This can be done in two ways:
1. Using the TimeOut Property
Set the TimeOut property to automatically interrupt the recognition process if the specified timeout value is exceeded. By default, the TimeOut property is set to 0.
2. Using the Abort Method
Call the Abort method to stop the recognition process if it was started in a separate thread. This method returns control immediately without blocking the process.
Exception Handling
Both methods of abortion result in a RecognitionAbortedException being thrown if the recognition process is not completed.
Example
The following code snippet demonstrates how to abort the recognition process and handle the exception.
// Read multiple barcodes from an image with a timeout and handle exceptions
var reader = new BarCodeInstance.BarCodeReader(
`${path}multiple_codes.png`,
"Pdf417,DataMatrix,QR,Code39Extended,Code128,RM4SCC"
);
console.log("ReadWithTimeout:");
reader.Timeout = 1000;
try {
reader.ReadBarCodes();
console.log(`BarCodes count: ${reader.FoundCount}`);
} catch (e) {
console.log(e.message);
}
reader.delete();