Skip to content

Commit

Permalink
Merge branch 'v1'
Browse files Browse the repository at this point in the history
  • Loading branch information
edi9999 committed Apr 28, 2017
2 parents 23fd9d5 + c84476b commit 1f8b7bd
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 56 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
sudo: false
sudo: required

language: node_js

node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "6"
- "7"

before_install:
- sudo apt-get -qq update
- sudo add-apt-repository --yes ppa:dhor/myway
- sudo apt-get -qq update
- sudo apt-get install graphicsmagick
- nvm install 6
- nvm use 6

Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Changelog
=========

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

1.0.0 [unreleased]
------------------

### Changed

- Update api to return result and bounding box : https://github.com/edi9999/jsqrcode/issues/35
- Update api to be error-first ([#30](https://github.com/edi9999/jsqrcode/pull/30)\)

0.x
---

- Initial release
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
JavaScript QRCode reader for HTML5 enabled browser.
===================================================

This is a port of Lazarsoft’s qrcode reader
This was started as a port of Lazarsoft’s qrcode reader.

It is a maintained fork, so feel free to open issues or PR !

[![Build Status](https://travis-ci.org/edi9999/jsqrcode.svg?branch=master&style=flat)](https://travis-ci.org/edi9999/jsqrcode)

Expand All @@ -28,7 +30,13 @@ var qr = new QrCode();
Set its callback to a custom function:

```
qr.callback = function(result,err) { if(result) console.log(result) }
qr.callback = function(error, result) {
if(error) {
console.log(error)
return;
}
console.log(result)
}
```

Decode an image by its URL or Data URI:
Expand All @@ -46,13 +54,6 @@ var data = context.getImageData(0, 0, width, height);
qr.decode(data);
```

Decode from canvas with "qr-canvas" ID:

```
qr.decode()
```

Building it yourself
====================

If you want, you can build the script yourself.
Expand Down Expand Up @@ -87,3 +88,8 @@ You will have access to the global variable `QrCode` if you do the following in
```

See [examples/browser-upload/index.html](examples/browser-upload/index.html) for a very basic example using a file upload.

Changelog
=========

See [`CHANGELOG.md`](CHANGELOG.md)\.
8 changes: 4 additions & 4 deletions examples/browser-upload/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ <h2>Result</h2>
<script type="text/javascript">
(function() {
'use strict';

var upload = document.getElementById('upload');
var preview = document.getElementById('preview');
var qr = new QrCode();

qr.callback = function(result,err) {
qr.callback = function(err, result) {
var span = document.querySelector('span') || document.createElement('span');
if(result){
span.textContent = result;
Expand All @@ -46,7 +46,7 @@ <h2>Result</h2>
for (var i = 0; i < this.files.length; i++) {
var file = this.files[i];
var imageType = /^image\//;

if (!imageType.test(file.type)) {
throw new Error('File type not valid');
}
Expand All @@ -68,4 +68,4 @@ <h2>Result</h2>
})();
</script>
</body>
</html>
</html>
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
},
"homepage": "https://github.com/edi9999/jsqrcode",
"devDependencies": {
"chai": "^1.9.1",
"chai": "^3.5.0",
"eslint": "^2.13.1",
"mocha": "^2.4.5",
"png-js": "^0.1.1",
"image-parser": "^1.2.4",
"mocha": "^3.2.0",
"rollup": "^0.34.13",
"uglify-js": "^2.7.3"
}
Expand Down
20 changes: 4 additions & 16 deletions src/qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,15 @@ QrCode.prototype.decode = function(src, data) {
}

if (this.callback != null) {
this.callback(this.result, this.error);
this.callback(this.error, this.result);
}

return this.result;

}).bind(this);

if (src == undefined) {
/* decode from canvas #qr-canvas */

var canvas_qr = document.getElementById("qr-canvas");
var context = canvas_qr.getContext('2d');

this.width = canvas_qr.width;
this.height = canvas_qr.height;
this.imagedata = context.getImageData(0, 0, this.width, this.height);

decode();
} else if (src.width != undefined) {
if (src != undefined && src.width != undefined) {
/* decode from canvas canvas.context.getImageData */

this.width = src.width;
this.height = src.height;
this.imagedata = {"data": data || src.data};
Expand Down Expand Up @@ -102,7 +90,7 @@ QrCode.prototype.decode = function(src, data) {
this.imagedata = context.getImageData(0, 0, image.width, image.height);
} catch (e) {
this.result = "Cross domain image reading not supported in your browser! Save it to your computer then drag and drop the file!";
if (this.callback != null) return this.callback(this.result);
if (this.callback != null) return this.callback(null, this.result);
}

decode();
Expand Down Expand Up @@ -153,7 +141,7 @@ QrCode.prototype.process = function(imageData) {
console.log('QR Code processing time (ms): ' + time);
}

return this.decode_utf8(str);
return {result: this.decode_utf8(str), points: qRCodeMatrix.points};
};

QrCode.prototype.getPixel = function(imageData, x, y) {
Expand Down
94 changes: 71 additions & 23 deletions test/qrcode.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,90 @@
var expect = require('chai').expect;
var fs = require('fs');
var PNG = require('png-js');
var QrCode = require('../dist/index.js');
var ImageParser = require("image-parser");

it('should work with basic image', function(done) {
function copy(input) {
return JSON.parse(JSON.stringify(input));
}

it('should work with basic image', function(done) {
var c = fs.readFileSync(__dirname + '/image.png');
var p = new PNG(c);

p.decode(function(data) {

var img = new ImageParser(c);
img.parse(function(err) {
if (err) {
return done(err);
}
var qr = new QrCode();
qr.callback = function(result) {

expect(result).to.equal('Test');
qr.callback = function(error, result) {
if (error) {
return done(error);
}
expect(copy(result)).to.deep.equal({
"result": 'Test',
"points": [
{
"count": 2,
"estimatedModuleSize": 8,
"x": 36,
"y": 148,
},
{
"count": 2,
"estimatedModuleSize": 8,
"x": 36,
"y": 36,
},
{
"count": 2,
"estimatedModuleSize": 8,
"x": 148,
"y": 36,
}
]
});
done();
};
qr.decode(p, data);
qr.decode({width: img.width(), height: img.height()}, img._imgBuffer);
});
});

it('should work with imageData format', function(done) {

var c = fs.readFileSync(__dirname + '/image.png');
var p = new PNG(c);

p.decode(function(data) {

var img = new ImageParser(c);
img.parse(function(err) {
if (err) {
return done(err);
}
var qr = new QrCode();
qr.callback = function(result) {

expect(result).to.equal('Test');
qr.callback = function(error, result) {
if (error) {
return done(error);
}
expect(copy(result)).to.deep.equal({
"result": 'Test',
"points": [
{
"count": 2,
"estimatedModuleSize": 8,
"x": 36,
"y": 148,
},
{
"count": 2,
"estimatedModuleSize": 8,
"x": 36,
"y": 36,
},
{
"count": 2,
"estimatedModuleSize": 8,
"x": 148,
"y": 36,
}
]
});
done();
};
qr.decode({
height: p.height,
width: p.width,
data: data
});
qr.decode({height: img.height(), width: img.width(), data: img._imgBuffer});
});
});

0 comments on commit 1f8b7bd

Please sign in to comment.