【方案三】JAVA中使用ocr(Umi-OCR)

目录

前言:

需求:

代码:

难点:

参考文档:


前言:

前两个方案都是自己做着玩儿的,实际运用到上线项目是要收费的,该方案使用的是免费开源的工具,就算运用到商业项目也不会侵权,建议使用这个方案。

需求:

上传图片,识别出内容

代码:

java 复制代码
@Value("${ocr.url}")
//"http://121.229.10.211:32009/umi/api/ocr"
private String ocrUrl;

@PostMapping("/ocr")
@ApiOperationSupport(order = 3)
@ApiOperation(value = "识别图像", notes = "上传图像")
public R<FarmerApplyEntity> ocr(@RequestBody MultipartFile file) throws IOException, ParseException {
        // 创建临时文件
        File tfile = File.createTempFile("tempfile", file.getOriginalFilename());
        // 写入数据
        file.transferTo(tfile);

        // 构造请求体
        JSONObject inputObject = new JSONObject();
        String base64 = Base64.encode(tfile);
        inputObject.put("base64", base64.replace("data:image/png;base64,", ""));
        JSONObject options = new JSONObject();
        options.put("ocr.language", "models/config_chinese.txt");
        options.put("ocr.cls", true);
        options.put("ocr.limit_side_len", 4320);
        options.put("tbpu.parser", "multi_none");
        options.put("data.format", "txt");
        inputObject.put("options", options);

        String jsonInputString = inputObject.toString();
        // 发送 POST 请求
        HttpResponse response = HttpUtil.createPost(ocrUrl)
                .contentType("application/json") // 设置请求头,指定内容类型为 JSON
                .body(jsonInputString) // 设置请求体
                .execute();
        System.out.printf(JSON.parseObject(response.body()).toJSONString());
        JSONObject retObject = JSON.parseObject(response.body());
        JSONArray data = retObject.getJSONArray("data");
        // 将JSONArray里的每一个object里的text串联起来
        // 用于存储串联结果的StringBuilder
        StringBuilder concatenatedText = new StringBuilder();

        // 遍历JSONArray
        for (int i = 0; i < data.size(); i++) {
            // 获取每个JSONObject
            JSONObject jsonObject = data.getJSONObject(i);
            // 获取text属性
            String text = jsonObject.getString("text");
            // 将text添加到StringBuilder
            concatenatedText.append(text);
        }
        tfile.delete();

        return R.data(contractService.boxOcrResult(concatenatedText.toString()));
    }

难点:

需要自己启动一个ocr的服务,上面代码是调用这个服务进行识别,ocrUrl就是启动后的服务访问地址,下载发行版启动服务(仅支持win7以上)

如果系统是linux则需要使用docker 构建镜像启动服务

参考文档:

Umi-OCR: Umi-OCR 是一款免费、开源、可批量的离线 OCR 软件,基于 PaddleOCR,适用于 Windows10/11 平台

相关推荐
豆瓣鸡13 分钟前
算法日记 - Day3
java·开发语言·算法
萧瑟余晖29 分钟前
Java深入解析篇九之NIO详解
java·网络·nio
The Chosen One9851 小时前
高进度算法模板速记(待完善)
java·前端·算法
极光代码工作室3 小时前
基于SpringBoot的课程预约系统
java·springboot·web开发·后端开发
Leighteen3 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言
名字还没想好☜4 小时前
Go 的 time.After 在 select 循环里内存泄漏:定时器堆积原理与 timer.Reset 正确姿势
java·数据库·golang·go·goroutine
圆山猫4 小时前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v
caishenzhibiao5 小时前
期货先行者主图 同花顺期货通指标
java·c语言·c#
史呆芬5 小时前
分布式事务实战:微服务跨服务数据一致性解决方案
java·后端·spring cloud
IT界的老黄牛5 小时前
限流命中后该怎么办:直接丢、阻塞等待、延迟重投三种姿势的取舍
java·rocketmq·redisson·令牌桶·削峰·分布式限流