告别复杂配置!Spring Boot优雅集成百度OCR的终极方案
1. 准备工作
1.1 注册百度AI开放平台
-
访问百度AI开放平台
-
注册账号并登录
-
进入控制台 → 文字识别 → 创建应用
-
记录下
API Key
和Secret Key
2. 项目配置
2.1 添加依赖 (pom.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
)
baidu:ocr:api-key: "your_api_key" # 替换为你的API Keysecret-key: "your_secret_key" # 替换为你的Secret Keyaccess-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
)
@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
)
@Component
public class AccessTokenUtil {private final BaiduOcrProperties ocrProperties;@Autowiredpublic 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_tokenreturn json.split("\"access_token\":\"")[1].split("\"")[0];}}}
}
4.2 OCR服务类 (BaiduOcrService.java
)
@Service
public class BaiduOcrService {private final AccessTokenUtil accessTokenUtil;private final BaiduOcrProperties ocrProperties;@Autowiredpublic BaiduOcrService(AccessTokenUtil accessTokenUtil, BaiduOcrProperties ocrProperties) {this.accessTokenUtil = accessTokenUtil;this.ocrProperties = ocrProperties;}public String recognizeText(MultipartFile file) throws IOException {// 1. 获取Access TokenString accessToken = accessTokenUtil.getAccessToken();// 2. 将图片转换为Base64String 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
)
@RestController
public class OcrController {private final BaiduOcrService ocrService;@Autowiredpublic 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. 关键点说明
-
配置封装:
使用@ConfigurationProperties
将YAML中的配置自动绑定到Java对象 -
Access Token获取:
百度OCR需要先获取临时Access Token(有效期30天) -
图片处理:
将上传的图片转换为Base64编码 -
错误处理:
实际生产中需添加更完善的异常处理机制 -
性能优化:
-
缓存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密钥替换为你的实际密钥。