package cn.huge.base.common.utils;
|
|
|
import cn.huge.module.mediate.constant.CaseBaseConstsEnum;
|
import com.baidu.aip.ocr.AipOcr;
|
import com.google.common.collect.Maps;
|
import org.json.JSONObject;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* @title: 百度云OCR工具类
|
* @description: 百度云OCR工具类
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
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";
|
|
public static void main(String[] args) {
|
|
}
|
|
/**
|
* 身份证识别(图片路径)
|
* @param imagePath
|
* @return
|
*/
|
public static JSONObject idcardRecoImg(String imagePath) {
|
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 response = client.idcard(imagePath, idCardSide, options);
|
return response;
|
|
}
|
|
/**
|
* 身份证识别(二进制流)
|
* @param images
|
* @return
|
*/
|
public static JSONObject idcardRecoByte(byte[] images) {
|
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 response = client.idcard(images, idCardSide, options);
|
return response;
|
}
|
|
/**
|
* 身份证识别(二进制流)
|
* @param images
|
* @return
|
*/
|
public static Map<String, Object> ocrIdcard(byte[] images) {
|
Map<String, Object> map = Maps.newHashMap();
|
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);
|
JSONObject result = json.getJSONObject("words_result");
|
String sex = result.getJSONObject("性别").getString("words");
|
map.put("sex",sex);
|
if(sex.equals(CaseBaseConstsEnum.SEX_1.getDes())){
|
map.put("sexName",CaseBaseConstsEnum.SEX_1.getIndex());
|
}else{
|
map.put("sexName",CaseBaseConstsEnum.SEX_2.getIndex());
|
}
|
String name = result.getJSONObject("姓名").getString("words");
|
map.put("trueName",name);
|
String address = result.getJSONObject("住址").getString("words");
|
map.put("sendAddress",address);
|
String idcard = result.getJSONObject("公民身份号码").getString("words");
|
map.put("certificateType", "09_00015-1");
|
map.put("certificateTypeName", "身份证");
|
map.put("certificateNumber",idcard);
|
String bir = result.getJSONObject("出生").getString("words");
|
String birthday = "";
|
birthday += bir.substring(0,4)+"-"+bir.substring(4,6)+"-"+bir.substring(6,8);
|
map.put("birthday",birthday);
|
String nation = result.getJSONObject("民族").getString("words")+"族";
|
map.put("nation",nation);
|
return map;
|
}
|
|
/**
|
* 身份证识别(二进制流)
|
*
|
* @param images
|
* @return
|
*/
|
public static Map<String, Object> ocrIdcard2(byte[] images) {
|
Map<String, Object> map = Maps.newHashMap();
|
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);
|
JSONObject result = json.getJSONObject("words_result");
|
String sex = result.getJSONObject("性别").getString("words");
|
map.put("sex", sex+"性");
|
String name = result.getJSONObject("姓名").getString("words");
|
map.put("name", name);
|
String address = result.getJSONObject("住址").getString("words");
|
map.put("address", address);
|
String idcard = result.getJSONObject("公民身份号码").getString("words");
|
map.put("idcard", idcard);
|
String bir = result.getJSONObject("出生").getString("words");
|
String birthday = "";
|
birthday += bir.substring(0, 4) + "-" + bir.substring(4, 6) + "-" + bir.substring(6, 8);
|
map.put("birthday", birthday);
|
String nation = result.getJSONObject("民族").getString("words") + "族";
|
map.put("nation", nation);
|
return map;
|
}
|
}
|
/**
|
* -------------------_ooOoo_-------------------
|
* ------------------o8888888o------------------
|
* ------------------88" . "88------------------
|
* ------------------(| -_- |)------------------
|
* ------------------O\ = /O------------------
|
* ---------------____/`---'\____---------------
|
* -------------.' \\| |// `.-------------
|
* ------------/ \\||| : |||// \------------
|
* -----------/ _||||| -:- |||||- \-----------
|
* -----------| | \\\ - /// | |-----------
|
* -----------| \_| ''\---/'' | |-----------
|
* -----------\ .-\__ `-` ___/-. /-----------
|
* ---------___`. .' /--.--\ `. . __----------
|
* ------."" '< `.___\_<|>_/___.' >'"".-------
|
* -----| | : `- \`.;`\ _ /`;.`/ - ` : | |-----
|
* -----\ \ `-. \_ __\ /__ _/ .-` / /-----
|
* ======`-.____`-.___\_____/___.-`____.-'======
|
* -------------------`=---='
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
* ---------佛祖保佑---hugeinfo---永无BUG----------
|
*/
|