61

I'm currently looking for a js library that can encode text in qr codes. The only one I've been able to find so far seems broken, although other people claim to be using it. The sample page doesn't work. By playing a bit with it I managed to generate codes but they don't get decoded by the phone software.

Is there another library is js for this? Has anyone managed to get it to work?

I'm not interested in a solution that pulls the code from an online service (kaywa, google etc.).


Update:

Well, you guys are right, that library does work. My problem was that I tried including it in a HTML5 Boilerplate page, and document.write doesn't seem to work in that. I modified the sample code anyway to make the browser draw in a canvas not a table and I got the order of the fillRect function backwards. Below is the corrected function call.

context.fillRect(c * UNIT_SIZE, r * UNIT_SIZE, UNIT_SIZE, UNIT_SIZE);
// it's column-row, not row-column; don't ask why :)

Since I don't get my image transposed anymore :), now the qr decodes fine. Thanks for the support.

Alex Ciminian
  • 11,398
  • 15
  • 60
  • 94
  • 2
    If you plan on supporting mobile phones, you will have much less headaches going with a server side solution. Mobile javascript isn't as well supported as in browsers. – Byron Whitlock Dec 28 '10 at 21:29
  • I'm not targeting mobiles with this, I just want a generator I can deploy anywhere :), it'll be used by desktop people. Since javascript does the trick, I saw no point in creating it as a web service. – Alex Ciminian Dec 29 '10 at 02:28
  • There seem to be quite a lot of plugins and snippets if I search for them: http://www.google.com/search?q=QR+codes+javascript. – pimvdb Aug 25 '11 at 20:33
  • 7
    Note that Comment (1) is now out of date, and javascript support in modern mobile devices, is very good. There are a few exceptions, so always check. – ocodo Jan 13 '13 at 22:54
  • 1
    A no-framework, no-dependencies, customizable, animate-able, SVG-based `` HTML element: https://github.com/bitjson/qr-code – this lets you generate QR codes without writing any JS. Just include the `script` for the web component and add `` anywhere on the page. – Jason Dreyzehner Mar 01 '23 at 17:10

17 Answers17

26

I wrote a simple GPLv3 qr encoder in javascript that is local, uses HTML5 and is really fast as it is a port of an embedded C version I wrote for Atmel AVR processors.

http://code.google.com/p/jsqrencode/downloads/list

There is a live version (which is saveable as a webapp on iOS devices) at http://zdez.org/qrenc3.html (save to home, opens in safari so you can copy the image or use airprint)

Here is the link to the downloadable source code.

handle
  • 5,859
  • 3
  • 54
  • 82
tz1
  • 296
  • 2
  • 4
  • Nice, I'll give it a go... sorry for the late reply, but I just saw your post. – Alex Ciminian Mar 04 '11 at 21:21
  • 1
    @Muhd `qrcode.js` works fine in IE6~8. It uses table tag if user's browser doesn't support Canvas. Check it out http://davidshimjs.github.com/qrcodejs/ – David Shim Dec 26 '12 at 08:13
  • Nice. @tz1- Can you update it to include user defined image embedded inside the QR like- http://goqr.me/qr-code-logo/ – raghavsood33 Nov 15 '15 at 21:59
  • +1 despite the fact that your code format sucks, this seems to be the best qr js generator regarding the quality of the qr code image – albanx Nov 24 '16 at 12:01
  • by the way it has some bugs, for example you cannot set the width of the qr code, it seems a bit random – albanx Nov 24 '16 at 13:38
23

Script you posted is WORKING, sample.html is not parsed as HTML.

alt text

Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • 1
    Well you can try to fix this http://d-project.googlecode.com/svn/trunk/misc/qrcode/js/qrcode.js but I would go for server side scripting, what if user disables JS or something. – Dejan Marjanović Dec 27 '10 at 23:51
  • It's going to be part of an intranet app, so disabled javascript won't be the case. I thought about fixing the code, but it's not documented at all, I'd like to avoid that if possible. – Alex Ciminian Dec 27 '10 at 23:54
  • FYI this JS works great :) http://debconf11.com/so2.php , sample.html is not parsed as HTML :) – Dejan Marjanović Dec 28 '10 at 00:05
22

jquery-qrcode jQuery plugin also generates QR code using HTML5 canvas element or HTML table, if canvas is not supported.

https://github.com/jeromeetienne/jquery-qrcode

$('#test').qrcode({
    width: 120,
    height: 120, 
    text: "https://github.com/jeromeetienne/jquery-qrcode"});

enter image description here

Working jsFiddle demo:

