Hello,
I am experiencing a very strange issue with the BarCodeReader.Read() method. When my program gets to reader.Read(), the loop is never entered; which to me indicates that the barcode cannot be read. However, If I comment out the while loop and execute the various reader methods the barcode can be read successfully.
For example, the following code snippet will result in a barcode not being read:
BarCodeReader reader = new BarCodeReader(@"C:\barcode.jpg", BarCodeReadType.Code128);
while (reader.Read())
{
Console.Write("Code Text: " + reader.GetCodeText());
Console.Write("Symbology Type: " + reader.GetReadType());
}
reader.Close();
The following will result in a barcode successfully being read without issue:
BarCodeReader reader = new BarCodeReader(@"C:\barcode.jpg", BarCodeReadType.Code128);
Console.Write("Code Text: " + reader.GetCodeText());
Console.Write("Symbology Type: " + reader.GetReadType());
reader.Close();
I have attached a copy of the PDF with barcode.
Thank you!
Hi Jaden,
Thank you for contacting support. We have tested your sample PDF document against the latest build of Aspose.BarCode 6.7.0. Please note, without calling read method of BarCodeReader class, the GetCodeText method returns an empty string. Similarly, GetReadType method returns -1 value on my side. You have provided a source PDF document. How you are extracting barcode from it. Please recheck once, if the problem persists, then please share complete source or steps to extract barcode image.
We’re looking forward to help you.
Thank you for your prompt response. Here is the code I am using to open the PDF and attempt to read the barcode.
Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
pdfExtractor.BindPdf(“C:\pathtofile.pdf”);
while (pdfExtractor.HasNextImage())
{
System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
pdfExtractor.GetNextImage(imageStream);
imageStream.Position = 0;
using (Aspose.BarCodeRecognition.BarCodeReader barcodeReader = new Aspose.BarCodeRecognition.BarCodeReader(imageStream, BarCodeReadType.Code128))
{
barcodeReader.ImageBinarizationHints = RecognitionHints.ImageBinarization.Grayscale;
barcodeReader.ImageBinarizationHints = RecognitionHints.ImageBinarization.MedianSmoothing;
barcodeReader.ImageBinarizationHints = RecognitionHints.ImageBinarization.ComplexBackground;
if (barcodeReader.Read().Equals(true))
{
while (barcodeReader.Read())
{
string barcodeCodeText = barcodeReader.GetCodeText();
string barcodeReadType = barcodeReader.GetReadType().ToString();
float barcodeQuality = barcodeReader.GetRecognitionQuality();
System.Console.WriteLine("Codetext found: " + barcodeCodeText + ", Symbology: " + barcodeReadType + ", Quality: " + barcodeQuality.ToString());
}
barcodeReader.Close();
}
}
}
Unfortunately, the above code only works if I comment out while (barcodeReader.Read()). I can set a breakpoint on the while loop and watch it skip right over everything inside. However, if I comment out while (barcodeReader.Read()) the debugger goes inside and I can see all the values.
Thank you.
Hi Jaden,
Thank you for supplying the source code. In the source code, you’re calling read method two times. The barcode image has only one barcode, so it returns true value once. Please comment if statement and run this sample code again. It works perfect on my side. We always require to call the Read method before getting barcode text, otherwise it’ll return an empty string.
We hope, this helps. Please feel free to reply us in case of any confusion or questions.
I believe I understand now. I thought that barcodeReader.Read().Equals(false) simply returned a bool of whether or not the barcode could be read and barcodeReader.Read() actually read the barcode.
What you are saying is that both barcodeReader.Read().Equals(false) and barcodeReader.Read() will trigger an attempt to read a barcode, even if that read is not successful. If that is the case, when would I use one over the other?
I ask because in my process I need to know when a read failed so that I can log that failure and react to it in some way.
Hi Jaden,
Thank you for your inquiry. In case, the barcode image has three barcodes and Aspose API can only read two barcodes, then Read method call will return true values two times. First time call of the Read method allows to get first barcode credentials and second time call allows to get second barcode credentials. After this, it always returns false value. We can get barcode credentials, e.g. by calling GetCodeText and GetReadType methods.
If you think that Aspose.BarCode API is not reading some other barcodes, then please share these barcodes sample images. We’ll take a closer look and guide you accordingly.
Sorry for replying on old thread but I recently started evaluation of Aspose for my project and tried to run example on the github: aspose.barcode - npm and I am running into similar issue. I get these errors:
while (reader.Read())
^
TypeError: reader.Read is not a function
if I comment the reader.Read() I get next error:
console.log(reader.getCodeText(false));
^
TypeError: reader.getCodeText is not a function
The full code for reference is:
const barcode = require(“aspose.barcode”);
const test_assist = require(‘./tests_assist’);
const fs = require(“fs”);
let BarcodeGenerator = barcode.AsposeBarcode.Generator.BarcodeGenerator;
let BarcodeReader = barcode.AsposeBarcode.Reader.BarcodeReader;
let EncodeTypes = barcode.AsposeBarcode.Generator.EncodeTypes;
test_assist.set_license();
let generator = new BarcodeGenerator(EncodeTypes.CODE_128, “12367891011”);
let file_path = “./resources/generating/setBarcodeType.png”; // I changed it for my local file
generator.save(file_path, “PNG”);
let image_data_base64 = fs.readFileSync(file_path).toString(‘base64’);
let reader = new BarcodeReader(image_data_base64, null, DecodeType.ALL_SUPPORTED_TYPES);
if (reader.read()) // I tried reader.read() as well as reader.Read() both failed
{
console.log("Recognized barcode code text: " + reader.getCodeText(false) + “\n”);
console.log("Recognized barcode code type: " + reader.getCodeTypeName() + “\n”);
}
Nevermind, I dug through the Reader.js and seems like the example on npmjs site is incorrect. Here is the corrected one:
const barcode = require(“aspose.barcode”);
let Reader = barcode.AsposeBarcode.Reader;
let BarcodeReader = barcode.AsposeBarcode.Reader.BarcodeReader;
let full_path = “./uploads/setBarcodeType.png”; // location and file name
let reader = new BarcodeReader(full_path, null, DecodeType.PDF_417);
reader.setQualitySettings(Reader.QualitySettings.getHighPerformance());
reader.getQualitySettings().setAllowMedianSmoothing(true);
reader.getQualitySettings().setMedianSmoothingWindowSize(5);
reader.readBarCodes().forEach(function(result, i, results)
{
console.log(result.getCodeText(true));
console.log("\n");
console.log(result.getCodeTypeName());
});
@tariq101,
Good to know that you have sorted it out now. And, yes, we removed BarcodeReader.read() method sometime ago and introduced more enhanced BarcodeReader.readBarCodes() method which should be used instead. We also mentioned this about the APIs in official release notes of Aspose.BarCode for Node.js via Java (Public API and Backward Incompatible Changes section). We also recommend you to kindly browse through the documentation with example codes for your reference which are up-do-date.
We will also update the examples on npmjs page accordingly soon.