qrcode-pure
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -13,2 +13,3 @@ /** | ||
function Decode(canvas, error) { | ||
this.canvas = canvas | ||
this.text = this._init(this.getImageData(canvas), error); | ||
@@ -27,3 +28,3 @@ } | ||
} | ||
console.log('', imageData) | ||
return text; | ||
@@ -54,9 +55,8 @@ }, | ||
export default function (canvas) { | ||
var text = ''; | ||
var error = function (e) { | ||
console.log('error', e) | ||
} | ||
text = (new Decode(canvas, error)).text; | ||
const decodeObj = new Decode(canvas, error) | ||
return text; | ||
return decodeObj; | ||
}; |
@@ -7,3 +7,2 @@ /** | ||
let QREncodeConf = { | ||
@@ -189,3 +188,5 @@ /** | ||
return result | ||
return { | ||
canvas: result | ||
} | ||
}; |
{ | ||
"name": "qrcode-pure", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "qrcode encode and decode", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# qrcode-pure | ||
### 简介 | ||
qrcode encode and decode in js (without jquery) | ||
> 二维码编码与解码。 | ||
> 核心源码来自项目[qrcode](https://github.com/nuintun/qrcode),但qrcode使用的是jquery封装,`qrcode-pure`在此将两个功能拆分了出来,单纯的作为两个函数调用。 | ||
生成\解码二维码的函数。核心源码来自项目[qrcode](https://github.com/nuintun/qrcode),但qrcode使用的是jquery封装,qrcode-pure在此将两个功能拆分了出来。 | ||
### 安装 | ||
@@ -14,5 +12,28 @@ ``` bash | ||
### 文档 | ||
#### qrdecode 解码 | ||
##### 调用参数 | ||
需要传入一个canvas DOM | ||
##### 返回值 | ||
调用`qrdecode`解码后返回的值为一个对象。 | ||
|参数|备注| | ||
|-|:-:|-| | ||
|text|解码后的值| | ||
|canvas|解析的canvasDOM| | ||
#### qrencode 编码 | ||
##### 调用参数 | ||
可参考项目[qrcode](https://github.com/nuintun/qrcode)内的参数。 | ||
##### 返回值 | ||
调用`qrencode`编码后返回的值为一个对象。 | ||
|参数|备注| | ||
|-|:-:|-| | ||
|canvas|编码后的的canvasDOM| | ||
### 使用 | ||
#### 解码 | ||
在项目中使用:(vue单文件组件为例) | ||
在项目中使用:(解析单张二维码) | ||
``` html | ||
@@ -24,2 +45,3 @@ <!-- template --> | ||
``` | ||
``` javascript | ||
@@ -29,3 +51,3 @@ // 引入解码函数 | ||
// script | ||
import qedecode from 'qrcode-pure/lib/qrdecode' | ||
import qrdecode from 'qrcode-pure/lib/qrdecode' | ||
@@ -58,3 +80,3 @@ // methods | ||
function decode(){ | ||
let result = qedecode(this.$refs['decode-canvas') | ||
let result = qrdecode(this.$refs['decode-canvas') | ||
@@ -66,2 +88,41 @@ // result返回值为解码后的值 | ||
在项目中使用:(解析多张二维码) | ||
``` html | ||
<!-- template --> | ||
<input type="file" multiple @change="handleChange"> | ||
``` | ||
``` javascript | ||
// 引入解码函数 | ||
// script | ||
import qrdecode from 'qrcode-pure/lib/qrdecode' | ||
// methods | ||
handleChange(e) { | ||
if (e.target.files.length === 0) return | ||
Object.values(e.target.files).forEach(v => { | ||
let canvas = document.createElement('canvas'), | ||
ctx = canvas.getContext('2d'), | ||
reader = new FileReader() | ||
reader.onload = function(e) { | ||
var img = new Image() | ||
img.onload = function() { | ||
canvas.width = img.width | ||
canvas.height = img.height | ||
ctx.drawImage(img, 0, 0) | ||
// 调用结果 result 为对象,具体参数参考 文档 => decode 解码 | ||
let result = qrdecode(canvas) | ||
} | ||
img.src = e.target.result | ||
} | ||
v && reader.readAsDataURL(v) | ||
}) | ||
} | ||
``` | ||
#### 编码 | ||
@@ -84,9 +145,12 @@ | ||
// 参数具体内容请参考项目[qrcode](https://github.com/nuintun/qrcode),在此不再赘述。 | ||
// 返回值为二维码canvas的DOM | ||
// 返回值为一个对象 | ||
// 其中canvas属性为编码后二维码的canvas DOM | ||
let result = qrencode({ text: 'https://pkjy.github.io' }) | ||
} | ||
``` | ||
### 在线体验 | ||
[qrcode-pure尝试](https://pkjy.github.io/#/gallery/qrcode-pure) | ||
> 注:本项目处理建设阶段,代码变化比较频繁。 | ||
[qrcode-pure体验](https://pkjy.github.io/#/gallery/qrcode-pure) | ||
81642
2766
151