【方案三】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 平台

相关推荐
Hi202402177 分钟前
支持OCR和AI解释的Web PDF阅读器:解决大文档阅读难题
pdf·flask·llm·ocr·阅读器
kevin 116 分钟前
如何识别发票特殊版式?OCR大模型如何颠覆传统并保证准确率?
ocr
Seven9717 分钟前
Spring AI 框架中如何集成 MCP?
java
开往198228 分钟前
spring boot整合mybatis
java·spring boot·mybatis
北京_宏哥33 分钟前
《刚刚问世》系列初窥篇-Java+Playwright自动化测试-27- 操作单选和多选按钮 - 上篇(详细教程)
java·前端·面试
回家路上绕了弯1 小时前
Java双亲委派机制:从原理到实践的全面解析
java·后端
努力的小郑1 小时前
亿级流量下的生死抉择:Apache BeanUtils vs MapStruct性能差距32倍!架构师选型指南
java·spring·apache
努力的小郑1 小时前
BeanUtils拷贝大对决:Spring与Apache Commons的差异与妙用
java·spring·apache
求知摆渡1 小时前
Spring Boot 3.5 + Spring Cloud Stream:邮件发送与幂等实战
java·spring boot·spring cloud
用户40078422112601 小时前
苍穹外卖实现员工账号启用禁用
java