| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.File; |
| | | import java.util.Base64; |
| | | import java.util.*; |
| | | import java.nio.file.Files; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 腾讯云OCR工具类 |
| | |
| | | // {"患者签名(手印)", "签字人身份证号码", "日期", "联系电话", "本人", "签字人与患者关系"} |
| | | req.setItemNames(itemNames != null ? itemNames : new String[]{"患者姓名", "性别", "年龄", "身份证号", "诊断", "需支付转运费用", "行程", "开始时间", "结束时间", "家属签名"}); |
| | | req.setOutputLanguage("cn"); |
| | | req.setReturnFullText(false); |
| | | req.setReturnFullText(true); |
| | | req.setItemNamesShowMode(false); |
| | | ExtractDocMultiResponse resp = client.ExtractDocMulti(req); |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | //将 WordList |
| | | List<String> wordListResult = new ArrayList<>(); |
| | | if (responseData.containsKey("WordList") && responseData.getJSONArray("WordList") != null){ |
| | | JSONArray wordList = responseData.getJSONArray("WordList"); |
| | | for (int i = 0; i < wordList.size(); i++) { |
| | | JSONObject word = wordList.getJSONObject(i); |
| | | // { |
| | | // "Coord": { |
| | | // "LeftBottom": { |
| | | // "X": 472, |
| | | // "Y": 1500 |
| | | // }, |
| | | // "LeftTop": { |
| | | // "X": 467, |
| | | // "Y": 1420 |
| | | // }, |
| | | // "RightBottom": { |
| | | // "X": 636, |
| | | // "Y": 1490 |
| | | // }, |
| | | // "RightTop": { |
| | | // "X": 631, |
| | | // "Y": 1410 |
| | | // } |
| | | // }, |
| | | // "DetectedText": "行程:" |
| | | // } |
| | | String detectedText = word.getString("DetectedText"); |
| | | wordListResult.add(detectedText); |
| | | } |
| | | } |
| | | //我们从wordListResult中行程:后面,需要支付转运费用:之间的文字 |
| | | String content = extractContentFromWordList(wordListResult, "行程:", "需支付转运费用:"); |
| | | log.info("提取到行程: {}", content); |
| | | resultMap.put("行程", content); |
| | | |
| | | log.info("手写体识别提取到 {} 个字段", resultMap.size()); |
| | | return resultMap; |
| | |
| | | } |
| | | } |
| | | |
| | | private static String extractContentFromWordList(List<String> wordListResult, String s, String s1) { |
| | | //提取s和s1之间的内容 |
| | | //如果word中只有一或-或->,统一处理成-> |
| | | |
| | | |
| | | int startIndex = -1; |
| | | int endIndex = -1; |
| | | |
| | | for (int i = 0; i < wordListResult.size(); i++) { |
| | | String word = wordListResult.get(i); |
| | | if (word.contains(s)) { |
| | | startIndex = i; |
| | | } |
| | | |
| | | if (word.contains(s1)) { |
| | | endIndex = i; |
| | | } |
| | | } |
| | | if (startIndex == -1 || endIndex == -1 || startIndex >= endIndex) { |
| | | return ""; |
| | | } |
| | | List<String> w=wordListResult.subList(startIndex + 1, endIndex); |
| | | Boolean findAle=false; |
| | | List<String> result=new ArrayList<>(); |
| | | for(String word:w){ |
| | | if (!findAle && (word.equals("-") || word.equals("->") || word.equals("→") || word.equals("一") || word.equals("=>")) ){ |
| | | findAle = true; |
| | | word = word.replace("-", "→") |
| | | .replace("一", "→") |
| | | .replace("=>", "→"); |
| | | } |
| | | result.add(word); |
| | | }; |
| | | return String.join("", result); |
| | | } |
| | | |
| | | /** |
| | | * 身份证识别 |
| | | * @param imagePath 图片路径 |