告别复杂配置!Spring Boot优雅集成百度OCR的终极方案

1. 准备工作

1.1 注册百度AI开放平台
  1. 访问百度AI开放平台

  2. 注册账号并登录

  3. 进入控制台 → 文字识别 → 创建应用

  4. 记录下API KeySecret Key


2. 项目配置

2.1 添加依赖 (pom.xml)
XML 复制代码
<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Configuration Properties 处理器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    
    <!-- Apache HttpClient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
    
    <!-- JSON 处理 -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>
2.2 配置YAML (application.yml)
TypeScript 复制代码
baidu:
  ocr:
    api-key: "your_api_key"         # 替换为你的API Key
    secret-key: "your_secret_key"   # 替换为你的Secret Key
    access-token-url: "https://aip.baidubce.com/oauth/2.0/token"
    general-basic-url: "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"

3. 配置参数封装

3.1 创建配置类 (BaiduOcrProperties.java)
TypeScript 复制代码
@Data
@Builder
@Component
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "baidu.ocr")
public class BaiduOcrProperties {
    private String apiKey;
    private String secretKey;
    private String accessTokenUrl;
    private String generalBasicUrl;
}

4. OCR服务实现

4.1 工具类:获取Access Token (AccessTokenUtil.java)
TypeScript 复制代码
@Component
public class AccessTokenUtil {

    private final BaiduOcrProperties ocrProperties;

    @Autowired
    public AccessTokenUtil(BaiduOcrProperties ocrProperties) {
        this.ocrProperties = ocrProperties;
    }

    public String getAccessToken() throws IOException {
        String url = String.format("%s?grant_type=client_credentials&client_id=%s&client_secret=%s",
                ocrProperties.getAccessTokenUrl(),
                ocrProperties.getApiKey(),
                ocrProperties.getSecretKey());

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                HttpEntity entity = response.getEntity();
                String json = EntityUtils.toString(entity);
                // 解析JSON获取access_token
                return json.split("\"access_token\":\"")[1].split("\"")[0];
            }
        }
    }
}
4.2 OCR服务类 (BaiduOcrService.java)
TypeScript 复制代码
@Service
public class BaiduOcrService {

    private final AccessTokenUtil accessTokenUtil;
    private final BaiduOcrProperties ocrProperties;

    @Autowired
    public BaiduOcrService(AccessTokenUtil accessTokenUtil, BaiduOcrProperties ocrProperties) {
        this.accessTokenUtil = accessTokenUtil;
        this.ocrProperties = ocrProperties;
    }

    public String recognizeText(MultipartFile file) throws IOException {
        // 1. 获取Access Token
        String accessToken = accessTokenUtil.getAccessToken();

        // 2. 将图片转换为Base64
        String imageBase64 = Base64.getEncoder().encodeToString(file.getBytes());

        // 3. 构建请求参数
        Map<String, String> params = new HashMap<>();
        params.put("image", imageBase64);
        params.put("language_type", "CHN_ENG"); // 中英文混合

        // 4. 发送OCR请求
        String url = ocrProperties.getGeneralBasicUrl() + "?access_token=" + accessToken;
        return postFormData(url, params);
    }

    private String postFormData(String url, Map<String, String> params) throws IOException {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

            // 构建表单参数
            StringBuilder formData = new StringBuilder();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (!formData.isEmpty()) formData.append("&");
                formData.append(entry.getKey()).append("=").append(entry.getValue());
            }

            httpPost.setEntity(new StringEntity(formData.toString()));

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            }
        }
    }
}

5. 控制器层

5.1 图片识别接口 (OcrController.java)
TypeScript 复制代码
@RestController
public class OcrController {

    private final BaiduOcrService ocrService;

    @Autowired
    public OcrController(BaiduOcrService ocrService) {
        this.ocrService = ocrService;
    }

    @PostMapping("/ocr")
    public String recognizeImage(@RequestParam("image") MultipartFile file) {
        try {
            return ocrService.recognizeText(file);
        } catch (Exception e) {
            return "{\"error\": \"" + e.getMessage() + "\"}";
        }
    }
}

6. 关键点说明

  1. 配置封装

    使用@ConfigurationProperties将YAML中的配置自动绑定到Java对象

  2. Access Token获取

    百度OCR需要先获取临时Access Token(有效期30天)

  3. 图片处理

    将上传的图片转换为Base64编码

  4. 错误处理

    实际生产中需添加更完善的异常处理机制

  5. 性能优化

    • 缓存Access Token(避免每次请求都获取)

    • 使用连接池管理HTTP客户端

    • 限制上传图片大小(在application.yml中配置spring.servlet.multipart.max-file-size


完整项目结构

复制代码
src/main/java
├── com/example/demo
│   ├── config
│   │   └── BaiduOcrProperties.java
│   ├── controller
│   │   └── OcrController.java
│   ├── service
│   │   ├── BaiduOcrService.java
│   │   └── AccessTokenUtil.java
│   └── DemoApplication.java
resources
└── application.yml

通过以上步骤,你已完成了一个可扩展的Spring Boot百度OCR集成方案。实际部署时请将YAML中的API密钥替换为你的实际密钥。

相关推荐
moxiaoran575313 分钟前
Springboot+MongoDB简单使用示例
spring boot·mongodb·spring
小鱼人爱编程1 小时前
Java基石--反射让你直捣黄龙
前端·spring boot·后端
双力臂40412 小时前
Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
java·spring boot·后端·单元测试
itLaity13 小时前
基于Kafka实现简单的延时队列
spring boot·分布式·kafka
midsummer_woo14 小时前
基于spring boot的医院挂号就诊系统(源码+论文)
java·spring boot·后端
SEO_juper15 小时前
企业级 AI 工具选型报告:9 个技术平台的 ROI 对比与部署策略
人工智能·搜索引擎·百度·llm·工具·geo·数字营销
fouryears_2341719 小时前
什么是JSON,如何与Java对象转化
java·spring boot·spring·json
wuk99820 小时前
互联网应用主流框架整合 Spring Boot开发
java·spring boot·后端
风象南21 小时前
SpringBoot实现Serverless:手撸一个本地函数计算引擎
spring boot·serverless