diff --git a/src/main/java/cn/com/tenlion/BusinessCardApplication.java b/src/main/java/cn/com/tenlion/BusinessCardApplication.java index d079e4f..6856ac6 100644 --- a/src/main/java/cn/com/tenlion/BusinessCardApplication.java +++ b/src/main/java/cn/com/tenlion/BusinessCardApplication.java @@ -3,7 +3,6 @@ package cn.com.tenlion; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; import springfox.documentation.swagger2.annotations.EnableSwagger2; diff --git a/src/main/java/cn/com/tenlion/util/BusinessLicenseBean.java b/src/main/java/cn/com/tenlion/util/BusinessLicenseBean.java new file mode 100644 index 0000000..0f78e1f --- /dev/null +++ b/src/main/java/cn/com/tenlion/util/BusinessLicenseBean.java @@ -0,0 +1,188 @@ +package cn.com.tenlion.util; + +/** + * 统一信用代码证(营业执照)识别结果封装类 + * ClassName: BusinessLicenseBean + * @Description: TODO + * 创建工具: IDEA + * 运行环境: [Tomcat7以上,MySql5.6以上,JDK7以上] + * @author 崔宝铖 + * @date 2021年3月19日 + */ +public class BusinessLicenseBean { + /** + * 统一社会信用代码号 + */ + private String licenseId; + /** + * 证件编号 + */ + private String licenseNumber; + /** + * 单位名称 + */ + private String licenseName; + /** + * 类型 + */ + private String licenseType; + /** + * 地址 + */ + private String licenseResidence; + /** + * 法定代表人 + */ + private String licenseLegalPerson; + /** + * 注册资本 + */ + private String licenseCapital; + /** + * 成立时间 + */ + private String licenseFoundTime; + /** + * 营业期限 + */ + private String licenseTerm; + /** + * 经营范围 + */ + private String licenseBusiness; + /** + * 登记机关 + */ + private String licenseAuthority ; + /** + * 组成形式 + */ + private String licenseComposition; + + public String getLicenseComposition() { + return licenseComposition == null ? "" : licenseComposition; + } + + public void setLicenseComposition(String licenseComposition) { + this.licenseComposition = licenseComposition; + } + + public String getLicenseId() { + return licenseId == null ? "" : licenseId; + } + + public void setLicenseId(String licenseId) { + this.licenseId = licenseId; + } + + public String getLicenseNumber() { + return licenseNumber == null ? "" : licenseNumber; + } + + public void setLicenseNumber(String licenseNumber) { + this.licenseNumber = licenseNumber; + } + + public String getLicenseName() { + return licenseName == null ? "" : licenseName; + } + + public void setLicenseName(String licenseName) { + this.licenseName = licenseName; + } + + public String getLicenseType() { + return licenseType == null ? "" : licenseType; + } + + public void setLicenseType(String licenseType) { + this.licenseType = licenseType; + } + + public String getLicenseResidence() { + return licenseResidence == null ? "" : licenseResidence; + } + + public void setLicenseResidence(String licenseResidence) { + this.licenseResidence = licenseResidence; + } + + public String getLicenseLegalPerson() { + return licenseLegalPerson == null ? "" : licenseLegalPerson; + } + + public void setLicenseLegalPerson(String licenseLegalPerson) { + this.licenseLegalPerson = licenseLegalPerson; + } + + public String getLicenseCapital() { + return licenseCapital == null ? "" : licenseCapital; + } + + public void setLicenseCapital(String licenseCapital) { + this.licenseCapital = licenseCapital; + } + + public String getLicenseFoundTime() { + return licenseFoundTime == null ? "" : licenseFoundTime; + } + + public void setLicenseFoundTime(String licenseFoundTime) { + this.licenseFoundTime = licenseFoundTime; + } + + public String getLicenseTerm() { + return licenseTerm == null ? "" : licenseTerm; + } + + public void setLicenseTerm(String licenseTerm) { + this.licenseTerm = licenseTerm; + } + + public String getLicenseBusiness() { + return licenseBusiness == null ? "" : licenseBusiness; + } + + public void setLicenseBusiness(String licenseBusiness) { + this.licenseBusiness = licenseBusiness; + } + + public String getLicenseAuthority() { + return licenseAuthority == null ? "" : licenseAuthority; + } + + public void setLicenseAuthority(String licenseAuthority) { + this.licenseAuthority = licenseAuthority; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"licenseId\":\"") + .append(licenseId).append('\"'); + sb.append(",\"licenseNumber\":\"") + .append(licenseNumber).append('\"'); + sb.append(",\"licenseName\":\"") + .append(licenseName).append('\"'); + sb.append(",\"licenseType\":\"") + .append(licenseType).append('\"'); + sb.append(",\"licenseResidence\":\"") + .append(licenseResidence).append('\"'); + sb.append(",\"licenseLegalPerson\":\"") + .append(licenseLegalPerson).append('\"'); + sb.append(",\"licenseCapital\":\"") + .append(licenseCapital).append('\"'); + sb.append(",\"licenseFoundTime\":\"") + .append(licenseFoundTime).append('\"'); + sb.append(",\"licenseTerm\":\"") + .append(licenseTerm).append('\"'); + sb.append(",\"licenseBusiness\":\"") + .append(licenseBusiness).append('\"'); + sb.append(",\"licenseAuthority\":\"") + .append(licenseAuthority).append('\"'); + sb.append(",\"licenseComposition\":\"") + .append(licenseComposition).append('\"'); + sb.append('}'); + return sb.toString(); + } +} diff --git a/src/main/java/cn/com/tenlion/util/HttpUtil.java b/src/main/java/cn/com/tenlion/util/HttpUtil.java new file mode 100644 index 0000000..09c9ef1 --- /dev/null +++ b/src/main/java/cn/com/tenlion/util/HttpUtil.java @@ -0,0 +1,161 @@ +package cn.com.tenlion.util; + +import org.apache.log4j.Logger; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.util.Map; +import java.util.Map.Entry; + +/** + * 网络请求发送 + * ClassName: HttpUtil + * @Description: TODO + * 创建工具: IDEA + * 运行环境: [Tomcat7以上,MySql5.6以上,JDK7以上] + * @author 崔宝铖 + * @date 2019年6月19日 + */ +public class HttpUtil { + + /** 请求超时时间 */ + private static Integer TIMEOUT = 3000; + + private static Logger logger = Logger.getLogger(HttpUtil.class); + + /** + * Post请求 + * @Description: TODO + * @param @param postUrl + * @param @param param + * @param @return + * @return String + * @throws IOException + * @throws + * @author 崔宝铖 + * @date 2019年6月19日 + */ + public static String doPost(String postUrl, Map param) throws IOException { + // 封装发送的请求参数 + StringBuffer buffer = new StringBuffer(); + int x = 0; + for(Entry map : param.entrySet()) { + buffer.append(map.getKey()).append("=").append(map.getValue().toString()); + if(x != param.size()-1) { + buffer.append("&"); + } + x++; + } + URL url = new URL(postUrl); + URLConnection urlConnection = url.openConnection(); + HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; + httpUrlConnection.setConnectTimeout(TIMEOUT); + // 设置请求头属性参数 + httpUrlConnection.setRequestProperty("charset", "UTF-8"); + httpUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + httpUrlConnection.setRequestProperty("accept", "application/json"); + // 发送POST请求必须设置如下两行 + httpUrlConnection.setDoOutput(true); + httpUrlConnection.setDoInput(true); + String response = "";// 响应内容 + String status = "";// 响应状态 + PrintWriter out = null; + BufferedReader in = null; + try{ + // 获取URLConnection对象对应的输出流 + out = new PrintWriter(httpUrlConnection.getOutputStream()); + // 发送请求参数 + out.write(buffer.toString()); + // flush输出流的缓冲 + out.flush(); + httpUrlConnection.connect(); + // 定义BufferedReader输入流来读取URL的响应数据 + in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(), "UTF-8")); + String line; + while ((line = in.readLine()) != null) { + response += line; + } + // 获得URL的响应状态码 + status = new Integer(httpUrlConnection.getResponseCode()).toString(); + }catch(Exception e) { + logger.error("发送请求执行出现异常,参数:"+param.toString(), e); + }finally { + try { + if (out != null) { out.close();} + if (in != null) {in.close();} + } catch (Exception ex) { + logger.error("发送请求关流出现异常,参数:"+param.toString(), ex); + } + } + if(!"200".equals(status)) { + return null; + } + return response; + } + + /** + * Get请求 + * @Description: TODO + * @param @param getUrl 请求地址 + * @param @param param 请求参数 + * @param @return + * @param @throws Exception + * @return RetBody + * @throws + * @author 崔宝铖 + * @date 2019年6月19日 + */ + public static String doGet(String getUrl, Map param) throws Exception { + // 封装发送的请求参数 + StringBuffer buffer = new StringBuffer(); + int x = 0; + for(Entry map : param.entrySet()) { + buffer.append(map.getKey()).append("=").append(map.getValue().toString()); + if(x != param.size()-1) { + buffer.append("&"); + } + x++; + } + URL url = new URL(getUrl+"?"+buffer.toString()); + URLConnection urlConnection = url.openConnection(); + HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; + httpUrlConnection.setConnectTimeout(TIMEOUT); + // 设置请求头属性参数 + httpUrlConnection.setRequestProperty("charset", "UTF-8"); + httpUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + httpUrlConnection.setRequestProperty("accept", "application/json"); + String response = "";// 响应内容 + String status = "";// 响应状态6 + PrintWriter out = null; + BufferedReader in = null; + try{ + httpUrlConnection.connect(); + // 定义BufferedReader输入流来读取URL的响应数据 + in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(), "UTF-8")); + String line; + while ((line = in.readLine()) != null) { + response += line; + } + // 获得URL的响应状态码 + status = new Integer(httpUrlConnection.getResponseCode()).toString(); + }catch(Exception e) { + logger.error("发送请求执行出现异常,参数:"+param.toString(), e); + }finally { + try { + if (out != null) { out.close();} + if (in != null) {in.close();} + } catch (Exception ex) { + logger.error("发送请求关流出现异常,参数:"+param.toString(), ex); + } + } + if(!"200".equals(status)) { + return null; + } + return response; + } +} diff --git a/src/main/java/cn/com/tenlion/util/OCRBusinessLicenseUtil.java b/src/main/java/cn/com/tenlion/util/OCRBusinessLicenseUtil.java new file mode 100644 index 0000000..ad6b206 --- /dev/null +++ b/src/main/java/cn/com/tenlion/util/OCRBusinessLicenseUtil.java @@ -0,0 +1,211 @@ +package cn.com.tenlion.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:\\f43e12965d7f4778ad105b4b1b5e1a33.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 params = new HashMap(); + 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 params = new HashMap(); + 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> 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; + } + +} diff --git a/src/main/java/cn/com/tenlion/util/QRCodeUtil.java b/src/main/java/cn/com/tenlion/util/QRCodeUtil.java index 0b357ed..3f029da 100644 --- a/src/main/java/cn/com/tenlion/util/QRCodeUtil.java +++ b/src/main/java/cn/com/tenlion/util/QRCodeUtil.java @@ -12,7 +12,6 @@ import ink.wgink.util.UUIDUtil; import ink.wgink.util.date.DateUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; - import javax.annotation.PostConstruct; import javax.imageio.ImageIO; import java.awt.*; @@ -24,10 +23,22 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.Map; +/** + * 二维码生成工具类 + * ClassName: QRCodeUtil + * @Description: TODO + * 创建工具: IDEA + * 运行环境: [Tomcat7以上,MySql5.6以上,JDK7以上] + * @author 崔宝铖 + * @date 2021年3月19日 + */ @Component public class QRCodeUtil { + private static final String CHARSET = "utf-8"; + private static final String FORMAT_NAME = "JPG"; + // 二维码尺寸 private static final int QRCODE_SIZE = 300; // LOGO宽度 @@ -72,7 +83,6 @@ public class QRCodeUtil { /** * 插入LOGO - * * @param source 二维码图片 * @param imgPath LOGO图片地址 * @param needCompress 是否压缩 @@ -114,7 +124,6 @@ public class QRCodeUtil { /** * 生成二维码(内嵌LOGO) - * * @param content 内容 * @param imgPath LOGO地址 * @param destPath 存放目录 @@ -131,7 +140,6 @@ public class QRCodeUtil { /** * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) - * * @param destPath 存放目录 * @date 2013-12-11 上午10:16:36 */ @@ -145,7 +153,6 @@ public class QRCodeUtil { /** * 生成二维码(内嵌LOGO) - * * @param content 内容 * @param imgPath LOGO地址 * @param destPath 存储地址 @@ -157,7 +164,6 @@ public class QRCodeUtil { /** * 生成二维码 - * * @param content 内容 * @param destPath 存储地址 * @param needCompress 是否压缩LOGO @@ -169,7 +175,6 @@ public class QRCodeUtil { /** * 生成二维码 - * * @param content 内容 * @param destPath 存储地址 * @throws Exception @@ -180,7 +185,6 @@ public class QRCodeUtil { /** * 生成二维码(内嵌LOGO) - * * @param content 内容 * @param imgPath LOGO地址 * @param output 输出流 @@ -194,7 +198,6 @@ public class QRCodeUtil { /** * 生成二维码 - * * @param content 内容 * @param output 输出流 * @throws Exception @@ -205,7 +208,6 @@ public class QRCodeUtil { /** * 解析二维码 - * * @param file 二维码图片 * @return * @throws Exception @@ -228,7 +230,6 @@ public class QRCodeUtil { /** * 解析二维码 - * * @param path 二维码图片地址 * @return * @throws Exception @@ -239,7 +240,6 @@ public class QRCodeUtil { /** * 二维码默认样式 - * * @param content * @return * @throws Exception