Parameter not Valid trying to read barcodes from a stream

Hey guys,

Doing some testing here on Aspose.Barcode and trying to read from a stream which originates in an Amazon S3 bucket. If I use a local filename, it works fine. If I try and pass barCodeReader a stream, I get the following Barcodereader.JPG (76.1 KB)

Code is pretty simple and here for convenience as well as in the screenshot :slight_smile:

            AmazonS3Client client = new AmazonS3Client();
            GetObjectRequest request = new GetObjectRequest();
            request.BucketName = newdoc.BucketName;
            request.Key = newdoc.ObjectKey;
            GetObjectResponse response = client.GetObject(request);
            Stream docStream = new MemoryStream();
            response.ResponseStream.CopyTo(docStream);
            BarCodeReader barCodeReader = new BarCodeReader(docStream, DecodeType.Code39Extended);
            barCodeReader.RecognitionMode = RecognitionMode.MaxBarCodes;
            while (barCodeReader.Read())
            {
                Console.WriteLine(barCodeReader.GetCodeText());
            }

Any ideas ? I’m using v 17.6.0 installed via NuGet and a temporary license file.

Thanks

Carl

@carlnoffki,

Please forward us a complete sample project. We will try to reproduce the issue and will update you about our findings.

There really isn’t any more to it than that - it just gets the S3 object defined by newdoc.BucketName and newdoc.ObjectKey… I can send you a project though please bear with me

@carlnoffki,

The issue you are facing could be due to following two issues:

1. File in the amazon storage is not an image. So, actual file is needed to be checked.
2. Not all bytes of the file are copied to MemoryStream. 

Please try the following code snippet at your end.

Stream docStream = new MemoryStream();
client.GetResponse().GetResponseStream().CopyTo(docStream);
docStream.Flush();

Hi Ikram,

I am happy to say that your suggestions helped to solve my problem . I changed it to save the file to disk first and I could see that it wasn’t a complete file so once that was fixed using your suggestion it works fine !

Am I correct in assuming that in order to read a barcode from a PDF I need to convert the pdf to an image (which I can also do in memory) and then pass that image to barcodereader ?

Thanks

Carl

@carlnoffki,

Thank you for your feedback. Yes, you are correct. You have to extract image from PDF document, save the extracted image as stream or on disk and then perform barcode recognition process in order to read the barcode from PDF document. Please visit the link Recognize Barcode from PDF Document for details.

Thats great, thanks - I will continue with my evaluation and testing

@carlnoffki,

Thank you. Feel free to contact us in case of any query or comments.

Hello, sorry to trouble you again. I purchased the barcode component and I was just trying to finish this project using the referred article you sent me on extracting the image from the PDF. It wasn’t working so I simplified my code to just load the PDF from disk (rather than pulling the stream from S3) and save the image to disk (rather than passing the imagestream to barcodereader). Its saving a 0 byte image so something isn’t quite right. I’ve attached the pdf I need to read the barcode from . Any insights you can give would be most valuable.

L126-20170918094127072.pdf (27.2 KB)

Thanks

Carl

@carlnoffki,

We have evaluated the sample PDF. We are able to recognize the barcode using the latest version of Aspose.BarCode i.e. 17.8. Sample code snippet is given below for your reference.

CODE:

Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
            pdfExtractor.BindPdf(@"L126-20170918094127072.pdf");
            
            // 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
                System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
                pdfExtractor.GetNextImage(imageStream);
                imageStream.Position = 0;

                Console.WriteLine("Recognizing barcode....");
                Aspose.BarCode.BarCodeRecognition.BarCodeReader barcodeReader = 
                    new Aspose.BarCode.BarCodeRecognition.BarCodeReader(imageStream);

                while (barcodeReader.Read())
                {
                    Console.WriteLine("Codetext found: " + barcodeReader.GetCodeText() + ", Symbology: " + barcodeReader.GetCodeType().ToString());
                }
                // close the reader
                barcodeReader.Close();

OUTPUT:

Codetext found: 12894756C, Symbology: Code39Standard

Thank you for testing. Could it be my (very) old copy of Apose.pdf that is causing the issue?

@carlnoffki,

Yes, This could be the cause of issue. We have used the latest version of Aspose.Pdf i.e. 17.9.

2 posts were split to a new topic: Renewal of Aspose.Pdf license