关于vue 使用百度ocr识别跨域问题

关于vue 使用百度ocr识别行驶证

1.登录地址

链接: https://cloud.baidu.com/product/ocr?track=cp:nsem|pf:pc|pp:nsem-chanpin-ocr-xiaoguo|pu:OCR-tongyongci|ci:|kw:10027679&bd_vid=10495086453955106834.
根据需求创建应用

获取access_token

由于vue的axios请求百度ocr识别接口 会出现跨域问题 可以使用代理解决(vue 2.0 和 vue3.0 解决如下)

1.vue 2.0 proxyTable 代理解决:在config目录下的index.js文件中添加如下配置即可

// An highlighted block
module.exports = {
  dev: {
 
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: './',
    proxyTable: {
      '/baiduApi': {
        target: 'https://aip.baidubce.com', //访问地址
        changeOrigin: true,
        secure: false, //只有代理https 地址需要次选项
        pathRewrite: {
          '^/baiduApi': ''
        }
      }
 
    },
  1. vue 3.0 proxy代理解决:在vue.config.js文件中的devServer中添加如下配置即可
// An highlighted block
proxy: {
        '/baiduApi': {
        target: 'https://aip.baidubce.com', //访问地址
        changeOrigin: true,
        secure: false, //只有代理https 地址需要次选项
        pathRewrite: {
            '^/baiduApi': ''
            }
          }
        },

这个只能本地 线上用nginx代理

location ~ ^/(baiduApi)/.* {
        rewrite  ^.+baiduApi/?(.*)$ /$1 break;
         include  uwsgi_params;
        root   /usr/share/nginx/html;
        proxy_pass https://aip.baidubce.com;
 }
  1. 经过配置代理后即可请求获取access_token的接口
this.axios.get('/baiduApi/oauth/2.0/token?grant_type=client_credentials&client_id=iBlglRY8dh8HyKXBwgOkyn6H&client_secret=32uqmGIdHqjCK1xk7L7U2D7RcrBRkVRG&',{headers:{
       dataType:'json'
     }}).then(res =>{
       if(res.status ==200){
         this.access_token = res.data.access_token;
       }
     })

在 el-upload的 on-change事件中写入转化base64

1.promise封装 将图片转base64

   //将图片转为base64格式
           transformBase64(file){
               return new Promise(((resolve, reject) => {
                   var reader = new FileReader();
                   reader.readAsDataURL(file);
                   reader.onload = function(e){ 
                       resolve(e.target.result)
                   }
               }))
           },

2.promise封装 请求百度OCR行驶证识别
链接: 百度OCR行驶证识别接口文档.

carIdentify(ocrImg){
               return new Promise(((resolve, reject) => {
                   var params = new FormData();
                   params.append('image',ocrImg);
                   params.append('detect_direction', true );
                   params.append('vehicle_license_side', 'front' );
                   params.append("access_token", this.access_token);
                   axios.post("/baiduApi/rest/2.0/ocr/v1/vehicle_license?access_token",params,
                     {headers: {'content-type': 'application/x-www-form-urlencoded'} }
                   ).then((res)=>{
                     resolve(res.data.words_result)
                   })
               }))
           },

3.在 el-upload 的 on-change事件中 请求上面两个 promise请求

  uploadImg(file,fileList){
               this.transformBase64(file.raw).then(data=>{
                   this.carIdentify(data).then(res=>{
                       console.log(res)
                   })
               })
           },
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值