欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > 小程序做自定义分享封面图,Canvas base64图片数据真机上不显示?【已解决】

小程序做自定义分享封面图,Canvas base64图片数据真机上不显示?【已解决】

2024/11/9 2:18:51 来源:https://blog.csdn.net/Leijiang0504/article/details/140223976  浏览:    关键词:小程序做自定义分享封面图,Canvas base64图片数据真机上不显示?【已解决】

首选说一下需求,做一个小程序分享,但是封面图要自定义,除了要有对应商品还有有背景图,商品名。类似这种

实现逻辑,把商品图和背景图,再加上价格和商品名用canvas 渲染出来

这是弄好之后的效果图,里面的背景色和商品图还有商品名称,价格这些可以自定义

具体实现步骤

1,写一个 canvas标签,写好对应的canvas-id="myCanvas" ,设置宽高

2,这个主要是有图片文字组成,图片渲染到canvas的话我们需要先转成bese64,完成之后在工具上就可以到了,但是这个时候真机是看不到的,需要我们把base64数据通过wx.getFileSystemManager方法写入一下,这样真机就可以访问了

3,写好之后我们就可以写入文字调整对应的位置就可以了

 好了下面开始吧,本来想直接沾全部代码的,后面想想还是一步一步来,只要一步一步调试好,基本不会出问题了,好了,下面开始

1,我们要想做这么图的话,因为是动态的,所以这里的图片我们要用canvas画出来

定义一个canvas标签

<canvas canvas-id="myCanvas" style="width: 508.47rpx; height: 406.78rpx;position: fixed;left: -800px;"></canvas>

注意这里fixed;left: -800px; 在调试阶段可以去掉要不然看不到效果图了 

2,定义好标签后我们就需要给他画出来,首先我们画canvas的话需要用本地图,或者用base64

因为我们商品图都是动态的所以这里只能用base64,我们需要在画图之前先把我们需要渲染的图片改成base64数据

