uniapp将二维码图片、文字绘制到canvas上并下载

项目需求:页面展示带log的电子健康卡二维码并实现下载,下载的二维码图片里有还要有文字信息

页面展示:

在这里插入图片描述

下载的二维码图片:

在这里插入图片描述

1、使用uQRCode插件:https://ext.dcloud.net.cn/plugin?id=1287
在这里插入图片描述
2、将解压文件放到项目里:
在这里插入图片描述
3、生成二维码并下载:

(1)引入uQrcode组件:

在这里插入图片描述(2)生成带log的二维码:

布局:

    <view class="flex flex-center">
        <uQrcode
          ref="qrcode"
          canvasId="qrcode"
          :size="200"
          :value="url"
          fileType="png"
          type="2d"
          :options="options"
          @click="remake"
          @complete="complete($event)"
        ></uQrcode>
    </view>
    
    <view class="flex flex-center mt10">
        <view class="btn" @click="saveQrcode">下载电子健康卡</view>
    </view>
    
    <!-- 下载的二维码+文字图片,下载之前和下载完不可见 -->
    <view class="flex flex-center mt10 pt200" v-if="showImg">
        <canvas
          class="my-canvas"
          :style="{
            width: `300px;`,
            height: `400px;`,
          }"
          canvas-id="myCanvasId"
        ></canvas>
    </view>

ts:

  //二维码log
  private options = {
    foregroundImageSrc: "",
    foregroundImageWidth: 50,
    foregroundImageHeight: 50,
  };
	
  private showImg = false;//是否显示下载的图片
  
  onLoad(option) {
    this.url = option.code;
    this.options.foregroundImageSrc = this.$miniImageUrl("nrhc.png");
  }

  //重新生成二维码
  remake() {
    const ref = this.$refs["qrcode"] as any;
    ref.remake();
  }

  complete(e) {
    if (e.success) {
      console.log("二维码生成成功");
    } else {
      console.log("二维码生成失败");
    }
  }

  //下载二维码
  saveQrcode() {
    this.showImg = true;
    uni.showLoading({
      title: "保存中...",
      mask: true,
    });
    const ref = this.$refs["qrcode"] as any;
    ref.getTempFilePath({
      success: (res: any) => {
        console.log(res);
        setTimeout(() => {
          this.saveImg(res.tempFilePath);
        }, 500);
      },
      fail: (err: any) => {
      	this.showImg = false;
        uni.hideLoading();
        uni.showToast({
          icon: "none",
          title: JSON.stringify(err),
        });
      },
    });
  }

  //二维码添加文字的图片保存
  saveImg(tempFilePath: string) {
    let me = this;
    let ctx = uni.createCanvasContext("myCanvasId", this);
    // 画布填充背景色,默认白色
    ctx.setFillStyle("#fff");
    // 设置画布尺寸
    ctx.fillRect(0, 0, 300, 400);
    // 绘制图片
    ctx.drawImage(tempFilePath, 50, 100, 200, 200);
    // 画笔颜色
    ctx.setFillStyle("#000000");
    // 字体大小
    ctx.setFontSize(20);
    ctx.setTextAlign("center");
    //绘制二维码上方文字
    ctx.fillText("*" + this.personName.substring(1, this.personName.length), 150, 40);
    ctx.fillText("电子健康卡静态码", 150, 70);
    //绘制二维码下方文字
    ctx.setFontSize(16);
    ctx.fillText(getCurrentDateTime(), 150, 340);
    ctx.fillText("*请妥善保管,以免被他人盗用", 150, 370);

    // 把以上内容画到 canvas 中
    ctx.draw(true, (ret: any) => {
      // 保存canvas为图片
      uni.canvasToTempFilePath({
        canvasId: "myCanvasId",
        quality: 1,
        complete: function (res1) {
          console.log(res1);
          //图片保存至相册
          uni.saveImageToPhotosAlbum({
            filePath: res1.tempFilePath,
            success: (res2: any) => {
              uni.hideLoading();
              uni.showToast({
                icon: "success",
                title: "保存成功",
              });
              me.showImg = false;
            },
            fail: (err: any) => {
              uni.hideLoading();
              uni.showToast({
                icon: "success",
                title: "保存失败",
              });
              me.showImg = false;
            },
          });
        },
      });
    });
  }

其中,getTempFilePath是在u-qrcode.vue的源码里加的一个方法:

    //获取TempFilePath - add by sqf  2022-11-02
    getTempFilePath(callback = {}) {
      this.toTempFilePath({
        success: (res) => {
          // #ifndef H5
          callback.success(res);
          // #endif
        },
        fail: (err) => {
          callback.fail(err);
          callback.complete();
        },
      });
    },
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在uniapp中生成二维码并保存到本地,可以按照以下步骤进行操作: 1. 首先,使用weapp.qrcode生成二维码。这个库可以用于生成二维码的数据。 2. 然后,使用uni.canvasToTempFilePath方法将生成的二维码转换为临时图片文件。 3. 使用canvas绘制二维码。 4. 接下来,使用uni.canvasToTempFilePath方法再次生成图片文件。 5. 最后,使用uni.saveImageToPhotosAlbum方法将图片保存到本地相册。 具体代码示例如下: ```javascript // 1. 使用weapp.qrcode生成二维码 const qrcode = require('weapp.qrcode'); const qrData = 'your qrcode data'; const qrcodeImg = qrcode.createQrCodeImg(qrData); // 2. 使用uni.canvasToTempFilePath生成临时图片文件 uni.canvasToTempFilePath({ canvasId: 'canvas', // canvas的id success: function (res) { const tempFilePath = res.tempFilePath; // 3. 使用canvas绘制二维码 const ctx = uni.createCanvasContext('canvas'); ctx.drawImage(qrcodeImg, 0, 0, 200, 200); // 绘制二维码图片 ctx.draw(false, function () { // 4. 使用uni.canvasToTempFilePath生成图片文件 uni.canvasToTempFilePath({ canvasId: 'canvas', success: function (res2) { const qrCodeFilePath = res2.tempFilePath; // 5. 使用uni.saveImageToPhotosAlbum保存到本地 uni.saveImageToPhotosAlbum({ filePath: qrCodeFilePath, success: function () { console.log('保存成功'); }, fail: function (err) { console.log('保存失败', err); } }); }, fail: function (err2) { console.log('生成二维码图片文件失败', err2); } }); }); }, fail: function (err3) { console.log('生成临时图片文件失败', err3); } }); ``` 通过以上步骤,你就可以在uniapp中生成二维码并保存到本地相册了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值