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> <input type="file" name="file" id="file"><br>
<div></div> <div></div>
</body> </body>
<script type="text/javascript" src="test.js"></script> <script type="text/javascript" src="test.1.js"></script>
</html> </html>

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

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

@ -4,8 +4,11 @@ var imgType = require('image-type')
var qrDecode = require('./') var qrDecode = require('./')
// var jpg = require('./src/imageDecode/jpg'); // var jpg = require('./src/imageDecode/jpg');
/**
function decode(buffer) { * 通过Buffer识别二维码
* @param {buufer} buffer 文件的Buffer
*/
function decodeByBuffer(buffer,debug) {
var type; var type;
return new Promise(function (res, rej) { return new Promise(function (res, rej) {
type = (imgType(buffer) || {}).ext; type = (imgType(buffer) || {}).ext;
@ -31,7 +34,7 @@ function decode(buffer) {
throw 'not image!' throw 'not image!'
} }
}).then(function (imageData) { }).then(function (imageData) {
if (type == 'gif') { if (type == 'gif' || type == 'png') {
return new Promise(function (res, rej) { return new Promise(function (res, rej) {
var errList = []; var errList = [];
var images = imageData; var images = imageData;
@ -40,7 +43,7 @@ function decode(buffer) {
if (errList.length < images.length) return; if (errList.length < images.length) return;
rej('解码失败!') rej('解码失败!')
} }
// console.log('length',imageData.length) debug && console.log('length',imageData.length)
if (imageData.length <= 0) { if (imageData.length <= 0) {
rej('解码失败!') rej('解码失败!')
} else if (imageData.length > 3) { } else if (imageData.length > 3) {
@ -57,13 +60,13 @@ function decode(buffer) {
images.push(imageData[Math.floor(i*sp)]) images.push(imageData[Math.floor(i*sp)])
}while(i++<l) }while(i++<l)
} }
// console.log(images.length) debug && console.log(images.length)
images.forEach(function (v) { images.forEach(function (v) {
setTimeout(function () { setTimeout(function () {
try { try {
res(qrDecode(v)); res(qrDecode(v));
} catch (e) { } catch (e) {
console.log(e); debug && console.log(e);
onerr(e) onerr(e)
} }
}, 0) }, 0)
@ -73,13 +76,19 @@ function decode(buffer) {
return qrDecode(imageData); return qrDecode(imageData);
}) })
} }
exports.decodeQRFile = function (path) {
/**
* 识别二维码图片文件
* @param {String} path 文件路径
*/
exports.decodeByPath = function (path) {
return new Promise(function (res, rej) { return new Promise(function (res, rej) {
fs.readFile(path, function (err, buffer) { fs.readFile(path, function (err, buffer) {
if (err) { return rej(err) } 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 jpg = require('./jpg');
var jpgDecode = new jpg.JpegDecoder(); var jpgDecode = new jpg.JpegDecoder();
var pngDecode = require('./png').decode; var upng = require('./png');
var bmpDecode = require('./bmp'); var bmpDecode = require('./bmp');
var gifDecode = require('./gif').decode; var gifDecode = require('./gif').decode;
var gif1Decode = require('./gif.1').decode; var gif1Decode = require('./gif.1').decode;
exports.bmp = function (buffer) { exports.bmp = function (buffer) {
// return new Promise(function (res, rej) { // return new Promise(function (res, rej) {
// res(bmpDecode(buffer)); // res(bmpDecode(buffer));
@ -22,20 +24,18 @@ exports.jpg = function (buffer) {
} }
exports.png = function (buffer) { exports.png = function (buffer) {
// return new Promise(function (res, rej) { // console.log('png');
// var png = pngDecode(buffer); var png = upng.decode(buffer);
// res({ var datas = upng.toRGBA8(png);
// data: png.data, var images = [];
// width: png.width, datas.forEach(function(v){
// height: png.height images.push({
// }); width:png.width,
// }) height:png.height,
var png = pngDecode(buffer); data:new Uint8Array(v),
return { })
data: png.data, })
width: png.width, return images;
height: png.height
};
} }
exports.gif = function (data) { 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 MIN_SKIP = 3;
var MAX_MODULES = 57; var MAX_MODULES = 57;
var INTEGER_MATH_SHIFT = 8; 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 imgDecode = require('../server')
var path = require('path');
var dir = __dirname;
// console.log(path);
var P = function (p) {
return path.join(__dirname, p);
}
var test = function (path, mark) {
imgDecode.decodeQRFile('./img/out.bmp').then(console.log) return imgDecode.decodeByPath(P(path))
imgDecode.decodeQRFile('./img/out.jpg').then(console.log,console.error) .then(console.log.bind(console, mark + ': '), console.error.bind(console, mark + ':'))
imgDecode.decodeQRFile('./img/out.png').then(console.log,console.error) }
imgDecode.decodeQRFile('./img/out.gif').then(console.log,console.error) Promise.resolve()
imgDecode.decodeQRFile('./img/dt.gif').then(console.log,console.error) .then(function () { test('./img/16.bmp', 'bmp16 ') })
imgDecode.decodeQRFile('./img/dt1.gif').then(console.log,console.error) .then(function () { return test('./img/24.bmp', 'bmp24 ') })
.then(function () { return test('./img/32.bmp', 'bmp32 ') })
//动态img .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