213 lines
9.7 KiB
Java
213 lines
9.7 KiB
Java
|
package cn.com.tenlion.systemcard.util;
|
|||
|
|
|||
|
import com.baidu.aip.util.Base64Util;
|
|||
|
import org.json.JSONObject;
|
|||
|
|
|||
|
import javax.imageio.ImageIO;
|
|||
|
import java.awt.image.BufferedImage;
|
|||
|
import java.io.*;
|
|||
|
import java.net.HttpURLConnection;
|
|||
|
import java.net.URL;
|
|||
|
import java.net.URLEncoder;
|
|||
|
import java.util.HashMap;
|
|||
|
import java.util.List;
|
|||
|
import java.util.Map;
|
|||
|
|
|||
|
/**
|
|||
|
* 统一信用代码证(营业执照)识别工具类
|
|||
|
* ClassName: OCRBusinessLicenseUtil
|
|||
|
* @Description: TODO
|
|||
|
* 创建工具: IDEA
|
|||
|
* 运行环境: [Tomcat7以上,MySql5.6以上,JDK7以上]
|
|||
|
* @author 崔宝铖
|
|||
|
* @date 2021年3月19日
|
|||
|
*/
|
|||
|
public class OCRBusinessLicenseUtil {
|
|||
|
|
|||
|
private static String apiKey = "6thm2Soep5COKhhvqGxCID3p";
|
|||
|
|
|||
|
private static String secretKey = "5f0tjGe6uvYbf72vUrNH5hRFmll4yA1m";
|
|||
|
|
|||
|
private static String accessToken = "";
|
|||
|
|
|||
|
// 鉴权token获取Url
|
|||
|
private static String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
|
|||
|
|
|||
|
// 营业执照识别url
|
|||
|
private static String businessLicenseUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license";
|
|||
|
|
|||
|
public static void main(String[] args) {
|
|||
|
BusinessLicenseBean result = businessLicenseFile("D:\\1.jpg");
|
|||
|
System.out.println(result);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 传入本地图片路径识别经营许可证
|
|||
|
* @param filePath
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static BusinessLicenseBean businessLicenseFile(String filePath) {
|
|||
|
BusinessLicenseBean bean = new BusinessLicenseBean();
|
|||
|
try {
|
|||
|
// 本地文件路径
|
|||
|
byte[] imgData = getFileBytes(new File(filePath));
|
|||
|
String imgStr = Base64Util.encode(imgData);
|
|||
|
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
|
|||
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
|
|||
|
Map<String, Object> params = new HashMap<String, Object>();
|
|||
|
params.put("image", imgParam);
|
|||
|
params.put("access_token", accessToken);
|
|||
|
String result = HttpUtil.doPost(businessLicenseUrl, params);
|
|||
|
// AccessToken如果过期
|
|||
|
if (result.contains("Access token invalid or no longer valid")) {
|
|||
|
accessToken = getAuth(apiKey, secretKey);
|
|||
|
return businessLicenseFile(filePath);
|
|||
|
}
|
|||
|
JSONObject jsonObject = new JSONObject(result);
|
|||
|
JSONObject wordsResult = jsonObject.getJSONObject("words_result");
|
|||
|
String licenseId = wordsResult.getJSONObject("社会信用代码").getString("words");
|
|||
|
String licenseNumber = wordsResult.getJSONObject("证件编号").getString("words");
|
|||
|
String licenseName = wordsResult.getJSONObject("单位名称").getString("words");
|
|||
|
String licenseType = wordsResult.getJSONObject("类型").getString("words");
|
|||
|
String licenseResidence = wordsResult.getJSONObject("地址").getString("words");
|
|||
|
String licenseLegalPerson = wordsResult.getJSONObject("法人").getString("words");
|
|||
|
String licenseCapital = wordsResult.getJSONObject("注册资本").getString("words");
|
|||
|
String licenseFoundTime = wordsResult.getJSONObject("成立日期").getString("words");
|
|||
|
String licenseTerm = wordsResult.getJSONObject("有效期").getString("words");
|
|||
|
String licenseBusiness = wordsResult.getJSONObject("经营范围").getString("words");
|
|||
|
String licenseAuthority = wordsResult.getJSONObject("登记机关").getString("words");
|
|||
|
String licenseComposition = wordsResult.getJSONObject("组成形式").getString("words");
|
|||
|
bean.setLicenseId(licenseId);
|
|||
|
bean.setLicenseNumber(licenseNumber);
|
|||
|
bean.setLicenseName(licenseName);
|
|||
|
bean.setLicenseType(licenseType);
|
|||
|
bean.setLicenseResidence(licenseResidence);
|
|||
|
bean.setLicenseLegalPerson(licenseLegalPerson);
|
|||
|
bean.setLicenseCapital(licenseCapital);
|
|||
|
bean.setLicenseFoundTime(licenseFoundTime);
|
|||
|
bean.setLicenseTerm(licenseTerm);
|
|||
|
bean.setLicenseBusiness(licenseBusiness);
|
|||
|
bean.setLicenseAuthority(licenseAuthority);
|
|||
|
bean.setLicenseComposition(licenseComposition);
|
|||
|
return bean;
|
|||
|
} catch (Exception e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
return bean;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 传入网络图片路径识别经营许可证
|
|||
|
* @param url
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static BusinessLicenseBean businessLicenseUrl(String url) {
|
|||
|
BusinessLicenseBean bean = new BusinessLicenseBean();
|
|||
|
try {
|
|||
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
|
|||
|
Map<String, Object> params = new HashMap<String, Object>();
|
|||
|
params.put("url", url);
|
|||
|
params.put("access_token", accessToken);
|
|||
|
String result = HttpUtil.doPost(businessLicenseUrl, params);
|
|||
|
// AccessToken如果过期
|
|||
|
if (result.contains("Access token invalid or no longer valid")) {
|
|||
|
accessToken = getAuth(apiKey, secretKey);
|
|||
|
return businessLicenseUrl(url);
|
|||
|
}
|
|||
|
JSONObject jsonObject = new JSONObject(result);
|
|||
|
JSONObject wordsResult = jsonObject.getJSONObject("words_result");
|
|||
|
String licenseId = wordsResult.getJSONObject("社会信用代码").getString("words");
|
|||
|
String licenseNumber = wordsResult.getJSONObject("证件编号").getString("words");
|
|||
|
String licenseName = wordsResult.getJSONObject("单位名称").getString("words");
|
|||
|
String licenseType = wordsResult.getJSONObject("类型").getString("words");
|
|||
|
String licenseResidence = wordsResult.getJSONObject("地址").getString("words");
|
|||
|
String licenseLegalPerson = wordsResult.getJSONObject("法人").getString("words");
|
|||
|
String licenseCapital = wordsResult.getJSONObject("注册资本").getString("words");
|
|||
|
String licenseFoundTime = wordsResult.getJSONObject("成立日期").getString("words");
|
|||
|
String licenseTerm = wordsResult.getJSONObject("有效期").getString("words");
|
|||
|
String licenseBusiness = wordsResult.getJSONObject("经营范围").getString("words");
|
|||
|
String licenseAuthority = wordsResult.getJSONObject("登记机关").getString("words");
|
|||
|
String licenseComposition = wordsResult.getJSONObject("组成形式").getString("words");
|
|||
|
bean.setLicenseId(licenseId);
|
|||
|
bean.setLicenseNumber(licenseNumber);
|
|||
|
bean.setLicenseName(licenseName);
|
|||
|
bean.setLicenseType(licenseType);
|
|||
|
bean.setLicenseResidence(licenseResidence);
|
|||
|
bean.setLicenseLegalPerson(licenseLegalPerson);
|
|||
|
bean.setLicenseCapital(licenseCapital);
|
|||
|
bean.setLicenseFoundTime(licenseFoundTime);
|
|||
|
bean.setLicenseTerm(licenseTerm);
|
|||
|
bean.setLicenseBusiness(licenseBusiness);
|
|||
|
bean.setLicenseAuthority(licenseAuthority);
|
|||
|
bean.setLicenseComposition(licenseComposition);
|
|||
|
return bean;
|
|||
|
} catch (Exception e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static String getAuth(String apiKey, String secretKey) {
|
|||
|
// 获取token地址
|
|||
|
String getAccessTokenUrl = authHost
|
|||
|
// 1. grant_type为固定参数
|
|||
|
+ "grant_type=client_credentials"
|
|||
|
// 2. 官网获取的 API Key
|
|||
|
+ "&client_id=" + apiKey
|
|||
|
// 3. 官网获取的 Secret Key
|
|||
|
+ "&client_secret=" + secretKey;
|
|||
|
try {
|
|||
|
URL realUrl = new URL(getAccessTokenUrl);
|
|||
|
// 打开和URL之间的连接
|
|||
|
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
|
|||
|
connection.setRequestMethod("GET");
|
|||
|
connection.connect();
|
|||
|
// 获取所有响应头字段
|
|||
|
Map<String, List<String>> map = connection.getHeaderFields();
|
|||
|
// 遍历所有的响应头字段
|
|||
|
for (String key : map.keySet()) {
|
|||
|
System.err.println(key + "--->" + map.get(key));
|
|||
|
}
|
|||
|
// 定义 BufferedReader输入流来读取URL的响应
|
|||
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|||
|
String result = "";
|
|||
|
String line;
|
|||
|
while ((line = in.readLine()) != null) {
|
|||
|
result += line;
|
|||
|
}
|
|||
|
/**
|
|||
|
* 返回结果示例
|
|||
|
*/
|
|||
|
// System.err.println("result:" + result);
|
|||
|
JSONObject jsonObject = new JSONObject(result);
|
|||
|
String access_token = jsonObject.getString("access_token");
|
|||
|
return access_token;
|
|||
|
} catch (Exception e) {
|
|||
|
System.err.printf("获取token失败!");
|
|||
|
e.printStackTrace(System.err);
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
private static byte[] getFileBytes(File file) throws IOException {
|
|||
|
String fileName = file.getName();
|
|||
|
String[] names = fileName.split("\\.");
|
|||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|||
|
if (names.length > 1) {
|
|||
|
String fileType = names[names.length - 1];
|
|||
|
BufferedImage bi;
|
|||
|
try {
|
|||
|
bi = ImageIO.read(file);
|
|||
|
ImageIO.write(bi, fileType, byteArrayOutputStream);
|
|||
|
byte[] bytes = byteArrayOutputStream.toByteArray();
|
|||
|
byteArrayOutputStream.close();
|
|||
|
return bytes;
|
|||
|
} catch (IOException e) {
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
}
|