master
cnwhy 7 years ago
parent 64acd199eb
commit 5a11eff012

@ -0,0 +1,25 @@
var qrDecode = require('./')
var decode =
exports.decode = function (bom) {
var canvas = document.createElement("canvas")
var ctx = canvas.getContext('2d')
canvas.width = bom.width;
canvas.height = bom.height;
ctx.drawImage(bom, 0, 0, canvas.width, canvas.height);
var data = ctx.getImageData(0, 0, canvas.width, canvas.height);
return qrDecode(data)
}
exports.decodeByUrl = function (src, cb) {
var img = new Image();
img.src = src;
img.onload = function () {
try {
cb(null,decode(img));
} catch (e) {
cb(e);
}
}
img.onerror = cb;
}

@ -10,5 +10,5 @@
<input type="file" name="file" id="file"><br>
<div></div>
</body>
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript" src="test.1.js"></script>
</html>

@ -1,5 +1,5 @@
var qrcodeDecode = require('../')
var qrcodeDecode = require('../browser')
console.log(1111)
document.getElementById('file').onchange = function (event) {
var el = event.target;
if (!el.files.length) return;
@ -20,19 +20,16 @@ document.getElementById('file').onchange = function (event) {
ok();
}
}).then((src)=>{
var img = new Image();
img.src = src;
img.onload = function(){
var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img,0,0,canvas.width,canvas.height)
//inputimg.src = src;
//qrcode.decode(src);
var imageData = ctx.getImageData(0,0,canvas.width,canvas.height)
alert(qrcodeDecode(imageData));
}
qrcodeDecode.decodeByUrl(src,function(err,txt){
var msg = document.createElement("div")
if(err){
console.log(err);
msg.innerHTML = "err: <br>" + err;
}else{
msg.innerHTML = txt;
}
document.body.appendChild(msg);
})
});

@ -4,8 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "npm demo",
"demo": "parcel ./demo"
"test": "npm run demo",
"demo": "parcel ./demo/index.html"
},
"repository": {
"type": "git",

@ -4,8 +4,11 @@ var imgType = require('image-type')
var qrDecode = require('./')
// var jpg = require('./src/imageDecode/jpg');
function decode(buffer) {
/**
* 通过Buffer识别二维码
* @param {buufer} buffer 文件的Buffer
*/
function decodeByBuffer(buffer,debug) {
var type;
return new Promise(function (res, rej) {
type = (imgType(buffer) || {}).ext;
@ -31,7 +34,7 @@ function decode(buffer) {
throw 'not image!'
}
}).then(function (imageData) {
if (type == 'gif') {
if (type == 'gif' || type == 'png') {
return new Promise(function (res, rej) {
var errList = [];
var images = imageData;
@ -40,7 +43,7 @@ function decode(buffer) {
if (errList.length < images.length) return;
rej('解码失败!')
}
// console.log('length',imageData.length)
debug && console.log('length',imageData.length)
if (imageData.length <= 0) {
rej('解码失败!')
} else if (imageData.length > 3) {
@ -57,13 +60,13 @@ function decode(buffer) {
images.push(imageData[Math.floor(i*sp)])
}while(i++<l)
}
// console.log(images.length)
debug && console.log(images.length)
images.forEach(function (v) {
setTimeout(function () {
try {
res(qrDecode(v));
} catch (e) {
console.log(e);
debug && console.log(e);
onerr(e)
}
}, 0)
@ -73,13 +76,19 @@ function decode(buffer) {
return qrDecode(imageData);
})
}
exports.decodeQRFile = function (path) {
/**
* 识别二维码图片文件
* @param {String} path 文件路径
*/
exports.decodeByPath = function (path) {
return new Promise(function (res, rej) {
fs.readFile(path, function (err, buffer) {
if (err) { return rej(err) }
res(decode(buffer));
res(decodeByBuffer(buffer));
})
})
}
exports.decode = decode;
exports.decodeByBuffer = decodeByBuffer;
exports.decodeByImageData = qrDecode;

@ -1,10 +1,12 @@
var jpg = require('./jpg');
var jpgDecode = new jpg.JpegDecoder();
var pngDecode = require('./png').decode;
var upng = require('./png');
var bmpDecode = require('./bmp');
var gifDecode = require('./gif').decode;
var gif1Decode = require('./gif.1').decode;
exports.bmp = function (buffer) {
// return new Promise(function (res, rej) {
// res(bmpDecode(buffer));
@ -22,20 +24,18 @@ exports.jpg = function (buffer) {
}
exports.png = function (buffer) {
// return new Promise(function (res, rej) {
// var png = pngDecode(buffer);
// res({
// data: png.data,
// width: png.width,
// height: png.height
// });
// })
var png = pngDecode(buffer);
return {
data: png.data,
width: png.width,
height: png.height
};
// console.log('png');
var png = upng.decode(buffer);
var datas = upng.toRGBA8(png);
var images = [];
datas.forEach(function(v){
images.push({
width:png.width,
height:png.height,
data:new Uint8Array(v),
})
})
return images;
}
exports.gif = function (data) {

@ -5,24 +5,6 @@
*/
/*
*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var MIN_SKIP = 3;
var MAX_MODULES = 57;
var INTEGER_MATH_SHIFT = 8;

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 282 KiB

After

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 375 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 100 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

@ -1,12 +1,23 @@
var imgDecode = require('../server')
var path = require('path');
var dir = __dirname;
// console.log(path);
var P = function (p) {
return path.join(__dirname, p);
}
imgDecode.decodeQRFile('./img/out.bmp').then(console.log)
imgDecode.decodeQRFile('./img/out.jpg').then(console.log,console.error)
imgDecode.decodeQRFile('./img/out.png').then(console.log,console.error)
imgDecode.decodeQRFile('./img/out.gif').then(console.log,console.error)
imgDecode.decodeQRFile('./img/dt.gif').then(console.log,console.error)
imgDecode.decodeQRFile('./img/dt1.gif').then(console.log,console.error)
//动态img
var test = function (path, mark) {
return imgDecode.decodeByPath(P(path))
.then(console.log.bind(console, mark + ': '), console.error.bind(console, mark + ':'))
}
Promise.resolve()
.then(function () { test('./img/16.bmp', 'bmp16 ') })
.then(function () { return test('./img/24.bmp', 'bmp24 ') })
.then(function () { return test('./img/32.bmp', 'bmp32 ') })
.then(function () { return test('/img/lx.jpg', 'jpg_lx') }) //jpg 连续
.then(function () { return test('/img/yh.jpg', 'jpg_yh') }) //jpg 优化
.then(function () { return test('./img/8.png', 'png8 ') })
.then(function () { return test('./img/24.png', 'png24 ') })
.then(function () { return test('./img/jt.gif', 'gif_jt') }) //单帧
.then(function () { return test('./img/dt.gif', 'gif_dt') }) //多帧

Loading…
Cancel
Save