A QR code is short and sweet (besides quite useful) for a quick response (they can be read quickly by a smartphone). Yep, they're normally scanned by someone with a smartphone ,but thanks to jsqrcode, we can achieve the scanning process without a camera and decode the information from plain (PNG or JPEG) images (in base64 format to be more specific and to increase accuracy) in case you ever need such a feature in any of your crazy company (or own) projects.
In this article, you'll learn how to scan a QR Code from its base64 value easily using jsqrcode.
Requirements
To achieve our goal, we are going to depend on the jsqrcode library. This library is a JavaScript QRCode reader implementation for HTML5 created by Lazar Laszlo. You can download (or clone) its source code in the official repository in Github here. The source code itself is based in 17 different JS files (that needs obviously to be loaded in your html document using the script tag).
Implementation
Basically, to use the library you'll need to use 2 lines, set a callback that receives the decoded data and provide the base64 image as first parameter in the decode function:
The following snippet, shows the most basic usage in a simple html snippet (feel free to download and test it locally). The imageURI variable contains a QRCode image (in this case PNG but it can be JPG) in base64 and it will be given as first parameter in our custom function decodeImageFromBase64
.
Is up to you how to convert an image (whatever the source is i.e your own website from an url or a local file) into its base64 format to be processed by the jsqrcode library.
Note that you get to work this scanner code with the getUserMedia API in order to create a live QR Code scanner with your webcamera, however that's another story and you can see a working example in the official demo website of the library.
Have fun !
Comments
Post a Comment