Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sample code to detect QRCode in an image

Tags:

I use this code in C# to decode (not detect) a QRCode and it works:

LuminanceSource ls = new RGBLuminanceSource(image, image.Width, image.Height); Result result = new QRCodeReader().decode(new BinaryBitmap(new HybridBinarizer(ls))); 

Now I would like to detect a QRCode in a more complex image with a lot of other stuffs such images and text. I'm not able to understand how to accomplish this because I cannot find any sample and transforming Bitmap (C#) to Bitmatrix for Detector (zxing) is not so direct.

Does anyone have a piece of code to give me?

thanks a lot


UPDATE


I try this code but I get a ReaderException:

The code:

LuminanceSource ls = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);             QRCodeMultiReader multiReader = new QRCodeMultiReader(); Result[] rs = multiReader.decodeMultiple(new BinaryBitmap(new HybridBinarizer(ls)), hints);  return rs[0].Text; 

The exception

com.google.zxing.ReaderException:  in com.google.zxing.qrcode.detector.FinderPatternFinder.selectBestPatterns()    in com.google.zxing.qrcode.detector.FinderPatternFinder.find(Hashtable hints)    in com.google.zxing.qrcode.detector.Detector.detect(Hashtable hints)    in com.google.zxing.qrcode.QRCodeReader.decode(BinaryBitmap image, Hashtable hints)    in com.google.zxing.qrcode.QRCodeReader.decode(BinaryBitmap image)    in ...Logic.BarCodeManager.QRCodeReader(Bitmap bitmap) in  

UPDATE 02/12/2011


I have just tried to scan the printed QRCode (with the piece of code on the top of the post) with an App on my iPhone and it works well! So the problem is surely in the detection/decode phase.

like image 507
robob Avatar asked Dec 01 '11 10:12

robob


People also ask

Will a QR code work from a picture?

Yes you can. Please make sure the QR code is clear so our partner can scan it. We can't be held responsible if an unclear picture or screenshot prevents you from redeeming the code.

Can you scan a QR code from a screenshot?

Open the Google Lens app, and go to the “image” option. Select the screenshot with the QR code. The app will scan the code, and generate its link in seconds. You can select the “only the QR code” option if there is text, or any other element on the image.


2 Answers

QR Codes always have the three squares in the top left, top right, bottom left corners. Knowing this you should be able to search for that square pattern within the pixel data of the image you are parsing, to figure out the top left, width and height of the qr code with a bit of simple logic parsing.

like image 59
willthiswork89 Avatar answered Oct 23 '22 18:10

willthiswork89


Though it's old. I still want to post it in case someone needs it. The noise of images makes them difficult for zxing to detect qrcodes. The results are much better if the images are noise free. I use a simple method to reduce noise of scanned images. It can be done by shrinking the image. The shrink factor may vary by the noise of images. I found the factor 3 works fine in my case.

like image 42
Tochi Avatar answered Oct 23 '22 19:10

Tochi