springboot集成百度云OCR,实现通用文字识别,身份证文字识别,车牌号识别等等

引入依赖

<dependency>
    <groupId>com.baidu.aip</groupId>
    <artifactId>java-sdk</artifactId>
    <version>4.15.8</version>
</dependency>

调用AipOcr

package com.study.baiduocr.util;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;

public class ApiOcrUtil {
    //设置APPID/AK/SK
    private static String APP_ID = "";
    private static String API_KEY ="";
    private static String SECRET_KEY = "";


    public static void main(String[] args) {

        // 初始化一个AipOcr
        AipOcr aipOcr = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        //设置本地图片地址
        String path = "C:\\\\Users\\\\admin\\\\Desktop\\\\微信图片_20220311103037.png";
        //通用文字识别
        basicGeneral(aipOcr,path);

    }

    /*
     * 通用文字识别
     * */
    public static void basicGeneral(AipOcr aipOcr,String path){
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");//识别语言类型,默认为中英文混合
        options.put("detect_direction", "true");//是否检查图片朝向,默认false不检查
        options.put("detect_language", "true");//是否检查语言,默认false不检查
        options.put("probability", "true");//是否返回识别结果中每一行的置信度

        // 调用接口,返回JSON格式数据
        JSONObject jsonObject = aipOcr.basicGeneral(path, options);
        //获取JSON对象里提取图片文字信息数组
        JSONArray jsonArray = jsonObject.getJSONArray("words_result");
        //循环打印信息
        for(int i = 0;i<jsonArray.length();i++){
            System.out.println(jsonArray.getJSONObject(i).get("words"));
        }
    }


    /*
     * 身份证文字识别
     * */
    public  static void idCord(AipOcr aipOcr,String path){
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");//检查图片朝向。默认false不检测
        options.put("detect_risk", "false");//是否开启身份证类型检测功能(类型为身份证复印件、临时身份证、身份证翻拍、修改过的身份证),默认false不开启
        String idCardSide = "front";//身份证含照片一面
//        String idCardSide = "back";//身份证含国徽一面
        //本地图片识别,返回JSON对象
        JSONObject jsonObject = aipOcr.idcard(path, idCardSide, options);
        //获取到我们需要的信息
        JSONObject result = jsonObject.getJSONObject("words_result");

        //获取迭代器,
        Iterator<String> iterator = result.keys();
        while (iterator.hasNext()){
            //获取键和值
            String key = iterator.next();
            String value = result.getJSONObject(key).getString("words");
            //打印识别结果
            System.out.println(key+" : "+value);
        }
    }

    /*
     * 车牌号识别
     * */
    public static void licencePlateNumber(AipOcr aipOcr,String path){
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("multi_detect", "false");//是否检测多张图片,默认false
        //本地图片识别,返回JSON对象
        JSONObject jsonObject = aipOcr.plateLicense(path, options);
        //获取需要的信息
        JSONObject result = jsonObject.getJSONObject("words_result");
        System.out.println("车牌颜色:"+result.getString("color")+"\n"+"车牌号:"+result.getString("number"));
    }





}

代码仓库地址

https://gitcode.net/m0_51527921/baiduocr.git

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大佬腿好粗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值