百度网络图片文字识别demo
百度文档:https://ai.baidu.com/ai-doc/OCR/Sk3h7xyad
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.baidu.aip.ocr.AipOcr;
public class Sample {
//设置APPID/AK/SK
public static final String APP_ID = "...";
public static final String API_KEY = "...";
public static final String SECRET_KEY = "...";
static String fileName="7";
public static void main(String[] args) throws IOException {
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 调用接口
/**
* 图片地址
*/
String path = "E:\\test\\jvm\\"+fileName+".png";
JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
Res r=JSON.parseObject(res.toString(),Res.class);
List<Words> l=r.getWords_result();
StringBuilder test=new StringBuilder();
for(Words w:l) {
test.append(w.getWords());
}
BufferedInputStream bis=new BufferedInputStream(new ByteArrayInputStream(test.toString().getBytes()));
/**
* 将文字输出到本地文件
*/
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File("E:\\test\\jvm\\"+fileName+".txt")));
int length;
byte b[]=new byte[1024];
while((length=bis.read(b))!=-1) {
bos.write(b, 0, length);
}
bos.flush();
bos.close();
bis.close();
}
}
fixed
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。