QRCode Decode from PDF using Thoughtworks QRCode

Jan 5 2021 8:45 AM
Using Thoughtworks and spirepdf trying to decode QR code
 
Check the code below
  1. private static void GetImages(string sourcePdf, string outputPath)  
  2. {  
  3. PdfDocument doc = new PdfDocument();  
  4. doc.LoadFromFile(sourcePdf);  
  5. outputPath = System.IO.Path.Combine(outputPath, String.Format(@"{0}.jpg", 0));  
  6. for (int i = 0; i < doc.Pages.Count; i++)  
  7. {  
  8. string QRCodeString = "";  
  9. PdfPageBase page = doc.Pages[i];  
  10. System.IO.Stream[] images = page.ExtractImages();  
  11. foreach (System.IO.Stream image in images)  
  12. {  
  13. QRCodeString = GetQRCodeString(Bitmap.FromStream(image), outputPath);  
  14. if (QRCodeString == "")  
  15. {  
  16. continue;  
  17. //QRCodeString == "Not supported in Spire.Barcode Evaluation version. " ||  
  18. }  
  19. else  
  20. {  
  21. QRCodeDecoder decoder = new QRCodeDecoder();  
  22. String decodedString = decoder.decode(new QRCodeBitmapImage(new System.Drawing.Bitmap(image)), System.Text.Encoding.UTF8);  
  23. Console.WriteLine(decodedString);  
  24. }  
  25. }  
  26. Console.WriteLine(QRCodeString);  
  27. }  
  28. }  
  29. private static string GetQRCodeString(System.Drawing.Image img, string outPutPath)  
  30. {  
  31. img.Save(outPutPath, System.Drawing.Imaging.ImageFormat.Jpeg);  
  32. string scaningResult = Spire.Barcode.BarcodeScanner.ScanOne(outPutPath);  
  33. System.IO.File.Delete(outPutPath);  
  34. return scaningResult;  
  35. }  
It is working for those pdf having Bar code at the top right corner. And showing for other positions
 
Error: ThoughtWorks.QRCode.ExceptionHandler.DecodingFailedException: 'Give up decoding'
 
Please help

Answers (1)