GithubHelp home page GithubHelp logo

yingye / weapp-qrcode Goto Github PK

View Code? Open in Web Editor NEW
1.6K 22.0 980.0 1.28 MB

weapp.qrcode.js 在 微信小程序 中,快速生成二维码

License: MIT License

JavaScript 100.00%
weapp wechat qrcode wechat-mini-program wepy

weapp-qrcode's Introduction

weapp-qrcode

npm version change-log

weapp.qrcode.js 在 微信小程序 中,快速生成二维码

Usage

先在 wxml 文件中,创建绘制的 canvas,并定义好 width, height, canvasId

<canvas style="width: 200px; height: 200px;" canvas-id="myQrcode"></canvas>

直接引入 js 文件,使用 drawQrcode() 绘制二维码。!!!在 调用 drawQrcode() 方法之前,一定要确保可以获取到 canvas context

在 v0.6.0 版本构建出多个文件,详情移步Build Files说明

// 将 dist 目录下,weapp.qrcode.esm.js 复制到项目目录中
import drawQrcode from '../../utils/weapp.qrcode.esm.js'

drawQrcode({
  width: 200,
  height: 200,
  canvasId: 'myQrcode',
  // ctx: wx.createCanvasContext('myQrcode'),
  text: 'https://github.com/yingye',
  // v1.0.0+版本支持在二维码上绘制图片
  image: {
    imageResource: '../../images/icon.png',
    dx: 70,
    dy: 70,
    dWidth: 60,
    dHeight: 60
  }
})

如果项目使用了 wepy 框架,可直接安装 weapp-qrcode npm包。

npm install weapp-qrcode --save
import drawQrcode from 'weapp-qrcode'

drawQrcode({
  width: 200,
  height: 200,
  canvasId: 'myQrcode',
  text: 'https://github.com/yingye'
})

DEMO

更多 demo 可以参考 examples目录,示例包含原生语法及WePY、mpvue、Taro框架。

API

drawQrcode([options])

options

Type: Object

参数 说明 示例
width 必须,二维码宽度,与canvaswidth保持一致 200
height 必须,二维码高度,与canvasheight保持一致 200
canvasId 非必须,绘制的canvasId 'myQrcode'
ctx 非必须,绘图上下文,可通过 wx.createCanvasContext('canvasId') 获取,v1.0.0+版本支持 'wx.createCanvasContext('canvasId')'
text 必须,二维码内容 'https://github.com/yingye'
typeNumber 非必须,二维码的计算模式,默认值-1 8
correctLevel 非必须,二维码纠错级别,默认值为高级,取值:{ L: 1, M: 0, Q: 3, H: 2 } 1
background 非必须,二维码背景颜色,默认值白色 '#ffffff'
foreground 非必须,二维码前景色,默认值黑色 '#000000'
_this 非必须,若在组件中使用,需要传入,v0.7.0+版本支持 this
callback 非必须,绘制完成后的回调函数,v0.8.0+版本支持。安卓手机兼容性问题,可通过自行设置计时器来解决,更多可以参考 issue #18 function (e) { console.log('e', e) }
x 非必须,二维码绘制的 x 轴起始位置,默认值0,v1.0.0+版本支持 100
y 非必须,二维码绘制的 y 轴起始位置,默认值0,v1.0.0+版本支持 100
image 非必须,在 canvas 上绘制图片,层级高于二维码,v1.0.0+版本支持,更多可参考drawImage { imageResource: '', dx: 0, dy: 0, dWidth: 100, dHeight: 100 }

位置信息可以参见下图:

TIPS

weapp.qrcode.js 二维码生成部分借鉴了 jquery-qrcode 源码,可以参考 jquery-qrcode

weapp-qrcode's People

Contributors

anndaming avatar barretem avatar marvin1023 avatar yingye avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

weapp-qrcode's Issues

1.0.0版本 真机上显示不出来二维码上的image图片

Description

1.0.0版本 真机上显示不出来二维码上的image图片,微信开发者工具上能显示图片

Environment

  • Platform: [iOS/Andriod]
  • Wechat version: [7.0]
  • weapp-qrcode version: [weapp.qrcode.min.js]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

[在这里描述期望的表现]

观察到的表现:

[在这里描述观察到的表现]

屏幕截图和动态 GIF 图

微信开发者工具上的,能正常显示image图片:
image
下面是手机上的,显示不出图片,iOS和Andriod都不显示:
image

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

真机生成的二维码错乱

