整体流程:
- 获得access_token
- 调用img.superresolution得到media_id
- 根据media_id下载图片
注:虽然以下的几个接口都是服务端API,但是我都是在客户端调用的,实测可行。
一、获取accecc_token
请求地址:
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
示例代码:
var that = this let APPID = "xxx" let APPSECRET = "xxx" wx.request({ url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}`, method: ‘GET‘, success: (res) =>{ console.log(res) console.log(res.data.access_token) that.setData({ access_token: res.data.access_token }) }, fail: (res) =>{ console.log(res) }, })
二、调用图像清晰化接口
请求地址:
POST https://api.weixin.qq.com/cv/img/superresolution?img_url=ENCODE_URL&access_token=ACCESS_TOCKEN
示例代码:
wx.request({ url: "https://api.weixin.qq.com/cv/img/superresolution", data: { img_url: "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=8816325c4036acaf4ded9eae1db0e675/fcfaaf51f3deb48fcfadd0bbfb1f3a292cf5788a.jpg", access_token: that.data.access_token, }, method: ‘POST‘, dataType: "json", header: { ‘content-type‘: ‘application/x-www-form-urlencoded‘ }, success: (res) => { console.log(res) that.setData({ media_id: res.data.media_id }) }, fail: (res) => { console.log(res) }, })
三、下载图片
返回的media_id有效期为3天,期间可以通过“获取临时素材”接口获取图片二进制,示例:
curl "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID" -o "output.jpg"
示例代码:
setTimeout(function () { var downloadUrl = `https://api.weixin.qq.com/cgi-bin/media/get?access_token=${that.data.access_token}&media_id=${that.data.media_id}` console.log(downloadUrl) that.setData({ downloadUrl: downloadUrl }) }, 2000)
如果要从输入框获取图片URL,textarea标签挺不错的
<view class="section"> <view>图片URL: </view> <textarea maxlength="1000" bindblur="bindTextAreaBlur" auto-height placeholder="请输入图片链接" /> </view>
bindTextAreaBlur: function (e) { console.log(e.detail.value) this.setData({ imgUrl: e.detail.value }) },
原文地址:https://www.cnblogs.com/lfri/p/12054117.html
时间: 2024-11-04 14:11:43