java实现百度云OCR文字识别
引入jar
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.6.0</version>
</dependency>
工具类
public class test {
private static final String APP_ID = "你的app id";
private static final String API_KEY = "你的api key";
private static final String SECRET_KEY = "你的secret key";
private static AipOcr getAipClient() {
return getAipClient(API_KEY, SECRET_KEY);
}
public static AipOcr getAipClient(String apiKey, String secretKey) {
AipOcr client = new AipOcr(APP_ID, apiKey, secretKey);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
return client;
}
public static String result(AipOcr client) {
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<>();
options.put("language_type", "CHN_ENG");
options.put("detect_direction", "true");
options.put("detect_language", "true");
JSONObject res = client.basicGeneralUrl(
"图片路径", options);
return res.toString(2);
}
}