JavaScript library for generating QR codes
qr.js is a pure JavaScript library for generating QR codes utilising HTML5 technology.
Originally based on jsqrencode but with cleaner safer code and not to mention a new simple and understandable API.
This library requires HTML5 to work as its <canvas> element is used to render the QR code.
All the data used by qr.js are optional and you can choose to provide no information at all if you wish. The API has two simple methods that take a single object argument that can contain the following values;
#fff
) is used by default<canvas>
element in which the
QR code should be rendered
<canvas>
element by default
#000
) is used by default<img>
element in which the QR
code should be rendered
<img>
element by default
L
ECC
level is used by default
4
is used by defaultqr.image([data|value][, callback(error, image)])
Returns either a new <img>
element containing the generated
QR code or the value of data.image
if it was specified.
Add an image of a rendered QR code to a page.
window.addEventListener('onload', function() {
// Get image for rendered QR code
var image = qr.image({
level: 'H'
, size: 4
, value: 'http://neocotic.com/qr.js'
})
// Check image was returned (may not have been if browser doesn't support the
// HTML5 canvas element)
if (image) {
// Add image to page
document.body.appendChild(image)
}
})
qr.canvas([data|value][, callback(error, canvas)])
Returns either a new <canvas>
element containing the generated
QR code or the value of data.canvas
if it was specified.
Render the QR code in an existing canvas on a page.
window.addEventListener('onload', function() {
// Render the QR code in <canvas id="qr"/>
qr.canvas({
canvas: document.getElementById('qr')
, level: 'L'
, size: 4
, value: 'http://neocotic.com/qr.js'
})
})
qr.save([data|value][, path][, callback(error)])
Generates a QR code and saves it to either the location specified, unless the script is running in the browser, in which case the user is prompted to specify a location and file name before it is downloaded.
Prompt the user to download the generated QR code.
window.addEventListener('onload', function() {
// Prompt user to save image as file
qr.save('http://neocotic.com/qr.js')
})
qr.toDataURL([data|value][, callback(error, url)])
Return the data URL for the generated QR code.
Show an alert containing the data URL for the generated QR code.
window.addEventListener('onload', function() {
// Get data URL for QR code
var url = qr.toDataURL('http://neocotic.com/qr.js')
// Check URL was returned (may not have been if browser doesn't support the
// HTML5 canvas element)
if (url) {
// Alert user with the URL
alert(url)
}
})
ECC (Error Correction Capacity) levels represent the amount of data that can
be restored if the QR code
symbol is smudged or damanged. There are four ECC
levels ranging from L
(smallest) to H
(best);
The qr.js API exposes some properties and methods which may only be useful to users in special cases.
qr.noConflict([callback(error)])
Relinquish qr.js' control of the qr
global
variable. If another library uses this variable calling this method will reassign
it back to that library.
qr.VERSION
The current version of qr.js.
QRCode
to qr
callback
functionality to API
methods
data
arguments to be an object or string value
VERSION
property to the API
toDataURL
,
save
and noConflict
methods to the API
If you have any problems with this library or would like to see the changes currently in development browse our issues.
Take a look at the documentation to get a better understanding of what the code is doing.
If that doesn't help, feel free to follow me on Twitter, @neocotic.