全部代碼下載下傳位址: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(); } } } }