urlTobase64(url) {console.log('ruw', url)wx.request({url: url,responseType: 'arraybuffer', //最关键的参数,设置返回的数据格式为arraybuffersuccess: res => {//把arraybuffer转成base64let base64 = wx.arrayBufferToBase64(res.data);//不加上这串字符,在页面无法显示的哦base64 = 'data:image/jpeg;base64,' + base64// this.lineBg = base64this.writecommodityImg(base64)//打印出base64字符串,可复制到网页校验一下是否是你选择的原图片呢// console.log('base64=>', this.lineBg)console.log('kais')const ctx = uni.createCanvasContext('myCanvas', this); // 获取 Canvas 上下文// 绘制背景图像let bg ="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAACgCAYAAABJ/yOpAAAAAXNSR0IArs4c6QAAHAJJREFUeF7tXQecFOX5fuZ65ZAmFjAq0VgSBTUgKBo19C4gRZAuIgIKAtJsKILwVyFSVQQDFpqIIhbsikTFWKJooigE6ccd19vuP8/ODDs7N9+Wu+N2j32/32+5Y+abrzzzPvPW2dNcM9q4IU0QEAQcEdCEICIZgoAaASGISIcg4AcBzTVdTCyREEFAhYAQRGRDEBANIjIgCFQMAc01rW3wUazYOODCVsD5zYEzzwfS6kBLTK7YzHKVIFANCLiLCoCcTGDvD8AP24HvPgbKSoOeWSsLliAXt4b218HQ6pwW9ODSURCINATcmfvgfms58O0HQS0tMEE0DVrb4dBa9QhqQOkkCNQEBNwfr4f7jWWA278BpZVNDWBitRmCmNa9a8KeZY2CQEgIuN5/EaA28eek+yXIha2g9Z0GTdNCmlg6CwI1AQG32w338zN1v0TRtLKp7Zx1TEwstLFLodU9vSbsVdYoCFQIAfeRvXA/cSvgKnO8XiuboiDIRa0Q029ahSaViwSBmoSAa/VM4F/OWkRNkJ4TENP0+pq0T1mrIFAhBFxfvgOsfdRZg5QqNEjMuKXQ6jeq0IRykSBQkxBwH9oD1+MjFAS5x9nEipmxDlpSSk3ap6xVEKgQAu7CPLge6KkiSHtHJz324c0VmkwuEgRqIgJlUzoIQWrijZM1Vw8CaoJMVmiQWaJBqufWyCyRgEDZPSoNIgSJhPsjawgzAkKQMN8AmT6yEVASpGRSB0cnPe6R1yJ7R7I6QaAKESid3NHZSReCVCHKMlSNRUAIUmNvnSy8OhBQE2SiwsSaLSZWddwYmSMyECidpDKxhCCRcYdkFWFFwA9BOjo76bNfDeuCZXJBoDoRKJ3USeGk360gyBwhSHXeIJkrvAiUThSChPcOyOwRjYCSIMUKDRIvGiSib6gsrmoRKFFpkOIJziZW/KNiYlXtLZDRIh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gImg(bg)ctx.drawImage(this.canvasImg.bgimgPath, 0, 0, 264, 215);// 绘制商品图ctx.drawImage(this.canvasImg.commodityimgPath, 20, 22, 90, 100); // 商品图// 添加商品名称ctx.setFontSize(18);ctx.setFillStyle('#000000');// this.productDetail.proName// ctx: 画布的上下文环境// content: 需要绘制的文本内容// drawX: 绘制文本的x坐标// drawY: 绘制文本的y坐标// lineHeight:文本之间的行高// lineMaxWidth:每行文本的最大宽度// lineNum:最多绘制的行数// */this.textPrewrap(ctx, this.productDetailbox.proName, 115, 40, 25, 115, 2)// this.textPrewrap(ctx, '测试风收到回复肯德基福克斯等哈开发手打开发机三打哈登记说法凯撒代发', 115, 40, 25, 115, 2) // ctx.fillText('测试风收到回复肯德基福克斯等哈开发手打开发机三打哈登记说法凯撒代发', 200,50, 140);// 添加商品价格// ctx.setFontSize(22);ctx.setFillStyle('#FF0000');// this.productDetail.pricectx.font = 'normal bold 26px Arial,sans-serif 'ctx.fillText('¥' + this.productDetail.price, 115, 120, 200, 70);// 绘制完成并导出图片ctx.draw(false, () => {uni.canvasToTempFilePath({canvasId: 'myCanvas',success: (res) => {console.log(res.tempFilePath); // res.tempFilePath 就是生成的图片路径this.tempFilePath=res.tempFilePath// this.uploadbgImg(res.tempFilePath)// 这里可以将 res.tempFilePath 返回给调用方或者保存到本地},fail: (res) => {console.error(res);}}, this);});}})},

这里的urlTobase64(url) url是你的远程图片路径,通过arrayBufferToBase64他会返回你一个base64数据

//把arraybuffer转成base64
let base64 = wx.arrayBufferToBase64(res.data);

转入成功后我们在把图片渲染到 canvas之前需要把base64写入一下

跟这个一样,你只需要换一下里面的base64数据就行,需要注意的是一个他好像不能循环利用,一个图片你要单独写一个方法进入写入

 写入方法:

	writebgImg(data){const base64 = data; //base64格式图片const time = new Date().getTime();//USER_DATA_PATH:文件系统中的用户目录路径 (本地路径)const imgPath = wx.env.USER_DATA_PATH + "/poster" + time + "" + ".png";const imageData = base64.replace(/^data:image\/\w+;base64,/, "");const file = wx.getFileSystemManager();file.writeFileSync(imgPath, imageData, "base64");this.canvasImg.bgimgPath=imgPathconsole.log('imgPath',this.canvasImg.bgimgPath);//imgPath就是图片在本地的地址//如需保存至本地// wx.saveImageToPhotosAlbum({//       filePath: imgPath,//        success: (res) => {//            wx.showModal({//                title: '照片已保存至相册',//                content: '快去分享给小伙伴吧',//                confirmText: '我知道了',//                showCancel: false,//           })//       }// })},

方法里面什么都不用改,他会正常给你返回一个真机可读取的路径

	// 商品图writecommodityImg(data){const base64 = data; //base64格式图片const time = new Date().getTime();//USER_DATA_PATH:文件系统中的用户目录路径 (本地路径)const imgPath = wx.env.USER_DATA_PATH + "/poster" + time + "" + ".png";const imageData = base64.replace(/^data:image\/\w+;base64,/, "");const file = wx.getFileSystemManager();file.writeFileSync(imgPath, imageData, "base64");this.canvasImg.commodityimgPath=imgPathconsole.log('imgPath',this.canvasImg.commodityimgPath);},

写入完之后你就可以渲染对应的图片数据了,根据你对应的位置和大小进行修改就可以了

	// 绘制商品图ctx.drawImage(图片渲染路径,x轴位置,y轴位置,宽,高)ctx.drawImage(this.canvasImg.commodityimgPath, 20, 22, 90, 100); // 商品图

文字也一样

ctx.fillText('¥' + this.productDetail.price, 115, 120, 200, 70);

但是如果文字是商品名称你又想设置换行可以根据我下面这个方法来

this.textPrewrap(ctx, this.productDetailbox.proName, 115, 40, 25, 115, 2)

 我这个方法是在urlTobase64里渲染的时候用到的,就是你在渲染文字的地址换成这个方法就行

     // this.productDetail.proName
                        // ctx: 画布的上下文环境
                        // content: 需要绘制的文本内容
                        // drawX: 绘制文本的x坐标
                        // drawY: 绘制文本的y坐标
                        // lineHeight:文本之间的行高
                        // lineMaxWidth:每行文本的最大宽度
                        // lineNum:最多绘制的行数

下面是对应方法

	textPrewrap(ctx, content, drawX, drawY, lineHeight, lineMaxWidth, lineNum) {var drawTxt = ''; // 当前绘制的内容var drawLine = 1; // 第几行开始绘制var drawIndex = 0; // 当前绘制内容的索引// 判断内容是否可以一行绘制完毕if (ctx.measureText(content).width <= lineMaxWidth) {ctx.fillText(content.substring(drawIndex, i), drawX, drawY);} else {for (var i = 0; i < content.length; i++) {drawTxt += content[i];if (ctx.measureText(drawTxt).width >= lineMaxWidth) {if (drawLine >= lineNum) {ctx.fillText(content.substring(drawIndex, i) + '..', drawX, drawY);break;} else {ctx.fillText(content.substring(drawIndex, i + 1), drawX, drawY);drawIndex = i + 1;drawLine += 1;drawY += lineHeight;drawTxt = '';}} else {// 内容绘制完毕,但是剩下的内容宽度不到lineMaxWidthif (i === content.length - 1) {ctx.fillText(content.substring(drawIndex), drawX, drawY);}}}}},

 最后绘制并导出就好了

// 绘制完成并导出图片ctx.draw(false, () => {uni.canvasToTempFilePath({canvasId: 'myCanvas',success: (res) => {console.log(res.tempFilePath); // res.tempFilePath 就是生成的图片路径this.tempFilePath=res.tempFilePath// this.uploadbgImg(res.tempFilePath)// 这里可以将 res.tempFilePath 返回给调用方或者保存到本地},fail: (res) => {console.error(res);}}, this);});

在对应分享页面设置图片就OK了 

onShareAppMessage() {// console.log(this.tempFilePath)return {title:'Hi,这个商品不错,分享给你',path: '/pages/index/index',imageUrl: this.tempFilePath, //背景图success: function() {uni.showToast({title: '分享成功',icon: 'success'});},fail: function() {uni.showToast({title: '分享失败',icon: 'none'});}}},

 有问题随时滴滴。。。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com