申请使用,个人完成认证可获得一定的免费使用场景,用来测试或者简单的场景足够了
创建应用,获得APP_ID、API_KEY和SECRET_KEY
import java.util.*;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
public class OCRUtils {
//设置APPID/AK/SK
public static final String APP_ID = "APP_ID ";
public static final String API_KEY = "API_KEY ";
public static final String SECRET_KEY = "SECRET_KEY ";
private static volatile AipOcr client = null;
final static Object lock = new Object();
public static AipOcr getClient(String appId,String apiKey,String secretKey){
if (client == null){
synchronized (lock){
if (client == null){
client = new AipOcr(appId, apiKey, secretKey);
}
}
}
return client;
}
public static void main(String[] args) {
// 初始化一个AipOcr
AipOcr client = getClient(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 调用接口
String path = "C:\\Users\\admin\\Pictures\\a.png";
JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
System.out.println(res.toString(2));
}
}
测试结果