1.注册小程序

小程序官网
注册完成后记录AppID

2.初始化开发环境

框架上使用了mpVue,下载微信开发者工具后跟着教程一步步执行,如果没有Node.js环境,需要先安装Node.js。
微信开发者工具
mpVue官方文档

3.初始化云开发环境

一开始用了自己服务器搭后台,踩了很多坑。
建议直接使用小程序云开发环境

云开发官方文档
1)在src/main.js文件添加以下语句初始化云开发

wx.cloud.init({
  env: '在云开发平台新建的环境'
  traceUser: true
})

2)在根目录下的project.config.json加上本地函数路径,并新建对应目录

{
   "cloudfunctionRoot": "/static/functions/"
}

编译后,即可看到该目录下显示云开发环境名
在这里插入图片描述

强烈建议可按下列教程配置一下,之后会开发会方便很多(亲测可用,不配置也可)
云函数突破二十个云函数限制 实现一键切换环境

4.文字识别Demo开发

1)在对应页面的index.vue开发前端界面
在这里插入图片描述
2)图片上传

                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()
            }
          })

最后,放上我做的文字、图片识别小程序二维码

在这里插入图片描述

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