当前位置: 首页 > news >正文

uniapp微信小程序调用百度OCR

uniapp编写微信小程序调用百度OCR
公司有一个识别行驶证需求,调用百度ocr识别
使用了image-tools这个插件,因为百度ocr接口用图片的base64
在这里插入图片描述
这里只是简单演示,accesstoken获取接口还是要放在服务器端,不然就暴露了自己的百度项目key

代码我使用了这个博主的代码
https://blog.csdn.net/sjt1010/article/details/108272945

还有需要将百度接口的地址加到微信公众平台,开发管理的服务器域名里

<template><view class="cheliangjinchudengji-index"><view><button type="primary" @click="chooseImg">行驶证拍照识别</button><view style="margin-left:10px ;" v-if="imgList.length > 0"><view v-for="(item,index) in imgList" :key="index"><image :src="item" mode=""></image></view></view><!-- <button type="primary" @click="getBaiduToken">token</button> --><view v-for="item of cardatas" :key="item"><view>{{item}}</view></view></view></view>
</template><script>import { pathToBase64, base64ToPath } from '../../js_sdk/mmmm-image-tools/index.js'import { fetchBaiduOCRDatas  } from '../../utils/basePath';export default {data() {return {imgList: [],token:'',cardatas:{chepai:'',xinghao:''}}},methods: {chooseImg(){let that = this;that.getBaiduToken();uni.chooseImage({success: (chooseImageRes) => {const tempFilePaths = chooseImageRes.tempFilePaths;// #ifdef MP-WEIXINuni.getFileSystemManager().readFile({filePath: tempFilePaths[0],encoding: 'base64',success: r => { console.log(r.data)let base64 = 'data:image/jpeg;base64,'  + r.data;that.imgList.push(base64)that.postBaiduOcr(base64);}})// #endif// // #ifdef APP-PLUS// pathToBase64(tempFilePaths[0])//   .then(base64 => {// 	that.imgList.push(base64)//   })//   .catch(error => {// 	console.error(error)//   })// // #endif}});},postBaiduOcr(imgbase64){let that = this;let accessToken = that.token;fetchBaiduOCRDatas("https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license?access_token="+accessToken, {"image":imgbase64} , {'content-type': 'application/x-www-form-urlencoded'}, "POST", res=>{if(res.statusCode=='200' || res.statusCode==200){uni.showToast({type: 'success',title: 'OCR成功!',icon: 'none',duration:3000});that.cardatas.chepai = res.data.words_result.号牌号码.words;that.cardatas.xinghao = res.data.words_result.品牌型号.words;}else{uni.showToast({type: 'error',title:  res.data.error_description,icon: 'none',duration:3000});}})},getBaiduToken(){fetchBaiduOCRDatas("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxx&client_secret=xxxx", {}, {'Content-Type': 'application/json', 'Accept': 'application/json'}, "POST", res=>{if(res.statusCode=='200' || res.statusCode==200){uni.showToast({type: 'success',title: 'token获取成功!',icon: 'none',duration:3000});this.token = res.data.access_token	}else{uni.showToast({type: 'error',title: res.data.error_description,icon: 'none',duration:3000});}})},}}
</script><style></style>
const fetchBaiduOCRDatas = (url, data, header, method, callback, errCallback)=>{// let token = uni.getStorageSync('token');uni.request({url: url,data: data,method:method,header: header,// {//      'content-type': 'application/x-www-form-urlencoded'//    },success (res) {uni.hideLoading();callback(res);}})
}

在这里插入图片描述
这个行驶证图片是百度开发平台提供的图片

有些人可能有base64传后台需求,但是拍照内存太大需要压缩,加一下压缩图片再转base64代码

chooseImg(){let that = this;that.getBaiduToken();uni.chooseImage({// sizeType: ['compressed'],success: (chooseImageRes) => {const tempFilePaths = chooseImageRes.tempFilePaths;// 压缩uni.compressImage({src: tempFilePaths[0],quality: 0.1,success: res => {uni.getFileSystemManager().readFile({filePath: res.tempFilePath,encoding: 'base64',success: r => { let base64 = 'data:image/jpeg;base64,'  + r.data;that.imgList.push(base64)that.postBaiduOcr(base64);console.log(`压缩111:${base64.length}KB`)}})}})}});
},
http://www.lryc.cn/news/462626.html

相关文章:

  • Vue3+TS项目---实用的复杂类型定义总结
  • 尚硅谷rabbitmq2024 工作模式路由篇 第11节 答疑
  • HTTP vs WebSocket
  • R语言医学数据分析实践-数据读写
  • JavaWeb环境下Spring Boot在线考试系统的优化策略
  • ETL技术在金蝶云星空与旺店通WMS集成中的应用
  • 【力扣热题100】3194. 最小元素和最大元素的最小平均值【Java】
  • 机器学习拟合过程
  • 如何快速部署一套智能化openGauss测试环境
  • 【设计模式】深入理解Python中的原型设计模式
  • Django CORS配置方案
  • 2024年开放式耳机哪个牌子好?推荐最好的顶级开放式耳机品牌
  • 零基础读懂Stable Diffusion!
  • Hash Join 和 Index Join工作原理和性能差异
  • Apifox简介及使用
  • 十、IPD 实施细节(产品设计与开发管理)
  • MySQL-13.DQL-聚合函数
  • 为什么跟别人学习如何证明定理要远比使用定理更有意义
  • Qt在Win,Mac和Linux的开机自启设置
  • spring boot热部署
  • 网关与蓝牙网关有什么不同之处?
  • JAVA计算双十一多产品实付款优惠券的省钱方案
  • 零售行业的数字化营销转型之路
  • js的for in 和 for of的详解
  • 前端工具函数库
  • Java程序设计:Spring boot(4)——Freemarker Thymeleaf视图技术集成
  • JavaScript 第19章:Web Storage
  • [山河2024] week2
  • 无限可能LangChain——开启大模型世界
  • URL路径以及Tomcat本身引入的jar包会导致的 SpringMVC项目 404问题、Tomcat调试日志的开启及总结