Description

真机生成的二维码错乱,导致无法识别。微信开发者工具生成的是好的。

Environment

  • Platform: [开发者工具/iOS/Andriod/Web]
    开发者工具
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
    微信开发者工具 Stable v1.02.1910120
  • Wechat version: [微信版本号]
    Version 7.0.8
  • weapp-qrcode version: [在package.json里]
    v1.0.0
  • other version: [如果在某一设备下出现该问题,请填写设备号]
    华为nova3i
    Android版本:9

Reproduce

问题复现步骤:

  1. 点击该静态图片
  2. 前端生成二维码,然后用小程序api的canvasToTempFilePath来生成临时图片路径,再previewImage该二维码

期望的表现:

希望真机和开发者工具一样生成正常可用的二维码

观察到的表现:

华为手机真机测试,生成的二维码定位符错乱不完整导致无法识别

屏幕截图和动态 GIF 图
真机上生成的二维码

![复现步骤的屏幕截图和动态 GIF 图](图片的 url)

Relevant Code / Logs

<canvas class="canvas-style" :style="{width: cwidth + 'px', height: cheight + 'px'}" canvas-id="qrcode"></canvas>
data () {
    return {
         cwidth: 200,
         cheight: 200,
         tempFilePath: '',
    }
}
                       /**
			 * 展示二维码
			 * @param {Object} idx
			 */
			async showCode(idx) {
				// 绘制二维码
				await this.genQrcode(this.list[idx].url)
				// canvas转为图片
				await canvasToTempFilePath({x: 0, y: 0, width: this.cwidth, height: this.cheight, destWidth: this.cwidth, destHeight: this.cheight, canvasId: 'qrcode'}).then(res => {
					// console.log(res.tempFilePath)
					this.tempFilePath = res.tempFilePath
				}).catch(err => {
					console.log(err)
				})
				// 预览图片,并可转发分享、保存到本地等
				previewImage({current: this.tempFilePath, urls: [this.tempFilePath]}).then(res => {
					
				}).catch(err => {
					console.log(err)
				})
			},
			
			/**
			 * 生成二维码
			 */
			genQrcode(url) {
				// console.log(typeof drawQrcode)
				return new Promise((resolve, reject) => {
					try {
						drawQrcode({
							width: this.cwidth,
							height: this.cheight,
							canvasId: 'qrcode',
							// ctx: uni.createCanvasContext('qrcode'),
							text: url,
							callback: (e) => {
								console.log(e)
								resolve()
							}
						})
					} catch (e) {
						console.log(e)
						//TODO handle the exception
						reject()
					}
				})
			},

是不是自定义组件中无法使用

所有参数都给的静态,其余步骤按文档来做的,只是二维码被放到了自定义组件中,但是发现没有报错,二维码显示不出来。是否不支持自定义组件中使用?
试过在使用组件的页面中onLoad或者onReady中就调用drawQrcode来绘制自定义组件中的二维码,但是还是没用。

使用0.6.0版本在开发环境可以很好的显示qrcode,真机和远程调试的时候显示不出来

你好,rt




代码里面定义了
function setQrcode(that){
drawQrcode({
width: 150,
height: 150,
canvasId: 'canvas',
text: qrcodetext,
correctLevel: 1
})
}
}
在onload里面调用了setQrcode
wxss文件里面定义了样式
.canvas {
margin: 80rpx auto;
width: 150px;
height: 150px;
/* border: 1px solid black; */
}

问题:在微信开发者工具里面显示没有问题,但是到了真机和远程调试的时候显示不出来,真机显示闪了一下二维码,但是页面没有

即将废弃的接口createCanvasContext.setFillStyle,建议更新

Description

在跑小程序体验评分时,发现有即将废弃的接口createCanvasContext.setFillStyle,建议更新替换一下

Environment

  • Platform: 微信开发者工具
  • weapp-qrcode version: v1.0.0

Reproduce

问题复现步骤:

  1. 使用微信开发者工具-小程序开发-Audits进行体验评分
    2.如下图:
    image

Relevant Code / Logs

CanvasContext.setFillStyle(string|CanvasGradient color)

从基础库 1.9.90 开始,本接口停止维护,请使用 CanvasContext.fillStyle 代替

这个小程序码可以分享此小程序的某页面吗,这个路径怎么设置呢?

Description

[问题简单描述]

Environment

  • Platform: [开发者工具/iOS/Andriod/Web]
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
  • Wechat version: [微信版本号]
  • weapp-qrcode version: [在package.json里]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

