<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
package com.yale.util;
import com.alibaba.fastjson.JSON;
import com.baidu.aip.ocr.AipOcr;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
public class Sample {
//设置APPID/AK/SK
public static final String APP_ID = "***";//请自行申请
public static final String API_KEY = "***";//请自行申请
public static final String SECRET_KEY = "***";//请自行申请
public static void main(String[] args) throws JsonProcessingException {
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
//client.setConnectionTimeoutInMillis(2000);
//client.setSocketTimeoutInMillis(60000);
// 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
//client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
// 可选:设置log4j日志输出格式,若不设置,则使用默认配置
// 也可以直接通过jvm启动参数设置此环境变量
//System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
// 调用接口
String image = "https://aip.bdstatic.com/portal/dist/1555683028116/ai_images/technology/ocr-cards/vehicle/demo-card-1.jpg";//行驶证照片路径
JSONObject result = client.webImageUrl(image, null);
//String path = "C:\\Users\\1\\Desktop\\demo-card-1.jpg";//demo图片可从上面url下载
//JSONObject result = client.vehicleLicense(path, null);
System.out.println("******************************* 美化json输出 ************************************");
//org.json.JSONObject不能直接格式化输出,需中转一下
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(result.toString());
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject));
System.out.println("********************************************************************************");
/******************************** 美化json输出 ************************************
{
"log_id" : 710418202553610567,
"words_result" : {
"车辆识别代号" : {
"words" : "HHGHH555778802669"
},
"住址" : {
"words" : "北京市石景山区"
},
"品牌型号" : {
"words" : "讴歌牌GHA7150JAD5B"
},
"发证日期" : {
"words" : "20180614"
},
"车辆类型" : {
"words" : "小型轿车"
},
"所有人" : {
"words" : "王京"
},
"使用性质" : {
"words" : "非营运"
},
"发动机号码" : {
"words" : "1122444"
},
"号牌号码" : {
"words" : "京AA5599"
},
"注册日期" : {
"words" : "20180614"
}
},
"words_result_num" : 10
}
*********************************************************************************/
}
}