生成二维码

利用JavaScript生成二维码并且中间有logo

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>该二维码支持中文和LOGO</title>
 
<script type="text/javascript" src="jquery-1.8.0.js"></script>
<script type="text/javascript" src="utf.js"></script>
<script type="text/javascript" src="jquery.qrcode.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#qrcodeCanvas").qrcode({
            render : "canvas",    //设置渲染方式,有table和canvas,使用canvas方式渲染性能相对来说比较好
            text : "<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">http://www.baidu.com</span>",    //扫描二维码后显示的内容,可以直接填一个网址,扫描二维码后自动跳向该链接
            width : "200",               //二维码的宽度
            height : "200",              //二维码的高度
            background : "#ffffff",       //二维码的后景色
            foreground : "#000000",        //二维码的前景色
            src: 'photo.jpg'             //二维码中间的图片
        });
    });
</script>
 
</head>
<body>
    <center>
      <h2>该二维码支持中文和LOGO</h2>
      <div id="qrcodeCanvas"></div>
    </center>
</body>
</html>

最后我采用的这个啦,很好用哦~
qr-image 在 Egg.js 中的使用

安装

  npm i qr-image --save

使用
前端:

<img src="/qrcode?text=hello Egg.js" alt="二维码"/>

后端:

'use strict';

const qr = require('qr-image');


class CelebrationController extends Controller {

  async qrcode() {
    // const { id } = this.ctx.query;
    const id = 1;
    try {
      // 大小默认5,二维码周围间距默认1
      const img = qr.image(`https://h5celebration.woody.club?id=${id}`, { type: 'png', size: 4, margin: 1 });
      this.ctx.status = 200;
      this.ctx.type = 'image/png';
      this.ctx.body = img;
    } catch (e) {
      this.ctx.status = 414;
      this.ctx.set('Content-Type', 'text/html');
      this.ctx.body = '<h1>414 Request-URI Too Large</h1>';
    }
  }
}
module.exports = CelebrationController;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 早上送儿子去上学了。吃过早饭,我陪女儿去补牙。女儿的牙齿上有个小洞,需要修补。女儿表现很好,积极配合医生,牙...
    王皓怡妈妈阅读 190评论 0 0
  • 上世纪八十年代,在武汉大学(湖北国防工业系统)政工干部专修班学习时,我和陈志武在同一个寝室住过三年。他淡然沉静,勤...
    曹焕甫阅读 683评论 0 0
  • 我一直有个梦想,想要有套属于自己的房子。 因为来公司3年,我搬了3次家。每次搬家,我都要拖着大包、小包、拉着沉重的...
    一米Sunny阅读 175评论 0 0
  • 定義 定義一系列的算法,把它們一個個封裝起來,並且使它們可以相互替換。 要點 將不變的部分和變化的部分隔開是每個設...
    JohnSmith阅读 285评论 0 1