美文网首页uniAPP
UNIAPP 中百度车牌识别调用

UNIAPP 中百度车牌识别调用

作者: Ankkaya | 来源:发表于2022-03-23 10:26 被阅读0次

    uniapp 安卓,小程序客户端调用百度车牌识别

    准备工作

    创建应用

    登录百度智能云平台

    进入安全认证,创建 Access Key

    image.png

    产品服务选择,人工智能 - 文字识别,创建应用。可用服务列表选择 交通场景OCR,开通对应的 车牌识别 服务。

    生成 Token

    使用创建的 Access Key 和 Secret Key 根据规则生成 Access Token

    接口调用

    注意车牌识别接口文档中请求头和参数提交要求,下面以提交参数 image 为例

    图片 base64 转换

    虽然 app 和小程序端都做了转化,不过小程序端没有对图片做压缩处理

    app 端

    // 压缩图片
    /** 
     * dst 压缩图片的地址
     * overwrite 是否覆盖原文件,true 为覆盖。如果不覆盖原文件,需要在选择图片时候不勾选原图
     **/
    plus.zip.compressImage({
      src: path,
      dst: path,
      overwrite: true,
      quality: 20,
      width: '780px',
      height: '1040px',
      format: 'jpg'
    }, function(res) {
      // 读取图片 base64 编码
      let reader = new plus.io.FileReader()
      reader.onloaded = filData => {
        // param 中 img 车牌识别的提交参数,encodeURL 转换
        that.param.img = encodeURL(fileData.target.result)
        // 调用请求接口
      }
    }, function(err) {
      console.log(err)
    })
    

    小程序端

    uni.getFileSystemManager().readFile({
      filePath: path,
      encoding: 'base64',
      success: res => {
        that.param.img = encodeURL(res.data)
        // 调用请求接口
      }
    })
    

    请求接口

    uni.request() {
      url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=自己生成的',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: that.param,
      success: res => {
        console.log(res)
      },
      fail: err => {
        console.log(err)
      }
    }
    

    注意

    1. 接口常见问题中提示需要去除 base64 转码后的图片头,如果请求失败,可尝试去除
    img.replace('/^data:image\/\w+;base64,/', '')
    

    相关文章

      网友评论

        本文标题:UNIAPP 中百度车牌识别调用

        本文链接:https://www.haomeiwen.com/subject/norcjrtx.html