镜像自GitHub仓库
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
cnwhy 518ed4af94 add build 7 years ago
demo add build 7 years ago
dist add build 7 years ago
src add build 7 years ago
test add build 7 years ago
.gitignore add build 7 years ago
README.md add build 7 years ago
browser.js add build 7 years ago
package.json add build 7 years ago
server.js add build 7 years ago

README.md

qrcode-decode

时要的二维码识别代码整理自 jsqrcode;

修改点:

  1. 模块化
  2. 剥离识别函数,改成适合服务端使用,但入参数还是保留为实现ImageData接口的对像

关于 ImageData

Canvas 中可以用 ctx.getImageData 方法得到;

如果你不想亲自把图片转为 ImageData, 根据你你的项目, 请使用这两个JS:

  • browser.js 浏览器项目 有两个API decodeByUrl, decodeByDom
  • server.js 服务端项目 提供两个API decodeByPath, decodeByBuffer

服务端当前支持 bmp , jpg , png , gif 格式;

demo

web

web端最终是利用Canvas获取ImageData, 注意兼容及跨域问题

var qrcodeDecode = require('qrcode-decode/browser');
// 传入二维码图片URL/dataURL
qrcodeDecode.decodeByUrl(src, function (err, txt) {
	if (err) { return console.log(err);}	
	alert(txt);
})

// 传入DOM可以画到canvas的dom都可以 `img` `canvas` 'video' 等
var img = document.getElementById('img1');
qrcodeDecode.decodeByDom(img, function (err, txt) {
	if (err) { return console.log(err);}	
	alert(txt);
})

nodejs

注意: 服务器端API是以 promise 返回结果,你注意你的node版本;

//解析文件
var qrcodeDecode = require('qrcode-decode/server');
qrcodeDecode.decodeByPath('xx/code.jpg').then(function(val){
	console.log(val);
},console.error.bind(console))

//解析Buffer
fs.readFile(path, function (err, buffer) {
	if (err) { return rej(err) }
	resqrcodeDecode.decodeByBuffer(buffer);
})