Android Example QR Code - Reverse Engineering a QR Code

pic1.png


I am leaving this project here for now. The majority of the work has been done to extract the critical information of a QR code - it is reported in the text box to the right of the qr code image (version information + interpreting it, format information + interpreting it, length of the message, and the message itself. No Reed Solomon error correction applied as yet.

The project expects a "perfect" QR code i.e more or less square (it will tolerate width/height distortion to a certain degree but not perspective distortion), black pixels and a white background.

Theoretically this project is capable to extract a message from any "perfect" 8-bit-byte mode QR code as long as what the image is of sufficient size:
1. version 1 to version 40
2. masking pattern 0 to 7
3. Error Level L, M, Q, and H

The code in the project is quite tolerant towards a distortion of the image in the x and y directions. I have added two spinners (selecting x and y dimensions respectively), the values of which can be changed, to test the "readability" of the QR Code by applying x and/or y distortions based on the selections of the spinners. The code assumes the QR code to be a "perfect" rectangle (i.e x=y or x >= y or x <= y). Distortions between the width of the top and the width of the bottom (or the length of the left and the length of the right) are not accommodated as yet in the code.

You need to extract the files in qrcodev21ehm6.zip and qrcodev4elm5.zip into the /files folder of the project (files were too big to add to the zipped project). This is the png files that the project makes use of (hope they are all in the zipped files - if not, let me know and I will post those that are missing)

Click on the QR code image to cycle through the various QR codes. Enable the checkbox to see the points inside the QR code that are read to extract the binary string that is then used to decode the data/information from.
 

Attachments

  • JHSQRCODEREADV4.zip
    19.7 KB · Views: 983
  • qrcodev21ehm6.zip
    443.9 KB · Views: 1,026
  • qrcodev4elm5.zip
    206 KB · Views: 958
Last edited:

ivan.tellez

Active Member
Licensed User
Longtime User
WOW, this ins a great proyect.

There is only a big problem. It only works wit "perfect" images. For example if you use a jpg image, it dosnt work. Maybe you can add some image processing to eliminate the compression artifacts and other noises.

thanks for sharing.
 

Mark Zraik

Member
Licensed User
Longtime User
I am leaving this project here for now. The majority of the work has been done to determine the critical information of a QR code - it is reported in the text box to the right of the qr code image. Establishing the remainder of the info in the qr code (the balance of the reversed masking patterns, the message words, and the error correction words) is actually now very simple to do for whoever wants to take it further.

The code in the project is quite tolerant towards a distortion of the image in the x and y directions. I have added two spinners (selecting x and y dimensions respectively), the values of which can be changed, to test the "readability" of the QR Code by applying x and/or y distortions based on the selections of the spinners. The code assumes the QR code to be a "perfect" rectangle (i.e x=y or x >= y or x <= y). Distortions between the width of the top and the width of the bottom (or the length of the left and the length of the right) are not accommodated as yet in the code.

All the QR code module values are now stored in matrix "qrcode". Easy to work with it from here onwards.

If someone can come up with code to capture a qr code image and pass it on as a cropped png or jpg image then we can write our own B4A QR code scanner/interpreter...

Try QRCPV2 and QRCPV3 from here http://www.b4x.com/android/forum/threads/qr-code-library.41408/#post-259448[/quote}

I downloaded this for later consumption. Thank you Johan!
Coroner
 

Johan Schoeman

Expert
Licensed User
Longtime User
qrphoto7.jpg


qrphoto8.jpg


Finding finding patterns in QR Codes...
You will need libraries MathSupport V1.02, Jpeg V1.10, and StringFunctions V1.05.
The project finds all vertical and horizontal transitions with a 1:1:3:1:1 ratio (typical ratio of the bwbbbwb composition of the Finding Pattern of a QR Code) and highlights them in red.
The image on the left is a RGB image (taken with camera)
The centre image is a grey scaled image of the image on the left (128 shades of grey)
The image on the right is a binarized image of the centre image based on Otsu thresholding
There are 5 different images - click on the left most image view to cycle through the 5 images (and be patient - it takes a while to come up with the new highlighted image :))
Notice extensive red highlighting of Finding Patterns - that is after all where we would expect most of the bwbbbwb patterns to be present. The code only scans every third line (first left-to-right, then top-to-bottom) to find 1:1:3:1:1 ratios. Only ratios that comply with bwbbbwb are highlighted.

Edit
Add attached qrphoto7.jpg and qrphoto8.jpg to the /files folder of the project and modify the code in ImageView1_Click accordingly to make use of them. Even though there are lots of "noise" in the two pics the concentration of red highlighted areas is still in the region of the finding patterns.
 

Attachments

  • JHS_QR_FP.zip
    345.6 KB · Views: 804
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Post #1 updated with the 2 x zipped files that contain the png files that the project in post #1 calls for. These files/images were created with project QRCPV3 (QRCPV4 will create exactly the same) that are posted elsewhere in the B4A forum
 

Johan Schoeman

Expert
Licensed User
Longtime User
Narrowing in on Finding Patterns of QR codes...
The attached project ---> applied some "filtering" to eliminate some of the false Finding Patterns (FP) that are just "noisy".
The left most imageview is a RGB picture taken with a camera
The centre imageview is a grey scaled image of the left most imageview (128 shades of grey)
The right most imageview is a binary image of the centre imageview. It was generated by making use of Otsu Thresholding.

Whatever is now highlighted in yellow used to be highlighted in red as well.
Yellow highlighted areas are now the only possible areas where finding patterns could reside (whatever is still in red is no longer possible FP areas)

Click on the left most imageview to cycle through the 8 different images (but be patient....it takes a while to process individual images).

Most images are surrounded by plenty of other "noise" in order to test the projects ability to locate areas that could possibly hold Finding Patterns. The "noisy" images were photographed from a news paper made of yellowish recycled paper to add some complexity to the identification of possible FP areas.
 

Attachments

  • JHS_QR_FP_1.zip
    454 KB · Views: 754
Last edited:
Top