Reading Barcode Issue

The following code cannot read barcode from the attached PDF file. I have tried my best but it is not working. Please help. I am using the v4.0.30319 version of Aspose.BarCode.dll and v4.0.30319 of Aspose.Pdf.Kit.dll.

using System;

using System.Diagnostics;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Aspose.BarCodeRecognition;

using Aspose.Pdf;

using Aspose.Pdf.Kit;

using System.IO;

using System.Drawing;

namespace ASPOSBarcodeConsoleApp

{

class Program

{

static void Main(string[] args)

{

PdfExtractor pdfExtractor = new PdfExtractor();

pdfExtractor.BindPdf(@"C:\\inetpub\\www\\BarCode.pdf");

// set page range for image extraction

pdfExtractor.StartPage = 1;

pdfExtractor.EndPage = 1;

// extract the images

Console.WriteLine("Extracting images.....");

pdfExtractor.ExtractImage();

// save images to stream in a loop

while (pdfExtractor.HasNextImage())

{

Console.WriteLine("Getting next image....");

// save image to stream

MemoryStream imageStream = new MemoryStream();

pdfExtractor.GetNextImage(imageStream);

imageStream.Position = 0;

Console.WriteLine("Recognizing barcode....");

// recognize the barcode from the image stream above

BarCodeReader barcodeReader = new BarCodeReader(imageStream, BarCodeReadType.Code39Standard);

while (barcodeReader.Read())

{

Console.WriteLine("Codetext found: " + barcodeReader.GetCodeText() + ", Symbology: " + barcodeReader.GetReadType().ToString());

}

// close the reader

barcodeReader.Close();

}

Console.Read();

}

}

}

v4.0.30319

Hello Nauman,

Thank you for inquiry.

The pdf contains Code128 barcode, but you are specifying Code39Standard in your code, that’s why the barcode reader does not read anything. Please modify your code to include Code128 symbology as well.

BarCodeReader reader = new BarCodeReader(stream, BarCodeReadType.Code39Standard | BarCodeReadType.Code128);

This will try to read both code128 and code39standard barcodes from the image.

Thanks for the response. I have changed it as suggested and now I am getting this error:

Sorry, evaluation version only allow Code39 recognition
Please set BarCodeReader's BarCodeReadType property to be Code39Standard.

We are currently evaluating this product to read QR codes. Please let me know if we can read QR code using the evaluation license.

Thanks,

Nauman.

Hello Nauman,

The trial version can only recognize Code39Standard barcodes. For reading other barcode symbologies, you need to set a valid license.

Please visit this page to get a free 30 days temporary license for evaluation. Once you get your license, please refer to this page for setting the license in your code.

Thanks. I have obtained a temporary license file and I am able to read barcode. I have run into another issue, if I set Autosize property to false when generating barcode, the above code cannot read the QR code.

I am using the following code to generate barcode from a PDF file uploaded through file upload control:

BarCodeBuilder barcode = new BarCodeBuilder();

barcode.SymbologyType = Symbology.QR;

//barcode.QREncodeMode = QREncodeMode.Auto;

barcode.QRErrorLevel = QRErrorLevel.LevelH;

barcode.ImageQuality = ImageQualityMode.AntiAlias;

barcode.CodeText = "test|123";

barcode.AutoSize = false;

//barcode.ImageHeight = 22;

//barcode.ImageWidth = 100;

MemoryStream stream = new MemoryStream();

stream.Seek(0, SeekOrigin.Begin);

barcode.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

stream.Position = 0;

// read the stream in image to get the coordinates

Bitmap bmp = new Bitmap(stream);

int nHeight = bmp.Height;

int nWidth = bmp.Width;

stream.Position = 0;

float upperLeftX = 475;

float upperLeftY = 500;

float lowerLeftX = 10;

float lowerLeftY = 5; // Controls position from the bottom

float upperRightX = upperLeftX + bmp.Width;

float upperRightY = lowerLeftY + bmp.Height;

MemoryStream memStream = new MemoryStream(this.FileUpload1.FileBytes);

PdfFileInfo pdfinfo = new PdfFileInfo(memStream);

//Height = 1008 for portrait

//Height = 612 for landscape

MemoryStream msPDFBarcode = new MemoryStream();

PdfFileMend pdfFileMend = new PdfFileMend(memStream, msPDFBarcode);

if (pdfinfo.GetPageHeight(1) > 1000)

{

pdfFileMend.AddImage(stream, 1, lowerLeftX, lowerLeftY, upperRightX, upperRightY);

}

else

{

//Reprocessing for landscape

Bitmap bmpLS = new Bitmap(stream);

bmpLS.RotateFlip(RotateFlipType.Rotate180FlipNone);

bmpLS.RotateFlip(RotateFlipType.Rotate90FlipNone);

MemoryStream streamLS = new MemoryStream();

streamLS.Seek(0, SeekOrigin.Begin);

bmpLS.Save(streamLS, System.Drawing.Imaging.ImageFormat.Bmp);

streamLS.Position = 0;

pdfFileMend.AddImage(streamLS, 1, lowerLeftX, lowerLeftY, 50, upperRightY);

}

pdfFileMend.Close();

FileStream fs = new FileStream("C:\\Inetpub\\www\\Barcode.pdf", FileMode.Create);

fs.Write(msPDFBarcode.GetBuffer(), 0, int.Parse(msPDFBarcode.Length.ToString()));

fs.Close();

Hello Nauman,

Could you please try to also specify the GraphicsUnit, height and width of the image, in case you are setting AutoSize to false:

builder.AutoSize = false;
builder.GraphicsUnit = System.Drawing.GraphicsUnit.Millimeter;
builder.ImageHeight = 20;
builder.ImageWidth = 20;