[Java] 纯文本查看 复制代码package baiduocr;
import java.util.HashMap;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
public class ExcelHandler2 {
//百度申请的APP_ID,API_KEY,SECRET_KEY
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 InterruptedException {
//生成一个百度OCR Client
AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
//设置请求的请求参数(此处为空)
HashMap options = new HashMap();
String image = "test.jpg";
//放入请求解析的图片和map集合
JSONObject res = client.tableRecognitionAsync(image, options);
System.out.println(res.toString(2));
//从返回的json结果中解析出requestId用于后面获取结果
String requestId = (String)new JSONObject(res.toString(2)).getJSONArray("result").getJSONObject(0).get("request_id");
System.out.println(requestId);
int percent = 0;
while(percent!=100){
//获取json格式的结果,请求的参数为空
JSONObject res2 = client.tableResultGet(requestId, new HashMap());
JSONObject object = res2.getJSONObject("result");
percent = object.getInt("percent");
System.out.println("percent:"+percent+"%");
Thread.sleep(500);
if(percent==100){
//解析出结果中的地址,复制到浏览器下载即可
System.out.println(object.getString("result_data"));
}
}
}
}