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密鑰替換為你的實際密鑰。