- 后端要求我穿一个图片文件流给他,给出了如图的接口,我写的代码在下。我应该怎么写才对?
[图片] 我写的如下 [图片] 不知道为啥报错 [图片] [图片] [图片]
10-21 - wx.chooseMedia调用相机后怎么添加水印?
WXML: <view class="right-section" bindtap="takesfzBack"> <image wx:if="{{ifphotosfzBack}}" class="section-image" style="height: 12vh;" src="/images/sfzBack.png" mode="" /> <image wx:else class="section-image" src="{{photosfzBack}}"></image> <canvas id="myCanvas" canvas-id="myCanvasId"></canvas> <view>上传身份证背面</view> </view> JS: onLoad() { this.setData({ pageLoaded: false }); }, onReady() { const query = wx.createSelectorQuery(); query.select('#myCanvas').fields({ node: true }).exec((res) => { if (res && res.length > 0) { this.myCanvas = res[0].node; this.setData({ pageLoaded: true }); } else { console.error('未找到具有 id="myCanvas" 的元素'); } }); }, takesfzBack() { if (this.data.pageLoaded) { // 调用系统相机或相册选择媒体文件 wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['camera'], success: (res) => { if (res.tempFiles && res.tempFiles.length > 0) { this.setData({ ifphotosfzBack: false, }); const tempFilePath = res.tempFiles[0].tempFilePath; this.addWatermark(tempFilePath); console.log('照片路径:', res.tempFiles[0].tempFilePath); } else { console.error('未获取到照片路径'); } }, fail: (err) => { console.error('选择媒体文件失败:', err); }, }); } else { console.error('页面尚未完全加载,无法执行操作。'); } }, addWatermark(imagePath) { if (this.myCanvas) { const date = new Date(); const formattedDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`; const ctx = this.myCanvas.getContext('2d'); const img = new Image(); img.src = imagePath; img.onload = () => { ctx.drawImage(img, 0, 0, img.width, img.height); ctx.setFontSize(20); ctx.setFillStyle('rgba(255, 255, 255, 0.7)'); ctx.fillText(formattedDate, 10, img.height - 30); wx.canvasToTempFilePath({ canvasId: 'myCanvasId', success: (res) => { this.setData({ photosfzBack: res.tempFilePath, }); }, }); }; } else { console.error('全局画布变量未初始化成功。'); } },
10-18 - wx.chooseImage调用有相机打印res.tempImagePath为undefined?
wx.chooseImage({ count: 1, sourceType: ['camera'], success: (res) => { this.setData({ ifphotosfzFront: false, photosfzFront: res.tempImagePath, }); console.log('照片路径:', res.tempImagePath); }, fail: (err) => { console.error(err); }, }); 上面是JS 下面HTML <view class="left-section" bindtap="takesfzFront"> <image wx:if="{{ifphotosfzFront}}" class="section-image" src="/images/sfzFront.png" mode="" /> <image wx:else src="{{photosfzFront}}"></image> <view>上传身份证正面</view> </view>
10-17