http://jsfiddle.net/maxim75/YwN8p/4/

Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55
  • 4
    Whenever I google anything JavaScript I always add -jquery behind it, to make sure I get clean and beautiful JavaScript without abomination. :) – Kebman Aug 15 '17 at 12:53
21

There is a simple JavaScript library which I found sometime back called QRCode.js.

QRCode.js is a cross-browser JavaScript library which allows you to generate QRCodes on the fly, on the client-side. It makes use of HTMl5 Canvas and Tables to display the QRCode. The library itself has no dependencies whatsoever.

To generate a QRCode, you just need to include the JavaScript library and then pass as parameters to the QRCode function, the text you want to encode as the QRCode, the width & height of the QRCode you want to display, as well as your specified foreground color and background color.

Cameron
  • 1,142
  • 8
  • 32
Hirvesh
  • 7,636
  • 15
  • 59
  • 72
  • 1
    The library's nice, but if you want me to change the downvote into an upvote please link directly to the source and not the ad-ridden aggregator page. :) – Alex Ciminian Feb 09 '13 at 00:32
  • 1
    My bad - this was how I bookmarked the resource! Updated the link :) – Hirvesh Feb 10 '13 at 05:58
  • how is it with varying sizes of input? Can you set the module size (size of each "dot") or only the overall size? It's better if you can set the module size as then it's more flexible to adjust for varying size of input – Don Cheadle Mar 11 '15 at 16:57
8

I've found a working javascript-jquery based qrcode generator that you might be interested. Its opensource and it's really working. Here is the link: https://github.com/jeromeetienne/jquery-qrcode

The good thing about this one is that its light weight and it does not call any remote service or website.

  • 1
    Internally this uses the same library as the original post - (https://github.com/jeromeetienne/jquery-qrcode/blob/master/src/qrcode.js) – ijk Feb 13 '13 at 22:55
  • The version as of this comment (2016) contains many nice features: canvas/image/div mode, adjustable: color, corner radius, plus customizable image and text overlay. – Steve Seeger Feb 22 '16 at 20:52
6

Google has a QR code generator as part of their Chart Tools that you can use here.

http://code.google.com/apis/chart/infographics/docs/qr_codes.html

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
4

I am using the ShieldUI Lite QR Code, which can be found here:

https://github.com/shieldui/shieldui-lite

Contains all versions of the QR Code, all Error levels.

Yavor Angelov
  • 521
  • 2
  • 5
4

The QR code in the initial post only supports up to 271 bytes (version 10, Size = 57x57, EC level L).

To make it support the full specs of QR code (version 40, 177x177, 2953 bytes), you have to add more values into its RS_BLOCK_TABLE.

See the remaining matrices (version 11-40) here: http://two.pairlist.net/pipermail/reportlab-users/2010-September/009707.html

Enzo Tran
  • 5,750
  • 6
  • 31
  • 36
4

Maksym's solution is better than Google's library because it runs at local does not need to call Google API. also if you don't use jQuery, you can use this one at https://github.com/amanuel/JS-HTML5-QRCode-Generator

YCode
  • 86
  • 3
2

Another HTML5 solution is http://davidshimjs.github.io/qrcodejs/ with IE6~10, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, Windows Mobile browser compatibility

raghavsood33
  • 749
  • 7
  • 17
2

As Canvas/PNG

There is also the new kjua from the same author as the "old" jQuery.qrcode.

As Canvas/PNG/SVG

And QR-Code-generator by @nayuki also has the ability to create QR codes as SVGs.

rugk
  • 4,755
  • 2
  • 28
  • 55
2

You can use Telerik Kendo-UI to generate qrcode and barcode. It is easy to use.

Try using this example code:

QR Code: https://demos.telerik.com/kendo-ui/qrcode/index

Barcode: https://demos.telerik.com/kendo-ui/barcode/index

Arif
  • 6,094
  • 4
  • 49
  • 81
1

npm has a few. I have no idea which ones are good.

$ npm search qrcode
npm http GET https://registry.npmjs.org/-/all/since?stale=update_after&startkey=1379059929305
npm http 200 https://registry.npmjs.org/-/all/since?stale=update_after&startkey=1379059929305
NAME             DESCRIPTION                                                   A
jsqrcode         a node port of Lazar Laszlo's `jsqrcode` qr code decoder      =
jsqrcode-lite    Simplified version of Lazar Laszlo's `jsqrcode` for node.     =
node-zxing       ZXing Wrapper                                                 =
qr               A small library to generate QR codes with libqrencode.        =
qr-element       qrcode dom element                                            =
qr.js            qrcode encoding in javascript                                 =
qrcode           QRCode / 2d Barcode api with both server side and client side s
qrcode-emitter   Emits QR codes found in an image stream.                      =
qrcode-npm       QRCode Generator for JavaScript                               =
qrcode-terminal  QRCodes, in the terminal                                      =
qrcode.js        QR Code Detection / Decoding / Generation                     =
qread            QRcode detector & decoder                                     =
qruri            NodeJS port of Kang Seonghoon's qr.js                         =
rescode          Generate Codes (EAN13, QRCODE ..)                             =
zbar             node-zbar is a NodeJS binding to the ZBar QR Code library.   
chovy
  • 72,281
  • 52
  • 227
  • 295
  • 1
    Your answer seem more interesting than they look! It's a shame! Please explain! (Add an introduction like *Have a look at `npm` search engine*). I did'nt know this before: +1 thanks! – F. Hauri - Give Up GitHub Jan 31 '14 at 07:28
0

I couldn't find a javascript qr code generator.

But you could Consider using Google's API:

http://code.google.com/apis/chart/docs/gallery/qr_codes.html http://togosoft.com/web/qrcode/qrcode.js

Update: I just tried http://d-project.googlecode.com/svn/trunk/misc/qrcode/js/qrcode.js and seems fine to me. The only thing I did was increase the size and made it close the TDs properly, since self closing is not correct html. http://k3rmit.org/nopaste/759 I tested 3 different strings and used a App called QRReader on my iPhone to decode.

Update 2: With var qr = new QRCode(10, QRErrorCorrectLevel.L); I was able to fit 271 chars in the QRCode however might not be the most reliable setting.

Thomas
  • 3,004
  • 23
  • 17
  • oops, i got to distracted reading the qr encoded answer and comment :) – Thomas Dec 27 '10 at 23:58
  • @Alex Ciminian just tried qrcode.js works fine for me (see update). What were your problems when decoding via phone? what software did you use to decode? – Thomas Dec 28 '10 at 00:20
  • 1
    google api has been deprecated http://googledevelopers.blogspot.ca/2012/04/changes-to-deprecation-policies-and-api.html – Robert Levy Jul 24 '13 at 15:04
