本文共 1768 字,大约阅读时间需要 5 分钟。
框架上使用了mpVue,下载微信开发者工具后跟着教程一步步执行,如果没有Node.js环境,需要先安装Node.js。
一开始用了自己服务器搭后台,踩了很多坑。
建议直接使用小程序云开发环境 1)在src/main.js文件添加以下语句初始化云开发wx.cloud.init({ env: '在云开发平台新建的环境' traceUser: true})
2)在根目录下的project.config.json加上本地函数路径,并新建对应目录
{ "cloudfunctionRoot": "/static/functions/"}
编译后,即可看到该目录下显示云开发环境名
强烈建议可按下列教程配置一下,之后会开发会方便很多(亲测可用,不配置也可)
1)在对应页面的index.vue开发前端界面
wx.chooseImage({ sizeType: ['original', 'compressed'], sourceType: ['album'], success: function (res) { that.identify(res.tempFilePaths[0], identifyType) } })
3)获取百度token
wx.request({ url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + that.API_KEY + '&client_secret=' + that.SECRET_KEY + '&', header: { 'content-type': 'application/json' }, success (res) { that.userInfo.token = res.data.access_token } })
4)解析图片并调用百度识别接口
wx.showLoading({ title: '识别中' }) wx.getFileSystemManager().readFile({ filePath: imageUrl, encoding: 'base64', success: res => { wx.request({ url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic', method: 'post', data: { access_token: xxxx, detect_direction: true, image: res.data}, header: { 'content-type': 'application/x-www-form-urlencoded' }, success: (res) => { console.log(res.data.words_result) } }) wx.hideLoading() } })
最后,放上我做的文字、图片识别小程序二维码
转载地址:http://zriob.baihongyu.com/