luodangjia
2025-01-21 d0565d28bafce1688c2423e43a94ea43923b664a
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
package com.ruoyi.system.controller;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.UrlDownloader;
import com.ruoyi.common.core.utils.orc.OcrUtils;
import com.ruoyi.system.api.model.BusinessLicense;
import com.ruoyi.system.api.model.IdCard;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.InputStream;
import java.net.URLEncoder;
 
@RestController
@RequestMapping("/common")
@Tag(name = "通用接口")
public class CommonController {
 
    /**
     * ocr提取身份证信息
     */
    @GetMapping("/idCard")
    @Operation(summary = "ocr提取身份证信息", description = "ocr提取身份证信息")
    public R<IdCard> idCard(@Parameter(name = "url", description = "图片地址") String url) {
        InputStream inputStream;
        try {
            // 找到最后一个斜杠的位置
            int lastSlashIndex = url.lastIndexOf("/");
            // 从斜杠后提取文件名
            String baseUrl = url.substring(0, lastSlashIndex);
            String fileName = url.substring(lastSlashIndex + 1);
            String encode = URLEncoder.encode(fileName, "UTF-8");
            url = baseUrl + "/" + encode;
            inputStream = UrlDownloader.downloadAsStream(url);
            String jsonStr = OcrUtils.idCard(inputStream,"IdCard");
            if (StringUtils.isEmpty(jsonStr)){
                return R.fail();
            }
 
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            Integer statusCode = jsonObject.getInteger("statusCode");
            if (statusCode != 200) {
                return R.fail("识别失败");
            }
            JSONObject body = jsonObject.getJSONObject("body");
            JSONObject data = body.getJSONObject("Data");
            JSONArray subImages = data.getJSONArray("SubImages");
            if (subImages != null && !subImages.isEmpty()){
                JSONObject subImage = subImages.getJSONObject(0);
                JSONObject kvInfo = subImage.getJSONObject("KvInfo");
                IdCard idCard = kvInfo.getObject("Data", IdCard.class);
                return R.ok(idCard);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.fail();
    }
 
    /**
     * ocr提取营业执照信息
     */
    @GetMapping("/businessLicense")
    @Operation(summary = "ocr提取营业执照信息", description = "ocr提取营业执照信息")
    public R<BusinessLicense> businessLicense(@Parameter(name = "url", description = "图片地址") String url) {
        InputStream inputStream;
        try {
            inputStream = UrlDownloader.downloadAsStream(url);
            String jsonStr = OcrUtils.idCard(inputStream,"BusinessLicense");
            if (StringUtils.isEmpty(jsonStr)){
                return R.fail();
            }
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            Integer statusCode = jsonObject.getInteger("statusCode");
            if (statusCode != 200) {
                return R.fail("识别失败");
            }
            JSONObject body = jsonObject.getJSONObject("body");
            JSONObject data = body.getJSONObject("Data");
            JSONArray subImages = data.getJSONArray("SubImages");
            if (subImages != null && !subImages.isEmpty()){
                JSONObject subImage = subImages.getJSONObject(0);
                JSONObject kvInfo = subImage.getJSONObject("KvInfo");
                BusinessLicense businessLicense = kvInfo.getObject("Data", BusinessLicense.class);
                return R.ok(businessLicense);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.fail();
    }
}