-1

https://drive.google.com/open?id=0B3TWIbBcUUTwcE1vV24wQnRxNGs

above is the link of js file include it in your project and write the below code in ctp file in cake php or any where you want to use. i tested it in cake php...

<-- including the file....
<?php echo $this->Html->script('jquery.qrcode.min.js')?>
<div style='padding:100px''><img id="demo" style='width: 100px,height: 100px'></div>
<script type="text/javascript">jQuery("#demo").qrcode({text: "HEllo World!"});</script>
brasofilo
  • 25,496
  • 15
  • 91
  • 179
-1

If you can't find a native JavaScript implementation, you can always AJAX the image yourself from your server.

http://www.swetake.com/qr/qr_cgi_e.html

Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
-1

I know this is an old question, but I think it is not necessary a library to generate a QR Code from a text. You just need to use the QR code API.

The implementation is very simple, we have a form with a text field that captures the content data. Whenever we press the generate button we generate a new request URL to the API this URL has two main components data and size. The former requires the text content encoded and the latter will define the size of the image. Here is the code:

const baseURL = 'https://api.qrserver.com/v1/create-qr-code/?data='
const config = '&size=120x120'
const btnElement = document.getElementById('generate')
const dataElement = document.getElementById('data')
const qrCode = document.querySelector('.qr-code')

function htmlDecode(text) {
  var div = document.createElement('div')
  div.innerHTML = text

  return ('textContent' in div) ? div.textContent : div.innerText
}

function htmlEncode(str) {
  var div = document.createElement('div')
  div[('textContent' in div) ? 'textContent' : 'innerText'] = str

  return div.innerHTML
}

window.onload = () => {
  btnElement.onclick = () => {
    qrCode.src = baseURL + encodeURIComponent(htmlEncode(dataElement.value)) + config
  }
}
.qr-code {
  max-width: 160px;
  margin: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<div class="container-fluid">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
  <div class="text-center">
    <img src="https://api.qrserver.com/v1/create-qr-code/?data=http%3A%2F%2Fwww.google.com&size=120x120" class="qr-code img-thumbnail img-responsive">
  </div>

  <div class="form-horizontal">
    <div class="form-group">
      <label class="control-label col-sm-4" for="data">Enter content:</label>
      <div class="col-sm-10">
        <input type="text" class="form-control" id="data" placeholder="i.e. www.google.com">
      </div>
    </div>
    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <button type="button" class="btn btn-default" id="generate">Generate</button>
      </div>
    </div>
  </div>
</div>
Teocci
  • 7,189
  • 1
  • 50
  • 48