刘光辉
11 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.base.controller;
 
import com.alibaba.fastjson.JSONObject;
import feign.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.model.ocr.OcrForm;
import jnpf.base.model.ocr.OcrModel;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.FileTypeConstant;
import jnpf.constant.MsgCode;
import jnpf.exception.DataException;
import jnpf.file.FileUploadApi;
import jnpf.model.upload.UploadFileModel;
import jnpf.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
 
/**
 * ocr解析
 *
 * @author JNPF开发平台组
 * @version v6.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2025/7/29 16:58:47
 */
@Slf4j
@Tag(name = "ocr解析", description = "ocr")
@RestController
@RequiredArgsConstructor
@RequestMapping("/ocr")
public class OcrController {
 
    private final ConfigValueUtil configValueUtil;
    private final FileUploadApi fileUploadApi;
 
    @Operation(summary = "ocr解析")
    @PostMapping
    public ActionResult<OcrModel> form(@RequestBody OcrForm form) {
        String url = form.getUrl();
        String substring = url.substring(url.lastIndexOf("/") + 1);
        //下载到本地
        Response response = fileUploadApi.downFile(new UploadFileModel(FileTypeConstant.ANNEXPIC, substring));
        try (InputStream ins = response.body().asInputStream()) {
            byte[] byteArray = IOUtils.toByteArray(ins);
            String str = Base64.getEncoder().encodeToString(byteArray);
            JSONObject body = new JSONObject();
            List<String> list = new ArrayList<>();
            list.add(str);
            body.put("images", list);
            String ocrUrl = configValueUtil.getOcrUrl();
            if (StringUtil.isEmpty(ocrUrl)) {
                return ActionResult.fail(MsgCode.SYS183.get());
            }
            if (ocrUrl.endsWith("/")) {
                ocrUrl = ocrUrl.substring(0, ocrUrl.length() - 1);
            }
            OcrModel ocrModel = OcrModel.get(form.getType());
            ocrModel.extract(ocrUrl, body);
            return ActionResult.success(ocrModel);
        } catch (DataException e) {
            log.error(e.getMessage(), e);
            return ActionResult.fail(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return ActionResult.fail(MsgCode.FA104.get());
    }
}