package com.walker.openocr; import com.baidu.aip.ocr.AipOcr; import com.fasterxml.jackson.databind.JsonNode; import com.walker.infrastructure.utils.FileUtils; import com.walker.infrastructure.utils.JsonUtils; import com.walker.openocr.oil.OilPrintResolver; import com.walker.openocr.table.TableConfig; import com.walker.openocr.table.TableObject; import com.walker.openocr.table.TableTextResolver; import com.walker.openocr.table.config.FileConfigLoader; import com.walker.openocr.util.BaiduOcrUtils; import com.walker.openocr.util.FileReaderUtils; import org.json.JSONObject; import org.junit.Test; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; public class BaiduOcrTest { //设置APPID/AK/SK public static final String APP_ID = "18547554"; public static final String API_KEY = "epNhm4LRAhSpDTCB6Wfc8VQO"; public static final String SECRET_KEY = "ICwmmmVQB7A89dhtiPfu0q8OiwDh1Qpb"; private AipOcr client = null; private String folder = "D:/dev_tools/workspace_2022/walker-dependencies/walker-ml-openocr/src/test/resources"; // @Test public void resolveOilSinopecWx() throws Exception{ String filePath = folder + "/oil_json_dqsh_print.txt"; byte[] content = FileUtils.getFileBytes(new File(filePath)); JsonNode jsonNode = JsonUtils.jsonStringToObjectNode(new String(content)); int textCount = jsonNode.get("words_result_num").asInt(); System.out.println("共有文本数量:" + textCount); List textBlocks = new ArrayList<>(); JsonNode wordNode = null; for(Iterator it = jsonNode.get("words_result").elements(); it.hasNext();){ wordNode = it.next(); textBlocks.add(BaiduOcrUtils.acquireTextBlock(wordNode)); } FileConfigLoader oilSinopecConfig = new FileConfigLoader(); TableConfig tableConfig = oilSinopecConfig.load(folder + "/oil_config_zsh_wx.txt"); List tableConfigs = new ArrayList<>(4); tableConfigs.add(tableConfig); TextResolver textResolver = new OilPrintResolver(); TableObject tableObject = textResolver.resolve(textBlocks, tableConfigs); System.out.println("金额:" + tableObject.getTableDataMap().get("jin_e")); System.out.println("支付时间:" + tableObject.getTableDataMap().get("zhi_fu_shi_jian")); } @Test public void textOcr(){ client = new AipOcr(APP_ID, API_KEY, SECRET_KEY); // 可选:设置网络连接参数 client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); // 可选:设置代理服务器地址, http和socket二选一,或者均不设置 // client.setHttpProxy("proxy_host", proxy_port); // 设置http代理 // client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理 // 可选:设置log4j日志输出格式,若不设置,则使用默认配置 // 也可以直接通过jvm启动参数设置此环境变量 // System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties"); // 传入可选参数调用接口 HashMap options = new HashMap(); options.put("language_type", "CHN_ENG"); options.put("detect_direction", "true"); options.put("detect_language", "true"); options.put("probability", "true"); // 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false // options.put("vertexes_location", "true"); // 调用接口 // String path = "F:/app_ocr_demo/ocr_oil_002.jpg"; // String path = "F:/app_ocr_demo/ocr_oil_010.jpg"; // String path = "F:/app_ocr_demo/ocr_oil_014.jpg"; String path = "F:/app_ocr_demo/ocr_e_002.jpg"; // String path = "F:/app_ocr_demo/ocr_oil_017.jpg"; // String path = "F:/app_ocr_demo/ocr_oil_011.jpg"; // JSONObject res = client.basicGeneral(path, options); JSONObject res = client.general(path, options); // 包含位置 if(res.has("error_code")){ System.out.println("调用返回错误:" + res.getString("error_msg")); return; } String data = res.toString(2); // FileReaderUtils.saveJson2File(data, "D:/dev_tools/workspace_2022/walker-dependencies/walker-ml-openocr/src/test/resources/oil_json_default_print.txt"); System.out.println(data); } // @Test public void readJson2BlockText() throws Exception{ String filePath = "D:/dev_tools/workspace_2022/walker-dependencies/walker-ml-openocr/src/test/resources/oil_json.txt"; byte[] content = FileUtils.getFileBytes(new File(filePath)); JsonNode jsonNode = JsonUtils.jsonStringToObjectNode(new String(content)); int textCount = jsonNode.get("words_result_num").asInt(); System.out.println("共有文本数量:" + textCount); List textBlocks = new ArrayList<>(); JsonNode wordNode = null; for(Iterator it = jsonNode.get("words_result").elements(); it.hasNext();){ wordNode = it.next(); textBlocks.add(BaiduOcrUtils.acquireTextBlock(wordNode)); } for(TextBlock block : textBlocks){ System.out.println(block); } } }