告别复杂配置!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密钥替换为你的实际密钥。

相关推荐
2301_7930868711 分钟前
SpringCloud 02 服务治理 Nacos
java·spring boot·spring cloud
MacroZheng1 小时前
还在用WebSocket实现即时通讯?试试MQTT吧,真香!
java·spring boot·后端
midsummer_woo2 小时前
基于springboot的IT技术交流和分享平台的设计与实现(源码+论文)
java·spring boot·后端
别惹CC3 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
柯南二号4 小时前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
javachen__5 小时前
SpringBoot整合P6Spy实现全链路SQL监控
spring boot·后端·sql
IT毕设实战小研12 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
一只爱撸猫的程序猿13 小时前
使用Spring AI配合MCP(Model Context Protocol)构建一个"智能代码审查助手"
spring boot·aigc·ai编程
甄超锋13 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
武昌库里写JAVA15 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习