java解决行驶证识别问题

博主使用百度提供的行驶证识别API进行开发。官网方法针对本地图片识别,而博主需求是前端上传图片直接识别,不保存图片。于是采用第二种方法,将上传图片转为二进制数组进行API调用与识别,并分享了代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里我是用百度提供的行驶证识别API,先上百度API镇贴地址:https://cloud.baidu.com/product/ocr

接下来是官网给的官方sdk中方法:

public void sample(AipOcr client) {
    // 传入可选参数调用接口
    HashMap<String, String> options = new HashMap<String, String>();
    options.put("detect_direction", "true");
    options.put("accuracy", "normal");


    // 参数为本地图片路径
    String image = "test.jpg";
    JSONObject res = client.vehicleLicense(image, options);
    System.out.println(res.toString(2));

    // 参数为本地图片二进制数组
    byte[] file = readImageFile(image);
    res = client.vehicleLicense(file, options);
    System.out.println(res.toString(2));

}

官网给的方法是针对于本地的图片进行识别,我当初需要开发的是前端上传图片,图片不需要保存,直接识别

所以我就用了第二种方法,参数与本地图片二进制数组,根据接收上传到的图片,将图片转成二进制数组进行API调用与识别

接下来

上我的代码

public void dentificationi(@RequestParam(value = "file", required = false) MultipartFile file) {
    try {
        if (file != null) {
          byte[] flies = file.getBytes();//获取图片的二进制数组
          HashMap<String, String> options = new HashMap<String, String>();
          options.put("detect_direction", "true");
          options.put("accuracy", "normal");
          String fileName = file.getOriginalFilename();// 文件原名称
          // 将文件类型截出来,判断文件类型
          String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
          if (type != null) {// 判断文件类型是否为空
            //这里是规定图片的类型,必须是png和jpg,我这里规定的是图片的后缀,如果需要上传别的类型文件,可以自行更改
            if ("BMP".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
              // 转存文件到指定的路径
              AipOcr client = new AipOcr(AipOcrConst.APPID, AipOcrConst.APIKEY, AipOcrConst.SECRETKEY);
              JSONObject res = client.vehicleLicense(flies, options);
              //    截取车牌号
              JSONObject resultJsonObject = res.getJSONObject("words_result");
              String engineNumber = resultJsonObject.getJSONObject("发动机号码").getString("words");//发动机号码
              String plateNumber = resultJsonObject.getJSONObject("号牌号码").getString("words");
              String owner = resultJsonObject.getJSONObject("所有人").getString("words");//所有人
              String vehicleIdentificationNumber = resultJsonObject.getJSONObject("车辆识别代号").getString("words");//车辆识别代号
               log.info("<--engineNumber-->"+engineNumber+"<--plateNumber-->"+plateNumber+"<--owner-->"+owner);
            }
          } else {
             log.error("文件类型错误");
          }
        } else {
            log.error("文件类型为空");
        }
      } 
    } catch (Exception e) {
      log.error("行驶证识别失败:" + e.getMessage());
    }
  }

这样就大功告成了。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值