v0.0.3 use jsqr

master
cnwhy 7 years ago
parent 2e142c20e8
commit 14edfc7fbb

102
dist/qr-decode.js vendored

@ -1,5 +1,5 @@
/*! /*!
* qr-decode v0.0.2 * qr-decode v0.0.3
* (c) 2018-present cnwhy <w.why@163.com> * (c) 2018-present cnwhy <w.why@163.com>
* Released under the ISC License. * Released under the ISC License.
*/ */
@ -847,8 +847,87 @@
if (number >= 0) return number >> bits;else return (number >> bits) + (2 << ~bits); if (number >= 0) return number >> bits;else return (number >> bits) + (2 << ~bits);
}; };
var ObjDP = Object.defineProperty;
function addAttr(obj, prop, value) {
ObjDP(obj, prop, {
configurable: true,
value: value
});
}
var Matrix = {
getPoint: function getPoint(x, y) {
return this[this.width * y + x];
},
setPoint: function setPoint(x, y, v) {
return this[this.width * y + x] = v;
},
getRow: function getRow(y) {
var start = this.width * y;
return this.slice(start, start + this.width);
},
getColumn: function getColumn(x) {
var col = [];
var y = 0;
while (y + x < this.length) {
col.push(this[y + x]);
y += this.width;
}
return col;
},
getHeight: function getHeight() {
return Math.ceil(this.length / width);
}
};
var extendArray2Matrix_1 = function (arr, width) {
addAttr(arr, 'width', width);
ObjDP(arr, 'height', {
get: Matrix.getHeight
});
addAttr(arr, 'get', Matrix.getPoint);
addAttr(arr, 'set', Matrix.setPoint);
addAttr(arr, 'getRow', Matrix.getRow);
addAttr(arr, 'getColumn', Matrix.getColumn);
return arr;
};
/**
* imageData对像转灰度矩阵
* @param {object} imageData
*/
var imageData2greyMatrix = function (imageData) {
var data = imageData.data,
width = imageData.width,
height = imageData.height;
var greyscalePixels = [];
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
var p = (y * width + x) * 4;
var r = data[p + 0];
var g = data[p + 1];
var b = data[p + 2];
var a = data[p + 3];
greyscalePixels.push((r * 0.2126 + g * 0.7152 + b * 0.0722) * a / 255);
}
extendArray2Matrix(greyscalePixels);
return greyscalePixels;
}
};
var Matrix2bitMatrix = function (Matrix) {}; //exports.getPointAtMatrix
var utils = { var utils = {
URShift: URShift URShift: URShift,
extendArray2Matrix: extendArray2Matrix_1,
imageData2greyMatrix: imageData2greyMatrix,
Matrix2bitMatrix: Matrix2bitMatrix
}; };
function ErrorCorrectionLevel(ordinal, bits, name) { function ErrorCorrectionLevel(ordinal, bits, name) {
@ -965,7 +1044,7 @@
var URShift$2 = utils.URShift; var URShift$2 = utils.URShift;
function BitMatrix(width, height) { function BitMatrix$1(width, height) {
if (!height) height = width; if (!height) height = width;
if (width < 1 || height < 1) { if (width < 1 || height < 1) {
@ -1052,7 +1131,7 @@
}; };
} }
var BitMatix = BitMatrix; var BitMatix = BitMatrix$1;
function ECB(count, dataCodewords) { function ECB(count, dataCodewords) {
this.count = count; this.count = count;
@ -2891,8 +2970,10 @@
Decoder.decode = function (bits) { Decoder.decode = function (bits) {
var parser = new BitMatrixParser(bits); var parser = new BitMatrixParser(bits);
var version = parser.readVersion(); var version = parser.readVersion(); //版本信息
var ecLevel = parser.readFormatInformation().ErrorCorrectionLevel; // Read codewords
var ecLevel = parser.readFormatInformation().ErrorCorrectionLevel; //格式信息
// Read codewords
var codewords = parser.readCodewords(); // Separate into data blocks var codewords = parser.readCodewords(); // Separate into data blocks
@ -2925,7 +3006,12 @@
var Decoder_1 = Decoder; var Decoder_1 = Decoder;
var debug = false; // 获取指定位置的灰度 var debug = false;
/**
* 返回获取指定位置的灰度的函数
* @param {ImageData} data
* @param {Object} base 一个有width,height信息
*/
var Pixel = function Pixel(data, base) { var Pixel = function Pixel(data, base) {
return function (x, y) { return function (x, y) {
@ -2940,7 +3026,7 @@
var binarize = function binarize(data, base, th) { var binarize = function binarize(data, base, th) {
var ret = new Array(base.width * base.height); var ret = new Array(base.width * base.height);
var getPixel = Pixel(data, base); var getPixel = Pixel(data, base); //
for (var y = 0; y < base.height; y++) { for (var y = 0; y < base.height; y++) {
for (var x = 0; x < base.width; x++) { for (var x = 0; x < base.width; x++) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1,6 +1,6 @@
{ {
"name": "qr-decode", "name": "qr-decode",
"version": "0.0.2", "version": "0.0.3",
"description": "QRCode parser/decode", "description": "QRCode parser/decode",
"main": "src/QRCodeDecode.js", "main": "src/QRCodeDecode.js",
"files": [ "files": [
@ -40,6 +40,7 @@
"dependencies": { "dependencies": {
"bmp-js": "^0.1.0", "bmp-js": "^0.1.0",
"image-type": "^3.0.0", "image-type": "^3.0.0",
"jsqr": "^1.1.1",
"pako": "^1.0.6" "pako": "^1.0.6"
} }
} }

@ -1,7 +1,7 @@
var fs = require('fs') var fs = require('fs')
var imgType = require('image-type') var imgType = require('image-type')
var imgDecode = require('./src/imageDecode') var imgDecode = require('./src/imageDecode')
var qrDecode = require('./src/QRDecode') var qrDecode = require('./src/')
/** /**
* 通过Buffer识别二维码 * 通过Buffer识别二维码

@ -3,7 +3,11 @@ var Decoder = require('./lib/Decoder');
var debug = false; var debug = false;
// 获取指定位置的灰度 /**
* 返回获取指定位置的灰度的函数
* @param {ImageData} data
* @param {Object} base 一个有width,height信息
*/
var Pixel = function (data,base) { var Pixel = function (data,base) {
return function(x,y){ return function(x,y){
if (base.width < x || base.height < y) { if (base.width < x || base.height < y) {
@ -16,7 +20,7 @@ var Pixel = function (data,base) {
var binarize = function(data,base,th){ var binarize = function(data,base,th){
var ret = new Array(base.width * base.height); var ret = new Array(base.width * base.height);
var getPixel = Pixel(data,base); var getPixel = Pixel(data,base); //
for (var y = 0; y < base.height; y++) { for (var y = 0; y < base.height; y++) {
for (var x = 0; x < base.width; x++) { for (var x = 0; x < base.width; x++) {
var gray = getPixel(x, y); var gray = getPixel(x, y);

@ -0,0 +1,4 @@
var jsqr = require('jsqr');
module.exports = function(imageData){
return jsqr(imageData.data,imageData.width,imageData.height).data;
}

@ -32,8 +32,8 @@ Decoder.correctErrors = function (codewordBytes, numDataCodewords) {
Decoder.decode = function (bits) { Decoder.decode = function (bits) {
var parser = new BitMatrixParser(bits); var parser = new BitMatrixParser(bits);
var version = parser.readVersion(); var version = parser.readVersion(); //版本信息
var ecLevel = parser.readFormatInformation().ErrorCorrectionLevel; var ecLevel = parser.readFormatInformation().ErrorCorrectionLevel; //格式信息
// Read codewords // Read codewords
var codewords = parser.readCodewords(); var codewords = parser.readCodewords();

Loading…
Cancel
Save