Skip to content

Commit

Permalink
fixing options to all methods. fixing bug wher eoptions were not rend…
Browse files Browse the repository at this point in the history
…ered. adding url test for ec level L
  • Loading branch information
soldair committed Oct 14, 2012
1 parent 6e5f786 commit 0f44836
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 26 deletions.
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ this libary can encode a string up to lengths:
2953 in error correct level L
2331 in error correct level M
1663 in error correct level Q
1273 in error correct level h
1273 in error correct level H

the default is H.
It can now be changed in an ugly way that wont be supported for more then another few days if you really need to.
Expand All @@ -45,22 +45,33 @@ install

npm install -g qrcode


node-canvas is a native module and requires dev packages of cairo and pixman to compile.
on ubuntu you can install them with apt-get and npm install will work great.

```sh

sudo apt-get install libpixman-1-dev libcairo2-dev
```

it is my higest priority for this module to use an all js png encoder and remove this dep.

api
---
QRCode.draw(text,cb(error,canvas));
QRCode.draw(text, [optional options], cb(error,canvas));
returns node canvas object see https://github.com/LearnBoost/node-canvas for all of the cool node things you can do
look up the canvas api for the other cool things

QRCode.toDataURL(text,cb(error,dataURL));
QRCode.toDataURL(text, [optional options], cb(error,dataURL));
returns mime image/png data url for the 2d barcode

QRCode.save(path,text,cb(error,written));
QRCode.save(path, text, [optional options] , cb(error,written));
saves png to the path specified returns bytes written

QRCode.drawText(text,cb)
QRCode.drawText(text, [optional options],cb)
returns an ascii representation of the qrcode using unicode characters and ansi control codes for background control.

QRCode.drawBitArray(text,cb(error,bits,width));
QRCode.drawBitArray(text, [optional options], cb(error,bits,width));
returns an array with each value being either 0 light or 1 dark and the width of each row.
this is enough info to render a qrcode any way you want =)

Expand All @@ -71,6 +82,7 @@ see tests/server.js

for client side use:
open tests/clientside.html in your browser

or run tests/clientsideserver.js
yes, it really works in the browser. new browsers but yeah it works.
for bad ones perhaps try excanvas?
Expand Down
2 changes: 1 addition & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ complete = function(){
console.log('build complete =)');
};