[在这里描述期望的表现]

观察到的表现:

[在这里描述观察到的表现]

屏幕截图和动态 GIF 图

![复现步骤的屏幕截图和动态 GIF 图](图片的 url)

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

能否将CanvasContext.draw()方法的reserve参数(第一个参数)暴露出来

Description

[问题简单描述]
现有代码里面的reserve参数是写死的false
ctx.draw(false, function (e) {
options.callback && options.callback(e);
});
类似海报图片的绘制:
步骤1.在canvas中绘制大量其它内容
步骤2.然后再留一小块区域绘制二维码
因为 你们把reserve参数写死为false,导致绘制二维码时把步骤1中绘制的内容全部清除了。

[在这里描述期望的表现]
希望能把 reserve 参数暴露出来可由外部传递,默认值为false即可。

支持透明度设置

Description

如题, UI 设计有时为追求好看,会搞个类似渐变色,设置颜色透明度;
目前该库暂不支持

在iOS上回调失效

Description

今天发现iOS设备上,生成二维码之后callback没有回调了。之前是可以的。

Environment

  • Wechat version: 7.0.5
  • weapp-qrcode version: 1.0

二维码上的图片能否支持自定义样式

image

比如这个二维码上的红色图片本来是方形的,想添加border-radius添加圆角效果,不知可以支持吗?目前暴露的image接口只看到这5个
{imageResource: '../../images/icon.png',
dx: 70,
dy: 70,
dWidth: 60,
dHeight: 60}

怎么将生成的二维码保存为本地图片。

Description

[问题简单描述]

Environment

  • Platform: [开发者工具/iOS/Andriod/Web]
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
  • Wechat version: [微信版本号]
  • weapp-qrcode version: [在package.json里]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:
怎么将生成的二维码保存为本地图片。
期望的表现:

[在这里描述期望的表现]

怎么将生成的二维码保存为本地图片。

[在这里描述观察到的表现]

怎么将生成的二维码保存为本地图片。

![复现步骤的屏幕截图和动态 GIF 图](图片的 url)

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

安卓真机上二维码会被裁掉一部分

Description

安卓真机上,二维码显示不全,右边和下面被裁掉一部分。

Environment

  • Platform: [Andriod]
  • Platform version: [MIUI 10.2]
  • Wechat version: [7.0.3]
  • weapp-qrcode version: [1.0.0]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

在安卓真机上能正常显示

观察到的表现:

二维码右侧和下面被裁掉一部分,调试模式显示 canvas 宽度和高度不是200px

屏幕截图和动态 GIF 图

4571554303064_ pic_hd

4561554303061_ pic_hd

Relevant Code / Logs

drawQrcode({
  width: 200,
  height: 200,
  canvasId: 'qrcode',
  text: url
})
.qrcode {
  width: 200px;
  height: 200px;
}

Taro 下生成h5页面报错

Description

[问题简单描述]
weapp.qrcode.esm.js?2b73:5 Uncaught (in promise) TypeError: Cannot read property 'setFillStyle' of undefined

Environment

Taro v1.3.14 版本

  • Platform: [开发者工具/iOS/Andriod/Web]
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
  • Wechat version: [微信版本号]
  • weapp-qrcode version: [在package.json里]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

[在这里描述期望的表现]

观察到的表现:

[在这里描述观察到的表现]

屏幕截图和动态 GIF 图

![复现步骤的屏幕截图和动态 GIF 图](图片的 url)

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

在已有内容的canvas上画二维码会清空之前的画布

Description

我需要在一个有内容的canvas上某个区域显示一个小的二维码,但是使用如下代码后会直接清空之前的画布

      // 绘制二维码
      drawQrcode({
        width: canvasWidth,
        height: canvasHeight,
        ctx,
        text: this.shareUrl,
        x: rpxToPx(32),
        y: rpxToPx(652)
      })

尝试使用save+restore也不能恢复

Environment

  • Platform: 开发者工具
  • Platform version: 1.02.1910120
  • Wechat version: 无
  • weapp-qrcode version: 1.0.0

Reproduce

问题复现步骤:

  1. [第一步]
    新建一个画布,画一些内容
  2. [第二步]
    使用传ctx的方式画二维码
  3. [第三步]
    再画一些别的东西

期望的表现:
2者都能显示

观察到的表现:
最后画的才能显示

链接长度过长,二维码生成报错,怎么解决?

