wlzboy
3 天以前 40a8157440e3b906da8f52e07d939d78c3f4c313
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package com.ruoyi.system.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
 
import com.ruoyi.system.config.TencentOCRConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.util.*;
import java.nio.file.Files;
 
/**
 * 腾讯云OCR工具类
 * 使用腾讯云OCR服务进行文字识别
 * 支持通用文字识别、手写体识别等多种识别类型
 * 
 * 使用示例:
 * // 通用文字识别
 * JSONObject result = TencentOCRUtil.generalRecognize("path/to/image.jpg");
 * 
 * // 手写体识别
 * JSONObject result = TencentOCRUtil.handwritingRecognize("path/to/image.jpg");
 */
@Component
public class TencentOCRUtil {
 
    private static final Logger log = LoggerFactory.getLogger(TencentOCRUtil.class);
 
    private static TencentOCRConfig staticTencentOcrConfig;
 
    @Autowired
    public void setTencentOcrConfig(TencentOCRConfig tencentOcrConfig) {
        TencentOCRUtil.staticTencentOcrConfig = tencentOcrConfig;
    }
 
    /**
     * 获取腾讯云OCR客户端实例
     * @return OcrClient客户端实例
     * @throws TencentCloudSDKException SDK异常
     */
    private static OcrClient getClient() throws TencentCloudSDKException {
        Credential cred = new Credential(
            staticTencentOcrConfig.getSecretId(), 
            staticTencentOcrConfig.getSecretKey()
        );
 
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(staticTencentOcrConfig.getEndpoint());
 
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
 
        return new OcrClient(cred, "", clientProfile);
    }
 
    /**
     * 通用文字识别(图片路径)
     * @param imagePath 图片路径
     * @return 识别结果
     */
    public static JSONObject generalRecognize(String imagePath) {
        try {
            byte[] imageBytes = Files.readAllBytes(new File(imagePath).toPath());
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            
            OcrClient client = getClient();
            GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
            req.setImageBase64(base64Image);
            
            GeneralBasicOCRResponse resp = client.GeneralBasicOCR(req);
            
            log.info("腾讯云OCR通用文字识别成功,图片路径: {}", imagePath);
            
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("data", JSON.parseObject(AbstractModel.toJsonString(resp)));
            result.put("content", extractContentFromTencentResult(JSON.parseObject(AbstractModel.toJsonString(resp))));
            
            return result;
            
        } catch (Exception e) {
            log.error("腾讯云OCR通用文字识别失败: {}", e.getMessage(), e);
            
            JSONObject errorResult = new JSONObject();
            errorResult.put("success", false);
            errorResult.put("error", e.getMessage());
            
            return errorResult;
        }
    }
 
    /**
     * 通用文字识别(文件对象)
     * @param imageFile 图片文件对象
     * @return 识别结果
     */
    public static JSONObject generalRecognize(File imageFile) {
        try {
            byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            
            OcrClient client = getClient();
            GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
            req.setImageBase64(base64Image);
            
            GeneralBasicOCRResponse resp = client.GeneralBasicOCR(req);
            
            log.info("腾讯云OCR通用文字识别成功,文件名: {}", imageFile.getName());
            
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("data", JSON.parseObject(AbstractModel.toJsonString(resp)));
            result.put("content", extractContentFromTencentResult(JSON.parseObject(AbstractModel.toJsonString(resp))));
            
            return result;
            
        } catch (Exception e) {
            log.error("腾讯云OCR通用文字识别失败: {}", e.getMessage(), e);
            
            JSONObject errorResult = new JSONObject();
            errorResult.put("success", false);
            errorResult.put("error", e.getMessage());
            
            return errorResult;
        }
    }
 
