一个基于 Spring Boot 的实现,用于代理百度 AI 的 OCR 接口 BaiduAIController.java BaiduAIConfig.java 在 application.yml 或 application.properties 中添加配置:application.yml 同时,需要在Spring Boot应用中配置RestTemplate:ApplicationConfig.java
BaiduAIController.java
package com. ranfeng. controller ; import com. ranfeng. common. core. domain. AjaxResult ;
import com. ranfeng. config. BaiduAIConfig ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. http. HttpEntity ;
import org. springframework. http. HttpHeaders ;
import org. springframework. http. MediaType ;
import org. springframework. util. LinkedMultiValueMap ;
import org. springframework. util. MultiValueMap ;
import org. springframework. web. bind. annotation. * ;
import org. springframework. web. client. RestTemplate ; import java. util. HashMap ;
import java. util. Map ;
@RestController
@RequestMapping ( "/api/baidu" )
public class BaiduAIController { @Autowired private BaiduAIConfig baiduAIConfig; @Autowired private RestTemplate restTemplate; @GetMapping ( "/token" ) public AjaxResult getToken ( ) { try { String url = "https://aip.baidubce.com/oauth/2.0/token" ; MultiValueMap < String , String > params = new LinkedMultiValueMap < > ( ) ; params. add ( "grant_type" , "client_credentials" ) ; params. add ( "client_id" , baiduAIConfig. getApiKey ( ) ) ; params. add ( "client_secret" , baiduAIConfig. getSecretKey ( ) ) ; HttpHeaders headers = new HttpHeaders ( ) ; headers. setContentType ( MediaType . APPLICATION_FORM_URLENCODED) ; HttpEntity < MultiValueMap < String , String > > requestEntity = new HttpEntity < > ( params, headers) ; Map < String , Object > response = restTemplate. postForObject ( url, requestEntity, Map . class ) ; return AjaxResult . success ( response) ; } catch ( Exception e) { return AjaxResult . error ( "获取百度AI Token失败: " + e. getMessage ( ) ) ; } } @PostMapping ( "/ocr" ) public AjaxResult ocrRecognize ( @RequestBody Map < String , Object > requestData) { try { String tokenUrl = "https://aip.baidubce.com/oauth/2.0/token" ; MultiValueMap < String , String > tokenParams = new LinkedMultiValueMap < > ( ) ; tokenParams. add ( "grant_type" , "client_credentials" ) ; tokenParams. add ( "client_id" , baiduAIConfig. getApiKey ( ) ) ; tokenParams. add ( "client_secret" , baiduAIConfig. getSecretKey ( ) ) ; HttpHeaders tokenHeaders = new HttpHeaders ( ) ; tokenHeaders. setContentType ( MediaType . APPLICATION_FORM_URLENCODED) ; HttpEntity < MultiValueMap < String , String > > tokenRequestEntity = new HttpEntity < > ( tokenParams, tokenHeaders) ; Map < String , Object > tokenResponse = restTemplate. postForObject ( tokenUrl, tokenRequestEntity, Map . class ) ; String accessToken = ( String ) tokenResponse. get ( "access_token" ) ; String ocrUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + accessToken; MultiValueMap < String , String > ocrParams = new LinkedMultiValueMap < > ( ) ; ocrParams. add ( "image" , ( String ) requestData. get ( "image" ) ) ; ocrParams. add ( "language_type" , ( String ) requestData. get ( "language_type" ) ) ; ocrParams. add ( "detect_direction" , ( String ) requestData. get ( "detect_direction" ) ) ; ocrParams. add ( "probability" , ( String ) requestData. get ( "probability" ) ) ; HttpHeaders ocrHeaders = new HttpHeaders ( ) ; ocrHeaders. setContentType ( MediaType . APPLICATION_FORM_URLENCODED) ; HttpEntity < MultiValueMap < String , String > > ocrRequestEntity = new HttpEntity < > ( ocrParams, ocrHeaders) ; Map < String , Object > ocrResponse = restTemplate. postForObject ( ocrUrl, ocrRequestEntity, Map . class ) ; return AjaxResult . success ( ocrResponse) ; } catch ( Exception e) { return AjaxResult . error ( "OCR识别失败: " + e. getMessage ( ) ) ; } }
}
BaiduAIConfig.java
package com. ranfeng. config ; import org. springframework. boot. context. properties. ConfigurationProperties ;
import org. springframework. context. annotation. Configuration ;
@Configuration
@ConfigurationProperties ( prefix = "baidu.ai" )
public class BaiduAIConfig { private String apiKey; private String secretKey; public String getApiKey ( ) { return apiKey; } public void setApiKey ( String apiKey) { this . apiKey = apiKey; } public String getSecretKey ( ) { return secretKey; } public void setSecretKey ( String secretKey) { this . secretKey = secretKey; }
}
在 application.yml 或 application.properties 中添加配置:application.yml
baidu : ai : api-key : xxxxxsecret-key : xxxxxx
同时,需要在Spring Boot应用中配置RestTemplate:ApplicationConfig.java
package com. ranfeng. config ; import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. web. client. RestTemplate ; @Configuration
public class ApplicationConfig { @Bean public RestTemplate restTemplate ( ) { return new RestTemplate ( ) ; }
}