Description

要生成二维码的链接过长,二维码生成失败,报错

Environment

  • Platform: [开发者工具/iOS/Andriod/Web]
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
  • Wechat version: [微信版本号]
  • weapp-qrcode version: [在package.json里]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

[在这里描述期望的表现]

观察到的表现:

[在这里描述观察到的表现]

屏幕截图和动态 GIF 图

1569230237

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

生成图片显示问题

请问qrcode.exportImage()生成的图片格式为wxfile://tmp_b51339b4d213b4485b1673baf67c3acf511dd807cbce8157.png,怎么显示在image上

建议添加在Taro框架下的使用说明

该库在Taro(v1.0.2)框架下可以使用,只是使用的时候必须要注意以下两点:

  1. drawQrcode方法的调用建议在componentDidMount的生命周期;
  2. 在drawQrcode方法中,_this参数为必填项,应该赋值为this.$scope

具体操作:

  1. 引入Canvas组件
    import { Canvas } from '@tarojs/components';
  2. 在组件中使用Canvas组件
    <Canvas style='width: 200px; height: 200px;' canvasId='myQrcode' />
  3. 在ComponentDidMount组件中加入以下方法
    drawQrcode({ width: 200, height: 200, canvasId: 'myQrcode', _this: this.$scope, text: 'https://github.com/yingye' });

谢大佬!!

callback 回调不准确

Description

我需要通过 wx.canvasToTempFilePath 获取画布的临时路径、但它必须是draw 绘制完成之后才能获取 我无法通过callback来完成这个操作(在安卓机型上它比draw要快)、目前只能通过callback加定时器延时来解决

怎么使用别的单位 不用px如 rpx

Description

[问题简单描述]

Environment

  • Platform: [开发者工具/iOS/Andriod/Web]
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
  • Wechat version: [微信版本号]
  • weapp-qrcode version: [在package.json里]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

[在这里描述期望的表现]

观察到的表现:

[在这里描述观察到的表现]

屏幕截图和动态 GIF 图

![复现步骤的屏幕截图和动态 GIF 图](图片的 url)

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

android 机型生成二维码,定位处错乱

image

图片标识箭头标识的位置,二维码标识位错乱(后面几行都错乱了);
控制台无报错提示;
ios 无此问题;

配置如下:

        drawQrcode({
            width: convert(206),
            height: convert(206),
            canvasId: 'qrcode',
            text: `https://xxxxx.com/a/b?id=123`,
            correctLevel: 3
          })

运行环境:

  • 微信版本 6.6.7
  • 小程序 version ^1.9.90
  • andorid MUI 9.5

考虑支持输出base64 ?

Description

[问题简单描述]

Environment

  • Platform: [开发者工具/iOS/Andriod/Web]
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
  • Wechat version: [微信版本号]
  • weapp-qrcode version: [在package.json里]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

[在这里描述期望的表现]

观察到的表现:

[在这里描述观察到的表现]

屏幕截图和动态 GIF 图

![复现步骤的屏幕截图和动态 GIF 图](图片的 url)

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

rpx为单位的时候出现尺寸不统一的问题

rpx为单位的时候出现尺寸不统一的问题

<canvas class="can" style="" canvas-id="ticket_qrcode"></canvas>

.can{
  width: 320rpx;
  height: 320rpx;
}
    drawQrcode({
      width: 320,
      height: 320,
      canvasId: 'ticket_qrcode',
      text: this.ticket_order,
    });

在drawQrcode中,width和height填写320是错误的,但是填写160就是正常的,填写160在模拟器上的样式是正常的,但是手机上查看就会变成160的大小。

只能生成二维码,没有删除二维码的方法吗?

Description

[问题简单描述]

Environment

  • Platform: [开发者工具/iOS/Andriod/Web]
  • Platform version: [对应工具或者iOS或者Andriod的版本号]
  • Wechat version: [微信版本号]
  • weapp-qrcode version: [在package.json里]
  • other version: [如果在某一设备下出现该问题,请填写设备号]

Reproduce

问题复现步骤:

  1. [第一步]
  2. [第二步]
  3. [其他步骤...]

期望的表现:

[在这里描述期望的表现]

观察到的表现:

[在这里描述观察到的表现]

屏幕截图和动态 GIF 图

![复现步骤的屏幕截图和动态 GIF 图](图片的 url)

Relevant Code / Logs

// TODO(you): code or logs here to reproduce the problem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.