Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a BMP image in a QR code

I'm trying to create (or, if I've somehow missed it in my research, find) an algorithm to encode/decode a bmp image into/from a QR code format. I've been using a guide (Thonky) to try to understand the basics of QR codes and I'm still not sure how to go about this problem, specifically:

  • Should I encode the data as binary or would numeric be more reasonable (assuming each pixel will have a max. value of 255)?

  • I've searched for information on the structured append capabilities of QR codes but haven't found much detail beyond the fact that it's supported by QR codes -- how could I implement/utilize this functionality?

And, of course, if there are any tips/suggestions to better store an image as binary data, I'm very open to suggestions!

Thanks for your time,

Sean

like image 207
user1748532 Avatar asked Oct 15 '12 23:10

user1748532


People also ask

Can you store an image in a QR code?

Yes, a QR Code can store an image or an image gallery.

Can you store a file in a QR code?

You probably know what QR Codes are. They are basically advanced barcodes that can store a lot of data in a small space. From PowerPoint presentations to multimedia files, QR Codes can store a wide array of information. Moreover, they offer a contactless experience.

Which image format is best for QR code?

Which is the best QR Code file format to use? We highly recommend downloading your QR Codes in PNG as it is better in preserving the best image quality.

What kind of data can be stored in a QR code?

The data stored in a QR code can include website URLs, phone numbers, or up to 4,000 characters of text. QR codes can also be used to: Link directly to download an app on the Apple App Store or Google Play.


1 Answers

I'm not sure you'll be able to achieve that, as the amount of information a QR Code can hold is quite limited.

First of all, you'll probably want to store your image as raw bytes, as the other formats (numeric and alphanumeric) are designed to hold text/numbers and would provide less space to store your image. Let's assume you choose the biggest possible QR Code (version 40), with the smallest level of error correction, which can hold up to 2953 bytes of binary information (see here).

First option, as you suggest, you store the image as a bitmap. This format allows no compression at all and requires (in the case of an RGB image without alpha channel) 3 bytes per pixel. If we take into account the file header size (14 to 54 bytes), and ignore the padding (each row of image data must be padded to a length being a multiple of 4), that allows you to store roughly 2900/3 = 966 pixels. If we consider a square image, this represents a 31x31 bitmap, which is small even for a thumbnail image (for example, my avatar at the end of this post is 32x32 pixels).

Second option, you use JPEG to encode your image. This format has the advantage of using a compression algorithm that can reduce the file size. This time there is no exact formula to get the size of an image fitting in 2.9kB, but I tried using a few square images and downsizing them until they fit in this size, keeping a good (93) quality factor: this gives an average of about 60x60 pixel images. (On such small images, it's normal not to see an incredible compression factor between jpeg and bmp, as the file header in a jpeg file is far larger than in a bmp file: about 500 bytes). This is better than bitmap, but remains quite small.

Finally, even if you succeed in encoding your image in this QR Code, you will encounter an other problem: a QR Code this big is very, very hard to scan successfully. As a matter of fact, this QR Code will have a size of 177x177 modules (a "module" being a small white or black square). Assuming you scan it using a smartphone providing so-called "HD" frames (1280x720 pixels), each module will have a maximum size on the frame of about 4 pixels. If you take into account the camera noise, the aliasing and the blur due to the fact that the user is never perfectly idle when scanning, the quality of the input frames will make it very hard for any QR Code decoding algorithm to successfully get the QR Code (don't forget we set its error correction level on low at the beginning of this!).

Even though it's not very good news, I hope this helps you!

like image 148
mbrenon Avatar answered Sep 25 '22 00:09

mbrenon