done();
done();
9 changes: 4 additions & 5 deletions lib/qrcode-draw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* copyright 2010 Ryan Day
* copyright 2010-2012 Ryan Day
* http://github.com/soldair/node-qrcode
*
* Licensed under the MIT license:
Expand Down Expand Up @@ -42,15 +42,14 @@ QRCodeDraw.prototype = {
defaultErrorCorrectLevel:QRCodeLib.QRErrorCorrectLevel.H,
QRErrorCorrectLevel:QRCodeLib.QRErrorCorrectLevel,
draw:function(canvas,text,options,cb){
var cb,
options = {},
level,
error;

var level,
error;

var args = Array.prototype.slice.call(arguments);
cb = args.pop();
canvas = args.shift();
text = args.shift();
options = args.shift()||{};


Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
,"scripts":{
"pretest":"node build.js"
,"prepublish":"node build.js"
,"test":"./test.sh"
,"test":"tap test/url.js"
}
,"bin":{
"qrcode":"./bin/qrcode"
Expand All @@ -21,6 +21,8 @@
"express":"2.5.x"
,"browserify":"1.9.x"
,"uglify-js":"1.2.x"
,"canvasutil":"*"
,"tap":"*"
}
,"repository":{
"type":"git"
Expand Down
46 changes: 39 additions & 7 deletions qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,30 @@ var draw = exports.draw = function(text,options,cb){
};

//returns data uri for drawn qrcode png
exports.toDataURL = exports.toDataURI = function(text,cb){
draw(text,function(error,canvas){
exports.toDataURL = exports.toDataURI = function(text,options,cb){

if(typeof options == 'function') {
cb = options;
options = {};
}

draw(text,options,function(error,canvas){
if(error) {
cb(error,'');
cb(error);
} else {
canvas.toDataURL(cb);
}
});
}

//synchronous PNGStream
exports.toPNGStream = function (text, WSpath, cb) {
exports.toPNGStream = function (text, WSpath, options,cb) {

if(typeof options == 'function'){
cb = options;
options = {};
}

var out = fs.createWriteStream(WSpath);

draw(text,function (error,canvas) {
Expand All @@ -95,7 +107,12 @@ exports.toPNGStream = function (text, WSpath, cb) {
}

//returns bytes written to file
exports.save = function(path,text,cb){
exports.save = function(path,text,options,cb){

if(typeof options == 'function'){
cb = options;
options = {};
}

draw(text,function(error,canvas){

Expand Down Expand Up @@ -128,14 +145,29 @@ exports.save = function(path,text,cb){
//this returns an array of points that have either a 0 or 1 value representing 0 for light and 1 for dark
//these values include points in the white edge of the qrcode because that edge is actually part of the spec
//
exports.drawBitArray = function(text,cb){
exports.drawBitArray = function(text,options,cb){

if(typeof options == 'function'){
cb = options;
options = {};
}

var drawInstance = new QRCodeDraw();
drawInstance.drawBitArray(text,function(error,bits,width){
cb(error,bits,width);
});
}

exports.drawText = function(text,cb){
//
// draw qr in your terminal!
//
exports.drawText = function(text,options,cb){

if(typeof options == 'function'){
cb = options;
options = {};
}

var drawInstance = new QRCodeDraw();
drawInstance.drawBitArray(text,function(error,bits,width){
if (!error) {
Expand Down
5 changes: 5 additions & 0 deletions test/clientside.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@
drawQR('i work client side too?');

</script>

<div>
<h1>L test</h1>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHwAAAB8CAIAAAAkfEPpAAAABmJLR0QA/wD/AP+gvaeTAAACMklEQVR4nO3dwW4CMQwA0W7F///yckO5RIpleyeBeceKFjSK3G0I2+u+7z89659+Ab/I6ACjA4wOMDrA6ACjA4wOMDrA6ACjA4wOMDrA6ACjA4wOMDrA6ACjA16Zb76uq+p1fKy8Zzs+7/j4la9Xyby37EoHGB1gdEBqpo8yMy46czPP9eTrnHGlA4wOMDqgbKaPVmZfx/V4dF5Xvc4oVzrA6ACjA1pmepXZPJ19fTbrd+NKBxgdYHTA1jN99OReeTdXOsDoAKMDWmZ69zXybI5Hn5e6lnelA4wOMDqgbKZ3XC9nzres7M9QXOkAowOMDkjN9N32rKP77xRXOsDoAKMD2s+nP7n33X3epoorHWB0gNEBZdfpVfsk0bnfsQ+z8nr8zNFhjA4wOuDRcy9V8zc6czN/T3T8neFKBxgdYHTAVbW3EJ2tmc8HZa6jdzjD7koHGB1gdAB2lrHjd0D0MdR8d6UDjA4wOqDsOr1D1X1gMj/H/fQvYXSA0QHH3Jex6tp/5fHd53Nc6QCjA4wOOOa+jCvzuuNzpB37M650gNEBRgdsfV/G6PdG56/76T/E6ACjA7a+h9dsXztzhr3j8VGudIDRAUYHbD3Tq/bHV97z7D6LOXKlA4wOMDrgmPsydvxPjKr7gkW50gFGBxgdsPV9GWcye+WZee11+sGMDjA6YOvz6d/KlQ4wOsDoAKMDjA4wOsDoAKMDjA4wOsDoAKMDjA4wOsDoAKMDjA4wOsDogDcjgAwAvM5/aQAAAABJRU5ErkJggg==" title='L'/>
</div>
</body>
</html>
4 changes: 2 additions & 2 deletions test/clientsideserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var express = require('express')
,app = express.createServer()
,fs = require('fs')
,QRCode = require(__dirname+'/../qrcode')
,canvasutil = require(__dirname+'/../../node-canvasutil/app.js')
,canvasutil = require('canvasutil')
,Canvas = require('canvas')
,Image = Canvas.Image;

Expand Down Expand Up @@ -297,4 +297,4 @@ effectHandlers.plain = function(args,cb){
};

app.listen(3031);
console.log('listening on 3031');
console.log('listening on 3031');
24 changes: 20 additions & 4 deletions test/url.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
// simple test
var test = require('tap').test;

// simple tdest
var QRCode = require(__dirname+'/../qrcode.js');

var shouldBe = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAACMCAIAAAAhotZpAAAABmJLR0QA/wD/AP+gvaeTAAAC0UlEQVR4nO2dQW7EIAwAm6r//3J6qLSiB1Qj24TpzhxXCdlo5FiAgeu+7w85m8+n/4D8jZIAKAmAkgAoCYCSACgJgJIAKAmAkgAoCYCSACgJgJIAKAmAkgAoCYCSACgJwFfm5uu6qv7Hi7HmYmx/tRYjcu/smu73WsVIAqAkAEoCkMpJI5lvbiYHzO6N5KEIT73XiJEEQEkAlASgL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ACjA1pmepXZPJ19fTbrd+NKBxgdYHTA1jN99OReeTdXOsDoAKMDWmZ69zXybI5Hn5e6lnelA4wOMDqgbKZ3XC9nzres7M9QXOkAowOMDkjN9N32rKP77xRXOsDoAKMD2s+nP7n33X3epoorHWB0gNEBZdfpVfsk0bnfsQ+z8nr8zNFhjA4wOuDRcy9V8zc6czN/T3T8neFKBxgdYHTAVbW3EJ2tmc8HZa6jdzjD7koHGB1gdAB2lrHjd0D0MdR8d6UDjA4wOqDsOr1D1X1gMj/H/fQvYXSA0QHH3Jex6tp/5fHd53Nc6QCjA4wOOOa+jCvzuuNzpB37M650gNEBRgdsfV/G6PdG56/76T/E6ACjA7a+h9dsXztzhr3j8VGudIDRAUYHbD3Tq/bHV97z7D6LOXKlA4wOMDrgmPsydvxPjKr7gkW50gFGBxgdsPV9GWcye+WZee11+sGMDjA6YOvz6d/KlQ4wOsDoAKMDjA4wOsDoAKMDjA4wOsDoAKMDjA4wOsDoAKMDjA4wOsDogDcjgAwAvM5/aQAAAABJRU5ErkJggg==";

test('qrcode to data uri should be correct.',function(t){
QRCode.toDataURL('i am a pony!',function(err,url){
if(err) console.log(err);
t.ok(!err,'there should be no error '+err);
t.equals(url,shouldBe,"url generated should match expected value");
t.end();
});

});

QRCode.toDataURL('i am a pony!',function(err,url){
if(err) console.log(err);
console.log(url == shouldBe?"PASS: url is what it should be !\n":"FAIL: oh no something broke the qr generation!\n");
test('qrcode generated with changed error correction should be expected value',function(t){
QRCode.toDataURL('i am a pony!',{errorCorrectLevel:'L'},function(err,url){
t.ok(!err,'there should be no error '+err);
t.equals(url,lShouldBe,"url should match expected value for error correction L");
t.end();
});
});

0 comments on commit 0f44836

Please sign in to comment.