    /**
     * 通用文字识别(图片字节数组)
     * @param imageBytes 图片字节数组
     * @return 识别结果
     */
    public static JSONObject generalRecognize(byte[] imageBytes) {
        try {
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            
            OcrClient client = getClient();
            GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
            req.setImageBase64(base64Image);
            
            GeneralBasicOCRResponse resp = client.GeneralBasicOCR(req);
            
            log.info("腾讯云OCR通用文字识别成功,字节数组长度: {}", imageBytes.length);
            
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("data", JSON.parseObject(AbstractModel.toJsonString(resp)));
            result.put("content", extractContentFromTencentResult(JSON.parseObject(AbstractModel.toJsonString(resp))));
            
            return result;
            
        } catch (Exception e) {
            log.error("腾讯云OCR通用文字识别失败: {}", e.getMessage(), e);
            
            JSONObject errorResult = new JSONObject();
            errorResult.put("success", false);
            errorResult.put("error", e.getMessage());
            
            return errorResult;
        }
    }
 
    /**
     * 高精度文字识别
     * @param imagePath 图片路径
     * @return 识别结果
     */
    public static JSONObject accurateRecognize(String imagePath) {
        try {
            byte[] imageBytes = Files.readAllBytes(new File(imagePath).toPath());
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            
            OcrClient client = getClient();
            GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest();
            req.setImageBase64(base64Image);
            
            GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req);
            
            log.info("腾讯云OCR高精度文字识别成功,图片路径: {}", imagePath);
            
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("data", JSON.parseObject(AbstractModel.toJsonString(resp)));
            result.put("content", extractContentFromTencentResult(JSON.parseObject(AbstractModel.toJsonString(resp))));
            
            return result;
            
        } catch (Exception e) {
            log.error("腾讯云OCR高精度文字识别失败: {}", e.getMessage(), e);
            
            JSONObject errorResult = new JSONObject();
            errorResult.put("success", false);
            errorResult.put("error", e.getMessage());
            
            return errorResult;
        }
    }
 
    public static JSONObject handwritingRecognize(String imagePath, String[] itemNames) {
         try {
            byte[] imageBytes = Files.readAllBytes(new File(imagePath).toPath());
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            
            OcrClient client = getClient();
 
            ExtractDocMultiRequest req = new ExtractDocMultiRequest();
            req.setImageBase64(base64Image);
            // {"患者签名(手印)", "签字人身份证号码", "日期", "联系电话", "本人", "签字人与患者关系"}
            req.setItemNames(itemNames != null ? itemNames : new String[]{"患者姓名", "性别", "年龄", "身份证号", "诊断", "需支付转运费用", "行程", "开始时间", "结束时间", "家属签名"});
            req.setOutputLanguage("cn");
            req.setReturnFullText(true);
            req.setItemNamesShowMode(false);
            ExtractDocMultiResponse resp = client.ExtractDocMulti(req);
 
            log.info("腾讯云OCR手写体识别成功,图片路径: {}", imagePath);
            
            // 解析响应数据
            JSONObject responseData = JSON.parseObject(AbstractModel.toJsonString(resp));
            
            
            log.info("手写体识别提取到 {} 个字段", responseData.size());
            return responseData;
            
        } catch (Exception e) {
            log.error("腾讯云OCR手写体识别失败: {}", e.getMessage(), e);
            JSONObject errorResult = new JSONObject();
            errorResult.put("error", e.getMessage());
            return errorResult;
        }
    }
    /**
     * 手写体识别
     * @param imagePath 图片路径
     * @param itemNames 需要提取的字段名称数组
     * @return 识别结果 Map,key为AutoName的值,value为AutoContent的值
     */
    public static Map<String, String> handwritingRecognizeWith(String imagePath, String[] itemNames) {
        Map<String, String> resultMap = new HashMap<>();
        try {
           JSONObject responseData = handwritingRecognize(imagePath, itemNames);
            
            // 从StructuralList中提取数据
            if (responseData.containsKey("StructuralList") && responseData.getJSONArray("StructuralList") != null) {
                JSONArray structuralList = responseData.getJSONArray("StructuralList");
                for (int i = 0; i < structuralList.size(); i++) {
                    JSONObject structural = structuralList.getJSONObject(i);
                    if (structural.containsKey("Groups") && structural.getJSONArray("Groups") != null) {
                        JSONArray groups = structural.getJSONArray("Groups");
                        for (int j = 0; j < groups.size(); j++) {
                            JSONObject group = groups.getJSONObject(j);
                            if (group.containsKey("Lines") && group.getJSONArray("Lines") != null) {
                                JSONArray lines = group.getJSONArray("Lines");
                                for (int k = 0; k < lines.size(); k++) {
                                    JSONObject line = lines.getJSONObject(k);
                                    String autoName = null;
                                    String autoContent = null;
                                    
                                    // 提取AutoName
                                    if (line.containsKey("Key") && line.getJSONObject("Key") != null) {
                                        JSONObject key = line.getJSONObject("Key");
                                        if (key.containsKey("AutoName")) {
                                            autoName = key.getString("AutoName");
                                        }
                                    }
                                    
                                    // 提取AutoContent
                                    if (line.containsKey("Value") && line.getJSONObject("Value") != null) {
                                        JSONObject value = line.getJSONObject("Value");
                                        if (value.containsKey("AutoContent")) {
                                            autoContent = value.getString("AutoContent");
                                        }
                                    }
                                    
                                    // 将键值对放入结果Map
                                    if (autoName != null && autoContent != null) {
                                        resultMap.put(autoName, autoContent);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //将 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;
            
        } catch (Exception e) {
            log.error("腾讯云OCR手写体识别失败: {}", e.getMessage(), e);
            resultMap.put("error", e.getMessage());
            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 图片路径
     * @param cardSide 身份证正反面,"FRONT"表示正面,"BACK"表示反面
     * @return 识别结果
     */
    public static JSONObject idCardRecognize(String imagePath, String cardSide) {
        try {
            byte[] imageBytes = Files.readAllBytes(new File(imagePath).toPath());
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            
            OcrClient client = getClient();
            IDCardOCRRequest req = new IDCardOCRRequest();
            req.setImageBase64(base64Image);
            req.setCardSide(cardSide);
            
            IDCardOCRResponse resp = client.IDCardOCR(req);
            
            log.info("腾讯云OCR身份证识别成功,图片路径: {},方向: {}", imagePath, cardSide);
            
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("data", JSON.parseObject(AbstractModel.toJsonString(resp)));
            result.put("content", extractContentFromTencentResult(JSON.parseObject(AbstractModel.toJsonString(resp))));
            
            return result;
            
        } catch (Exception e) {
            log.error("腾讯云OCR身份证识别失败: {}", e.getMessage(), e);
            
            JSONObject errorResult = new JSONObject();
            errorResult.put("success", false);
            errorResult.put("error", e.getMessage());
            
            return errorResult;
        }
    }
 
    /**
     * 银行卡识别
     * @param imagePath 图片路径
     * @return 识别结果
     */
    public static JSONObject bankCardRecognize(String imagePath) {
        try {
            byte[] imageBytes = Files.readAllBytes(new File(imagePath).toPath());
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            
            OcrClient client = getClient();
            BankCardOCRRequest req = new BankCardOCRRequest();
            req.setImageBase64(base64Image);
            
            BankCardOCRResponse resp = client.BankCardOCR(req);
            
            log.info("腾讯云OCR银行卡识别成功,图片路径: {}", imagePath);
            
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("data", JSON.parseObject(AbstractModel.toJsonString(resp)));
            result.put("content", extractContentFromTencentResult(JSON.parseObject(AbstractModel.toJsonString(resp))));
            
            return result;
            
        } catch (Exception e) {
            log.error("腾讯云OCR银行卡识别失败: {}", e.getMessage(), e);
            
            JSONObject errorResult = new JSONObject();
            errorResult.put("success", false);
            errorResult.put("error", e.getMessage());
            
            return errorResult;
        }
    }
 
    /**
     * 从腾讯云OCR结果中提取纯文本内容
     * @param result OCR识别结果
     * @return 提取的文本内容
     */
    private static String extractContentFromTencentResult(JSONObject result) {
        StringBuilder content = new StringBuilder();
        
        // 处理通用OCR结果
        if (result.containsKey("TextDetections") && result.getJSONArray("TextDetections") != null) {
            JSONArray textDetections = result.getJSONArray("TextDetections");
            for (int i = 0; i < textDetections.size(); i++) {
                JSONObject detection = textDetections.getJSONObject(i);
                if (detection.containsKey("DetectedText")) {
                    content.append(detection.getString("DetectedText")).append("\n");
                }
            }
        }
        // 处理手写体OCR结果
        else if (result.containsKey("Items") && result.getJSONArray("Items") != null) {
            JSONArray items = result.getJSONArray("Items");
            for (int i = 0; i < items.size(); i++) {
                JSONObject item = items.getJSONObject(i);
                if (item.containsKey("Itemstring")) {
                    content.append(item.getString("Itemstring")).append("\n");
                }
            }
        }
        // 处理身份证OCR结果
        else if (result.containsKey("Name") || result.containsKey("Sex") || result.containsKey("Nation") ||
                 result.containsKey("Birth") || result.containsKey("Address") || result.containsKey("IdNum")) {
            if (result.containsKey("Name") && result.getString("Name") != null) {
                content.append("姓名: ").append(result.getString("Name")).append("\n");
            }
            if (result.containsKey("Sex") && result.getString("Sex") != null) {
                content.append("性别: ").append(result.getString("Sex")).append("\n");
            }
            if (result.containsKey("Nation") && result.getString("Nation") != null) {
                content.append("民族: ").append(result.getString("Nation")).append("\n");
            }
            if (result.containsKey("Birth") && result.getString("Birth") != null) {
                content.append("出生: ").append(result.getString("Birth")).append("\n");
            }
            if (result.containsKey("Address") && result.getString("Address") != null) {
                content.append("地址: ").append(result.getString("Address")).append("\n");
            }
            if (result.containsKey("IdNum") && result.getString("IdNum") != null) {
                content.append("身份证号: ").append(result.getString("IdNum")).append("\n");
            }
        }
        // 处理银行卡OCR结果
        else if (result.containsKey("CardNo") && result.getString("CardNo") != null) {
            content.append("银行卡号: ").append(result.getString("CardNo")).append("\n");
        }
        
        return content.toString().trim();
    }
 
    /**
     * 从识别结果中提取目标字段(金额、日期、备注等)
     * @param ocrResult OCR识别的原始结果
     * @return 提取后的目标字段
     */
    public static Map<String, String> extractTargetFields(JSONObject ocrResult) {
        Map<String, String> extracted = new HashMap<>();
 
        // 校验OCR结果是否有效
        if (!ocrResult.containsKey("success") || !ocrResult.getBooleanValue("success")) {
            extracted.put("error", ocrResult.getString("error"));
            return extracted;
        }
 
        // 获取识别的文字内容
        String content = ocrResult.getString("content");
        if (content == null || content.isEmpty()) {
            extracted.put("error", "OCR识别结果为空");
            return extracted;
        }
 
        // 在内容中查找特定关键词
        String[] lines = content.split("\n");
        for (String line : lines) {
            line = line.trim();
            
            // 查找金额相关信息
            if (line.contains("金额") || line.contains("合计") || line.contains("总计") || line.matches(".*\\d+\\.\\d{2}.*")) {
                if (!extracted.containsKey("totalAmount")) {
                    extracted.put("totalAmount", line);
                }
            }
            
            // 查找日期相关信息
            if (line.contains("日期") || line.matches(".*\\d{4}[-/年]\\d{1,2}[-/月]\\d{1,2}.*")) {
                if (!extracted.containsKey("date")) {
                    extracted.put("date", line);
                }
            }
            
            // 查找备注相关信息
            if (line.contains("备注") || line.contains("说明")) {
                if (!extracted.containsKey("remark")) {
                    extracted.put("remark", line);
                }
            }
        }
 
        // 如果没有找到特定字段,返回全文
        if (extracted.isEmpty()) {
            extracted.put("fullText", content);
        }
 
        return extracted;
    }
 
    /**
     * 手写体识别(使用默认字段)
     * @param imagePath 图片路径
     * @return 识别结果 Map,key为AutoName的值,value为AutoContent的值
     */
    public static Map<String, String> handwritingRecognize(String imagePath) {
        String[] defaultItemNames = {"患者姓名", "性别", "年龄", "身份证号", "诊断", "需支付转运费用", "行程", "开始时间", "结束时间", "家属签名"};
        return handwritingRecognizeWith(imagePath, defaultItemNames);
    }
}