java调用百度接口识别图片文字,最全最完整方法
时间: 2023-12-04 09:02:55 浏览: 141
基于Java 调用百度API的图片文字识别(支持中文,英文和中英文混合)
以下是一个完整的Java示,演示如何使用Apache HttpClient库来调用百度OCR接口识别图片文字:
```java
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class BaiduOCR {
public static void main(String[] args) throws IOException {
// 设置API Key和Secret Key
String apiKey = "your_api_key";
String secretKey = "your_secret_key";
// 获取访问令牌
String accessToken = getAccessToken(apiKey, secretKey);
// 图片路径
String imagePath = "path_to_your_image";
// 将图片转为Base64编码
String imageBase64 = getImageBase64(imagePath);
// 调用百度OCR接口
String result = callBaiduOCR(imageBase64, accessToken);
System.out.println(result);
}
// 获取访问令牌
private static String getAccessToken(String apiKey, String secretKey) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://aip.baidubce.com/oauth/2.0/token");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("grant_type", "client_credentials"));
params.add(new BasicNameValuePair("client_id", apiKey));
params.add(new BasicNameValuePair("client_secret", secretKey));
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
httpClient.close();
return result.split("\"")[3];
}
// 将图片转为Base64编码
private static String getImageBase64(String imagePath) throws IOException {
File file = new File(imagePath);
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputStream.read(buffer);
inputStream.close();
return Base64.encodeBase64String(buffer);
}
// 调用百度OCR接口
private static String callBaiduOCR(String imageBase64, String accessToken) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("image", imageBase64));
params.add(new BasicNameValuePair("access_token", accessToken));
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
httpClient.close();
return result;
}
}
```
在上述代码中,你需要将`your_api_key`和`your_secret_key`替换为你自己的百度API Key和Secret Key,将`path_to_your_image`替换为你要识别的图片的路径。
请确保已经添加Apache HttpClient和Apache Commons Codec的依赖项到你的项目中。
这个示例代码会将图片转为Base64编码,并调用百度OCR接口来识别图片中的文字。它会返回一个包含识别结果的JSON字符串。
希望这能满足你的需求!如果有任何其他问题,请随时提问。
阅读全文