广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cn.huge.module.utils;
 
 
import cn.huge.module.mediate.constant.CaseBaseConstsEnum;
import cn.huge.module.mediate.constant.NotionBaseConstsEnum;
import cn.huge.module.sys.dto.IdcardOcrResultDTO;
import com.baidu.aip.ocr.AipOcr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @title: 百度云OCR工具类
 * @description: 百度云OCR工具类
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
@Slf4j
public class BaiduOcrUtils {
    // 公司百度云账号
//    public static final String APP_ID = "24661114";
//    public static final String API_KEY = "cKXdjXhgRP9fsbI15UxGB0jM";
//    public static final String SECRET_KEY = "1X1BY7oAAmjemQgzzdfG3VnTh0rUTO2X";
    // 个人账号-15602261488
    public static final String APP_ID = "27535980";
    public static final String API_KEY = "6U6tSA75gKf4UMXvHgDpzOC9";
    public static final String SECRET_KEY = "BBjjCDhy2sAVO0jfiaGn8OWIGE470lWU";
    // 个人账号-18607214221
    public static final String APP_ID_TEXT = "113806304";
    public static final String API_KEY_TEXT = "ajKtDuIs8xr0XvjbUZLPGi8C";
    public static final String SECRET_KEY_TEXT = "vgMrG5BJbUfGwKLda1yPWovTQWuRFWDB";
 
    public static void main(String[] args) {
 
    }
 
    /**
     * 身份证识别(二进制流)
     * @param images
     * @return
     */
    public static IdcardOcrResultDTO ocrIdcard(byte[] images) {
        try{
            AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
            // 设置识别身份证正面参数 front - 身份证含照片的一面 back - 身份证带国徽的一面
            String idCardSide = "front";
            // 自定义参数定义
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("detect_direction", "true");
            options.put("detect_risk", "true");
            JSONObject json = client.idcard(images, idCardSide, options);
            log.info("xsd:{}",json);
            JSONObject result = json.getJSONObject("words_result");
            // 解析识别结果
            IdcardOcrResultDTO idcardOcrResultDTO = new IdcardOcrResultDTO();
            String name = result.getJSONObject("姓名").getString("words");
            idcardOcrResultDTO.setTrueName(name);
 
            String sex = result.getJSONObject("性别").getString("words");
            idcardOcrResultDTO.setSexName(sex);
            idcardOcrResultDTO.setSex(CaseBaseConstsEnum.getIndex(sex));
 
            String idcard = result.getJSONObject("公民身份号码").getString("words");
            idcardOcrResultDTO.setCertiType(CaseBaseConstsEnum.CARD_TYPE_1.getIndex());
            idcardOcrResultDTO.setCertiTypeName(CaseBaseConstsEnum.CARD_TYPE_1.getDes());
            idcardOcrResultDTO.setCertiNo(idcard);
 
            String address = result.getJSONObject("住址").getString("words");
            idcardOcrResultDTO.setPlaceAddr(address);
 
            String bir = result.getJSONObject("出生").getString("words");
            String birthday = "";
            birthday += bir.substring(0,4)+"-"+bir.substring(4,6)+"-"+bir.substring(6,8);
            idcardOcrResultDTO.setBirthday(birthday);
 
            String nation = result.getJSONObject("民族").getString("words")+"族";
            idcardOcrResultDTO.setNationName(nation);
            idcardOcrResultDTO.setNation(NotionBaseConstsEnum.getIndex(nation));
            return idcardOcrResultDTO;
        }catch (Exception e){
            log.error("方法[BaiduOcrUtils.ocrIdcard]调用失败,异常信息:"+e, e);
            return null;
        }
    }
 
 
    public static Map<String, Object> ocrText(byte[] images) {
        AipOcr client = new AipOcr(APP_ID_TEXT, API_KEY_TEXT, SECRET_KEY_TEXT);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");
 
        Map<String, Object> map = new HashMap<String, Object>();
        // 参数为本地图片二进制数组
        JSONObject res = client.basicAccurateGeneral(images, options);
        map.put("wordsResultNum", res.getInt("words_result_num"));
        map.put("direction", res.getInt("direction"));
 
        List<String> wordsResultList = new ArrayList<>();
        JSONArray jsonArray = res.getJSONArray("words_result");
        for(int i = 0; i < jsonArray.length(); i++){
            JSONObject wordResult = jsonArray.getJSONObject(i);
            String words = wordResult.getString("words");
            if(StringUtils.isNotBlank(words)){
                wordsResultList.add(words);
            }
        }
        map.put("wordsResult", wordsResultList);
        return map;
    }
 
}
/**
 * -------------------_ooOoo_-------------------
 * ------------------o8888888o------------------
 * ------------------88" . "88------------------
 * ------------------(| -_- |)------------------
 * ------------------O\  =  /O------------------
 * ---------------____/`---'\____---------------
 * -------------.'  \\|     |//  `.-------------
 * ------------/  \\|||  :  |||//  \------------
 * -----------/  _||||| -:- |||||-  \-----------
 * -----------|   | \\\  -  /// |   |-----------
 * -----------| \_|  ''\---/''  |   |-----------
 * -----------\  .-\__  `-`  ___/-. /-----------
 * ---------___`. .'  /--.--\  `. . __----------
 * ------."" '<  `.___\_<|>_/___.'  >'"".-------
 * -----| | :  `- \`.;`\ _ /`;.`/ - ` : | |-----
 * -----\  \ `-.   \_ __\ /__ _/   .-` /  /-----
 * ======`-.____`-.___\_____/___.-`____.-'======
 * -------------------`=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * ---------佛祖保佑---hugeinfo---永无BUG----------
 */