全部代码下载地址:https://download.csdn.net/download/qq_33333654/11914449
package com.jeeplus.ocr; import com.alibaba.fastjson.JSON; import com.baidu.aip.ocr.AipOcr; import com.baidu.aip.util.Base64Util; import com.jeeplus.common.utils.CacheUtils; import com.jeeplus.common.utils.DateUtils; import com.jeeplus.common.utils.HttpUtil; import com.jeeplus.common.utils.StringUtils; import com.jeeplus.modules.sys.common.Constants; import com.jeeplus.ocr.bean.AccessTokenResultBean; import com.jeeplus.ocr.bean.AipOcrBasicGeneralResultBean; import com.jeeplus.ocr.bean.AipOcrCustomResultBean; import org.json.JSONObject; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.*; /** * @ProjectName: Hematopathy * @Package: com.jeeplus.ocr * @ClassName: AipOcrUtil * @Author: MC * @Description: ${description} * @Date: 2019/10/23 0023 10:48 * @Version: 1.0 */ public class AipOcrUtil { /** * @Method 通用文字识别 * @Author MC path:本地文件路径 options:请求API参数,可为空 * @Return * @Date 2019/10/23 0023 11:36 */ public static AipOcrBasicGeneralResultBean basicGeneralByPath(String path, HashMap<String, String> options){ if(checkFileIsImg(path)){ if(options == null || options.isEmpty()){ 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"); AipOcr client = AipOcrSingleton.getAipOcr(); JSONObject res = client.basicGeneral(path, options); String resStr = res.toString(); AipOcrBasicGeneralResultBean bean = JSON.parseObject(resStr, AipOcrBasicGeneralResultBean.class); return bean; } return null; } /** * @Method 通用文字识别 * @Author MC url:网络图片地址 options:请求API参数,可为空 * @Return * @Date 2019/10/23 0023 11:36 */ public static AipOcrBasicGeneralResultBean basicGeneralByURL(String url, HashMap<String, String> options){ if(!StringUtils.isEmpty(url)){ if(options == null || options.isEmpty()){ 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"); AipOcr client = AipOcrSingleton.getAipOcr(); JSONObject res = client.basicGeneralUrl(url, options); String resStr = res.toString(); AipOcrBasicGeneralResultBean bean = JSON.parseObject(resStr, AipOcrBasicGeneralResultBean.class); return bean; } return null; } /** * @Method 根据模版ID或者分类器ID识别本地图片 * @Author MC path:本地图片路径 templateSign:指定模版ID classifierId:指定分类器ID 模版ID与分类ID至少一个不为空 * @Return * @Date 2019/10/23 0023 13:59 */ public static AipOcrCustomResultBean customByPath(String path,String templateSign,String classifierId){ if(checkFileIsImg(path)){ if(StringUtils.isEmpty(templateSign) && StringUtils.isEmpty(classifierId)){ return null; } HashMap<String, String> options = new HashMap<String, String>(); if(!StringUtils.isEmpty(templateSign)){ options.put("templateSign", templateSign); } if(!StringUtils.isEmpty(classifierId)){ options.put("classifierId", classifierId); } AipOcr client = AipOcrSingleton.getAipOcr(); JSONObject res = client.custom(path, options); String resStr = res.toString(); AipOcrCustomResultBean bean = JSON.parseObject(resStr, AipOcrCustomResultBean.class); return bean; } return null; } /** * @Method 使用API调用方式进行自定义模版或者分类器的文字识别 * @Author MC ak:app_key 必传 sk:secret_key 必传 filePath:本地文件路径 要求为图片格式 必传 templateSign:自定义模版ID classifierParams:自定义分类器ID templateSign和classifierParams至少传递一个 * @Return * @Date 2019/10/24 0024 11:34 */ public static AipOcrCustomResultBean customByApiUrl(String ak, String sk,String filePath,String templateSign,String classifierParams) throws Exception { if(StringUtils.isEmpty(ak) || StringUtils.isEmpty(sk) || StringUtils.isEmpty(filePath)){ return null; } if(StringUtils.isEmpty(templateSign) && StringUtils.isEmpty(classifierParams)){ return null; } if(checkFileIsImg(filePath)){ String imgStr = imgFile2Bytes2Encode(filePath); String param = ""; if(!StringUtils.isEmpty(classifierParams)){ param = "classifierId="+classifierParams+"&image=" + URLEncoder.encode(imgStr, "UTF-8"); }else{ param = "templateSign="+templateSign+"&image=" + URLEncoder.encode(imgStr, "UTF-8"); } AccessTokenResultBean auth = getAuth(ak, sk); if(auth == null || !StringUtils.isEmpty(auth.getError())){ return null; } String accessToken = auth.getAccess_token(); // 请求模板识别 String result = HttpUtil.ocrPost(Constants.OCR_API_URL_GET_RECOGNISE, accessToken, param); AipOcrCustomResultBean bean = JSON.parseObject(result, AipOcrCustomResultBean.class); return bean; } return null; } /** * 获取API访问token * 该token有一定的有效期,需要自行管理,当失效时需重新获取. * @param ak - 百度云官网获取的 API Key * @param sk - 百度云官网获取的 Securet Key * @return assess_token 示例: * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567" */ public static AccessTokenResultBean getAuth(String ak, String sk) { AccessTokenResultBean bean = (AccessTokenResultBean) CacheUtils.get(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN); if(bean != null){ //检查缓存中的token是否过期 long expires_in = bean.getMyTimeOut().getTime(); Date nowDate = new Date(); long nowDateTime = nowDate.getTime(); if(expires_in > nowDateTime){ return bean; }else{ CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN); } } // 获取token地址 String authHost = Constants.OCR_API_URL_GET_ACCESS_TOKEN + "?"; String getAccessTokenUrl = authHost // 1. grant_type为固定参数 + "grant_type=client_credentials" // 2. 官网获取的 API Key + "&client_id=" + ak // 3. 官网获取的 Secret Key + "&client_secret=" + sk; 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); AccessTokenResultBean accessTokenResultBean = JSON.parseObject(result, AccessTokenResultBean.class); if(StringUtils.isEmpty(accessTokenResultBean.getError())){//获取成功 进行缓存 Integer expires_in = accessTokenResultBean.getExpires_in(); if(expires_in > 0){ int days = expires_in / ( 60 * 60 * 24); Date date=new Date(); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.add(Calendar.DATE,days);//把日期往后增加一天.整数往后推,负数往前移动 date=calendar.getTime(); accessTokenResultBean.setMyTimeOut(date);//设置有效期 }else{ CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN); return null; } CacheUtils.put(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN,accessTokenResultBean); }else{ CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN); } return accessTokenResultBean; /* JSONObject jsonObject = new JSONObject(result); String access_token = jsonObject.getString("access_token"); return access_token;*/ } catch (Exception e) { System.err.printf("获取token失败!"); CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN); e.printStackTrace(System.err); } return null; } private static boolean checkFileIsImg(String path){ if(StringUtils.isEmpty(path)){ return false; } File srcFile = new File(path); // 判断源文件是否存在 if (!srcFile.exists()) { return false; } if(path.indexOf(".") == -1){ return false; } String suffix= path.substring(path.indexOf("."), path.length()); if( suffix.equalsIgnoreCase("jpg") || suffix.equalsIgnoreCase("PNG ") || suffix.equalsIgnoreCase("GIF") || suffix.equalsIgnoreCase("tif") || suffix.equalsIgnoreCase("bmp") ){ return false; } return true; } /** * @Method 图片文件转换为字节在进行base64转码 * @Author MC * @Return * @Date 2019/10/24 0024 10:19 */ public static String imgFile2Bytes2Encode(String filePath) throws IOException { if(!checkFileIsImg(filePath)){ return null; } byte[] imgData = readFileByBytes(filePath); String imgStr = Base64Util.encode(imgData); return imgStr; } /** * @Method 文件转换为字节在进行base64转码 * @Author MC * @Return * @Date 2019/10/24 0024 10:19 */ public static String file2Bytes2Encode(String filePath) throws IOException { if(StringUtils.isEmpty(filePath)){ return null; } File srcFile = new File(filePath); // 判断源文件是否存在 if (!srcFile.exists()) { return null; } byte[] imgData = readFileByBytes(filePath); String imgStr = Base64Util.encode(imgData); return imgStr; } /** * 根据文件路径读取byte[] 数组 */ public static byte[] readFileByBytes(String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException(filePath); } else { ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length()); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); short bufSize = 1024; byte[] buffer = new byte[bufSize]; int len1; while (-1 != (len1 = in.read(buffer, 0, bufSize))) { bos.write(buffer, 0, len1); } byte[] var7 = bos.toByteArray(); return var7; } finally { try { if (in != null) { in.close(); } } catch (IOException var14) { var14.printStackTrace(); } bos.close(); } } } }