Set Barcode Source
Overview
In Aspose.BarCode for JavaScript via C++, you can define the barcode recognition source in three ways: as an image file, a stream, or a bitmap. Supported image formats for barcode reading include BMP, PNG, JPEG, GIF, and TIFF. Implementation details for each mode are provided below.
Read Barcodes from File
To set an image file as the source for barcode recognition, specify the full or relative path to the file in the BarCodeReader constructor, or use the SetBarCodeImage method to pass the path to an existing BarCodeReader object.
Example
The following code snippet demonstrates how to set an image file as the barcode reading source by specifying the path to the image.
// Read multiple barcode types from an image file
var reader = new BarCodeInstance.BarCodeReader(`${path}multiple_codes.png`,
"Pdf417,DataMatrix,QR,Code39Extended,Code128,RM4SCC");
console.log("ReadFromFile:");
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
const result = reader.FoundBarCodes(i);
console.log(`${result.CodeType}: ${result.CodeText}`);
}
reader.delete();
The code sample below illustrates how to define the path to the already created recognition source object.
// Read multiple barcode types from an image file using SetBarCodeImage
var reader = new BarCodeInstance.BarCodeReader();
reader.SetBarCodeImage(`${path}multiple_codes.png`);
reader.SetBarCodeReadType("Pdf417,DataMatrix,QR,Code39Extended,Code128,RM4SCC");
console.log("ReadSetBarCodeImageFromFile:");
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
const result = reader.FoundBarCodes(i);
console.log(`${result.CodeType}: ${result.CodeText}`);
}
reader.delete();
Read Barcodes from Base64 image string
In Aspose.BarCode for JavaScript via C++, it is possible to read barcodes from base64 image string. To set the recognition source as base64 image string, it is necessary to pass the base64 image string to the BarCodeReader constructor or the SetBarCodeImage method similarly to the case of using a raster image file.
// Generate a QR barcode
var gen = new BarCodeInstance.BarcodeGenerator("QR", "ASPOSE.BARCODE");
const base64image = gen.GenerateBarCodeImage(); // Generate base64 barcode image
// Read multiple barcode types from a bitmap image
var reader = new BarCodeInstance.BarCodeReader(base64image, "Pdf417,DataMatrix,QR,Code39Extended,Code128,RM4SCC");
console.log("ReadFromBitmap:");
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
const result = reader.FoundBarCodes(i);
console.log(`${result.CodeType}: ${result.CodeText}`);
}
gen.delete();
reader.delete();