微信小程序图像增强img.superresolution接口
2023-09-27 14:27:45 时间
整体流程:
- 获得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 }) },
相关文章
- 微信公共平台开发接口(三)
- API接口DTO测试数据构造的一个方式
- 微信小程序 - 接口更新记录以及解决方案(2018/12/26)
- 关于接口测试问题,这些文章推荐你反复看看
- 微信小程序 - 在自定义组件中请求后端 API 数据接口(引入该组件的页面触发)组件在哪个生命周期钩子函数中请求接口数据呢?
- 浅谈 接口测试
- 接口测试方案(接口测试思路)
- 接口快速开发框架 magic-api 2.x初体验
- 微信公众号接口配置
- Go基础系列:空接口
- 微信接口调用'updateTimelineShareData','updateAppMessageShareData' 的踩坑记录
- 微信js SDK接口
- Spring Security OAuth 2.x的刷新token接口/oauth/token自定义修改
- Python使用Flask实现RESTful API,使用Postman工具、requests库测试接口
- 微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
- 微信小程序 封装接口
- 微信接口