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;
/**
* 百度AI接口代理控制器
*/
@RestController
@RequestMapping("/api/baidu")
public class BaiduAIController {
@Autowired
private BaiduAIConfig baiduAIConfig;
@Autowired
private RestTemplate restTemplate;
/**
* 获取百度AI的access token
*/
@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());
}
}
/**
* 调用百度OCR识别接口
*/
@PostMapping("/ocr")
public AjaxResult ocrRecognize(@RequestBody Map<String, Object> requestData) {
try {
// 获取token
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");
// 调用OCR接口
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());
}
}
}