1.下面这个图片就是通过图片和文字等内容合成的一张带有微信小程序二维码的图片,在小程序内部长按可以识别出来:
2.基本思路是先将内容用canvas排好版,然后把该canvas转化成图片;图片利用wx.previewImage进行展示,才能识别图片中的微信小程序二维码,这是博主目前知道唯一一种识别二维码的手段。
3.1.合成canvaswxml:
canvascanvas-id"mycanvas"class'canvas'id"mycanvas"wx:if"{{isShowCav}}"style'border:1pxsolid#000000'/4.wxss
.canvas{position:absolute;top:0;left:0;z-index:2000;width:748rpx;height:1336rpx;}5.js
canvas:function(object){let_thisthis;letrealWidth,realHeight;//创建节点选择器varquerywx.createSelectorQuery();//选择idquery.select('#mycanvas').boundingClientRect()query.exec(function(res){//res就是该元素的信息数组realWidthres[0].width;realHeightres[0].height;console.log('realHeight',realHeight);console.log('realWidth',realWidth);constctxwx.createCanvasContext('mycanvas');ctx.drawImage("../../images/ctx-bg.jpg",0,0,realWidth,realHeight);ctx.drawImage(_this.data.canvasUserPic,(realWidth*0.099),(realHeight*0.052),(realWidth*0.091),(realWidth*0.091));ctx.setFontSize(12);ctx.setFillStyle("#a38874");ctx.fillText(object.date,(realWidth*0.201),(realHeight*0.076));ctx.setFontSize(14);ctx.setFillStyle("#a38874");ctx.fillText("农历"+object.lunar,(realWidth*0.201),(realHeight*0.099));ctx.drawImage("../../images/swiper-bg.png",(realWidth*0.099),(realHeight*0.112),(realWidth*0.8),(realHeight*0.60));ctx.drawImage(_this.data.canvasShowImg,(realWidth*0.099),(realHeight*0.112),(realWidth*0.8),(realHeight*0.30));ctx.drawImage("../../images/swiper-detail.png",(realWidth*0.099),(realHeight*0.395),(realWidth*0.8),(realHeight*0.03));ctx.setFontSize(16);ctx.setFillStyle("#8d7665");ctx.setTextAlign('center')ctx.fillText(object.title1,realWidth/2,_this.calculateWH(2,620,realWidth,realHeight));ctx.fillText(object.title2,realWidth/2,_this.calculateWH(2,666,realWidth,realHeight));ctx.drawImage("../../images/swiper-line.png",(realWidth-realWidth*0.71)/2,(realHeight*0.528),(realWidth*0.71),(realHeight*0.0195));ctx.drawImage("../../images/luckpic.png",_this.calculateWH(1,267,realWidth,realHeight),_this.calculateWH(2,763,realWidth,realHeight),_this.calculateWH(1,204,realWidth,realHeight),_this.calculateWH(2,60,realWidth,realHeight));ctx.setFontSize(12);ctx.fillText(object.luck_title,realWidth/2,_this.calculateWH(2,880,realWidth,realHeight));ctx.drawImage("../../images/code.jpg",_this.calculateWH(1,229,realWidth,realHeight),_this.calculateWH(2,989,realWidth,realHeight),_this.calculateWH(1,292,realWidth,realHeight),_this.calculateWH(1,292,realWidth,realHeight))ctx.draw();setTimeout(function(){wx.canvasToTempFilePath({canvasId:'mycanvas',success:function(res){vartempFilePathres.tempFilePath;_this.setData({canvasUrl:tempFilePath})if(tempFilePath!''){_this.setData({isShowCav:false});wx.hideLoading();wx.previewImage({current:_this.data.canvasUrl,//当前显示图片的http链接urls:[_this.data.canvasUrl],//需要预览的图片http链接列表})}},fail:function(res){console.log(res);}});},500);})},6.出现问题:1.服务器上发送过来的图片路径直接插进ctx.drawImage上,手机上显示不了。解决方案:利用wx.downloadFile将图片下载再保存好这个新图片路径,然后放到ctx.drawImage上
//下载图片onShow1:function(object){let_thisthis;_this.setData({isShowCav:true})wx.downloadFile({url:object.avatarurl,success:function(sres){_this.setData({canvasUserPic:sres.tempFilePath});wx.downloadFile({url:object.show_img,success:function(sres1){_this.setData({canvasShowImg:sres1.tempFilePath});_this.canvas(object);}})}})},7.2.canvas出现在手机上的顶层,不管z-index设置多少层都没有用。解决方案:利用wx:if"{{isShowCav}}"将canvas临时隐藏,要用到的时候再显示。不用再隐藏掉。
8.3.canvas里面的文字如何居中,官方文档虽然提供了案例,但是没有说具体是怎么用的。解决方案:
constctxwx.createCanvasContext('myCanvas')ctx.setStrokeStyle('red')ctx.moveTo(150,20)ctx.lineTo(150,170)ctx.stroke()ctx.setFontSize(15)ctx.setTextAlign('left')ctx.fillText('textAlignleft',150,60)ctx.setTextAlign('center')ctx.fillText('textAligncenter',150,80)ctx.setTextAlign('right')ctx.fillText('textAlignright',150,100)ctx.draw()9.这里面的居中不是我们常用的css那种居中;而是忽略了文字宽高的意思,所以你还是要给文字设置一个(x,y)坐标,只要将这个坐标写上canvas宽度的一半,它就可以实现居中了。
10.小程序的坑还是不好踩啊,一肛上半天时间就没了!
微信小程序canva生成图片长按图片识别小程序二维码详解-图片上的小程序码怎么来的-小程序图形验证码
浏览量:2370
时间:
来源:开心大表哥
版权声明
即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。
最新资讯
-

即速应用,赋能企业玩转微信小程序智慧经营
作为国内领军的智慧商业经营服务商,即速应用始终秉承“让每个企业都拥有自己的智慧店铺”的愿景,持续赋能更多企业玩转智慧经营。即速应用旗下拥有“小程序搭建工具-即速应用”、“私域流量专家-即客云”等产品,帮助商家打通互联网全生态营销闭环。 -

即客云2.0重磅更新,让微信小程序运营更简单!
即客云作为一款基于企业微信的第三方工具,现从多维度提供超过30种功能,自上线以来,已服务多家企业,受到一致好评。近期,我们根据客户反馈和市场调研正式推出升级版 即客云2.0!更新了私域运营SOP,群日历功能,批量拓客,客户雷达,消息推送,个人欢迎语,帮助企业更好运用企业微信;同时提升了社群运营工作标准化,提升运营效率,帮助企业实现客户增长,玩转私域流量。 -

零代码 + AI 双轮驱动|即速应用解锁人工智能小程序开发新范式
无需代码、无需 AI 算法功底,普通人也能快速搭建智能小程序。即速应用将人工智能与零代码开发深度融合,推出 AI 智能生成能力,用户通过自然语言描述需求,AI 自动生成小程序页面、功能模块与后台配置,覆盖商城、预约、同城、社区团购等全场景。平台内置 AI 智能推荐、智能客服、用户画像分析等能力,一键对接微信生态,打通视频号、企业微信、短信跳转,帮企业快速落地 AI 应用,抢占智慧经营先机,让每家企业都拥有 AI 驱动的智慧店铺。










