xx.js
Page({
data:{
result:"",
},
playAudio(){
let word = this.data.result;
this.audioCtx = wx.createAudioContext('myAudio')
this.audioCtx.setSrc('https://dict.youdao.com/dictvoice?audio=' + word + '&type=2')
this.audioCtx.play()
},
takePhoto() {
const ctx = wx.createCameraContext()
ctx.takePhoto({
quality: 'low',
success: (res) => {
/*
this.setData({
src: res.tempImagePath
});
*/
wx.compressImage({
src: res.tempImagePath, // 图片路径
quality: 50, // 压缩质量
success: (res2) => {
this.uploadFile(res2.tempFilePath);
}
})
}
})
},
uploadFile(tempFilePaths){
var that = this;
wx.uploadFile({
url: 'https://zhyframe.fzh.fun/app/index.php?i=1023&t=0&v=1.1&from=wxapp&c=entry&a=wxapp&do=Api_common|uploadPic&&m=zhyshop_sun', //仅为示例,非真实的接口地址
filePath: tempFilePaths,
name: 'file',
success (res){
let data = res.data;
if ('string' == typeof data) data = JSON.parse(data);
if(data.code == 1){
app.tips(data.msg)
return
}
that.setData({
src: data.other.img_root + data.data,
})
that.img2Text(that.data.src);
// const data = res.data;
//do something
}
})
},
img2Text(url){
var that = this;
wx.request({
url: 'https://im.n8y.cn/api/imageToText', //仅为示例,并非真实的接口地址
data: {
url: url
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data);
that.setData({
result:res.data.text
});
that.playAudio();
}
})
},
error(e) {
console.log(e.detail)
}
})
x.wxml
<camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<button type="primary" bindtap="takePhoto">拍照</button>
<view bindtap="playAudio">结果:{{result}}</view>
<view>预览</view>
<image mode="widthFix" src="{{src}}"></image>
<audio id="myAudio" ></audio>