首页 > 编程语言 >uniapp使用canvas绘制两张图片合并为一张图并保存至手机(H5+小程序)

uniapp使用canvas绘制两张图片合并为一张图并保存至手机(H5+小程序)

时间:2022-12-05 16:36:23浏览次数:43  
标签:uniapp canvas 20 10 ctx fillText H5 二维码 topHeight

 

 

需求: 访客二维码图片, 包含: 1二维码   2访客信息

1、二维码使用weapp-qrcode插件生成(canvas)

2、访客信息绘制到背景canvas上

3、最后,将二维码的canvas合并到背景canvas上。

 

 

js 部分:

 

listOwnerVisit(_objData).then((_visits) => {
                    _that.visitInfo = _visits.visits[0];
// 以上为获取数据接口
// 开始绘制二维码 drawQrcode({ width: 200, height: 200, canvasId: 'myQrcode', text: this.visitInfo.qrCode, callback: (e) => { uni.canvasToTempFilePath({ x: 0, y: 0, width: 400, height: 400, destWidth: 400, destHeight: 400, canvasId: 'myQrcode', success: (qrcodeTempRes) => { // 获取到单二维码的tempFilePath // 将二维码的tempFilePath画到背景canvas上 uni.getSystemInfo({ success: (systemRes) => {
  // 获取屏幕宽高 做居中用 this.windowWidth = systemRes.windowWidth; this.windowHeight = systemRes.windowHeight; let ctx = uni.createCanvasContext('myBg'); ctx.setFillStyle('#494949'); let topHeight = 0; // 二维码 ctx.drawImage(qrcodeTempRes.tempFilePath, this.windowWidth/2 - 100, topHeight += 20, 200, 200);// 列表名
  // 以下为二维码下方要求显示的文本内容
ctx.setTextAlign('left'); ctx.setFontSize(14); ctx.setFillStyle('#A0A0A0'); ctx.fillText('访客姓名:', 10, topHeight += 20); ctx.fillText('访客联系方式:', 10, topHeight += 20); ctx.fillText('来访事由:', 10, topHeight += 20); ctx.fillText('车牌号:', 10, topHeight += 20); ctx.fillText('车辆审核状态:', 10, topHeight += 20); ctx.fillText('停车场:', 10, topHeight += 20); ctx.fillText('停车位:', 10, topHeight += 20); ctx.fillText('随行人数:', 10, topHeight += 20); ctx.fillText('预计来访时间:', 10, topHeight += 20); ctx.fillText('预计离开时间:', 10, topHeight += 20); ctx.fillText('备注:', 10, topHeight += 20);

  // 绘制最终完整图
ctx.draw(true, () => { setTimeout(() => { // 获取到合并后的地址 uni.canvasToTempFilePath({ canvasId: 'myBg', fileType: 'jpg', success: (res2) => { this.tempFilePath = res2.tempFilePath //#ifdef H5 this.needLongTapSaveImg = true; //#endif } }) }) }) } }) } }) } }) })

 

 

 

html部分:

 

        <view class="u-qrcode" style="text-align: center;">
            <canvas style="width: 200px; height: 200px; margin: 0 auto;background: #fff;visibility: hidden;float: left;" canvas-id="myQrcode"></canvas>
            <canvas style="width: 100%; height: 600px; margin: 0 auto;background: #fff;" canvas-id="myBg" v-show="!needLongTapSaveImg"></canvas>
            <image :src="tempFilePath" style="width: 100%; height: 600px; margin: 0 auto;" v-show="needLongTapSaveImg"></image>
        </view>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1

标签:uniapp,canvas,20,10,ctx,fillText,H5,二维码,topHeight
From: https://www.cnblogs.com/Skate0rDie/p/16952643.html

相关文章

  • uniapp状态栏、导航栏、胶囊数据
    //获取状态栏高度varstatuBar=uni.getSystemInfoSync().statusBarHeight;//获取胶囊数据varcustom=wx.getMenuButtonBoundingClientRect()//获取胶囊的宽高......
  • 通过Shell脚本自动安装Hive&JDBC测试&提供CDH5网盘地址
    〇、参考地址1、Linux下编写脚本自动安装hivehttps://blog.csdn.net/weixin_44911081/article/details/121227024?ops_request_misc=%257B%2522request%255Fid%2522%253A......
  • uniapp和vue的区别
    https://m.php.cn/article/481418.html uniapp和vue的区别uni-app可以编译到(头条,支付宝,微信,QQ,百度)小程序,安卓版,ios版,h5版。通过打包实现一套代码多端运行;vue在web上是......
  • H5文件保存为TIFF
    上周一个朋友拜托我帮它处理下遥感数据,拿到手一看,发现文件格式.h5,但是以往接触的遥感数据格式基本为.tif,带着好奇上网查了一下,维基百科对它的解释:HDF:层级数据格式(Hiera......
  • H5 Canvas 越线绘制页面
    效果⚠因为使用的是斜率来处理的垂直逻辑tan,当为被除数为0时做了特殊处理,两点自由变换时到达零界点会有卡顿。推导开始复习初中二年级数学知识斜率k的公式:\(k=\df......
  • h5打包app点击手机返回键直接退出app的解决方法
    //android点击手机物理返回键退出appbug解决document.addEventListener("plusready",function(){ plus.key.addEventListener('backbutton',function(){ window.his......
  • Canvas学习笔记(二)绘制矩形
    在Canvas中,矩形分为“描边”矩形和“填充”矩形两种。“描边”矩形在Canvas中,我们使用strokeStyle属性和strokeRect()方法配合使用来绘制一个“描边”矩形。语法:ctx.str......
  • zfile 在线云盘、网盘、OneDrive、云存储、私有云、对象存储、h5ai、上传、下载
    基于Java的在线网盘程序,支持对接S3、OneDrive、SharePoint、又拍云、本地存储、FTP、SFTP等存储源,支持在线浏览图片、播放音视频,文本文件、Office、obj(3d)等文件类型。......
  • uniapp rpx在超过960时样式错位的问题
    到uni-app官网查一下,发现有下面这样一段话:“这里需要对rpx的使用特别强调一下。在移动设备上也有很多屏幕宽度,设计师一般只会按照750px屏幕宽度出图。此时使用rpx的好处在......
  • Canvas学习笔记(一)绘制直线
    Canvas简介HTMLCanvas,说得简单一点,就是使用JavaScript操作Canvas元素绘制各种图形的技术。使用Canvas元素绘制图形大体需要三步:获取Canvas对象;获取上下文环......