public static String documentOCR(String filepath){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String result = "";
try {
File file = new File(filepath);
// url 够买合合科技OCR文字识别码----也可加我微信所要------ 微信号: RoninStreet
URL url = new URL("");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("transfer-encoding", "chunked");
OutputStream out = con.getOutputStream();
FileInputStream inputStream = new FileInputStream(file);
byte[] data = new byte[2048];
int len = 0;
int sum = 0;
while ((len = inputStream.read(data))!= -1) {
out.write(data, 0, len);
sum = len + sum;
}
//System.out.println("upload size="+sum);
out.flush();
inputStream.close();
out.close();
int code = con.getResponseCode();
//System.out.println("code="+code+ " url="+url);
if (code==200) {
InputStream inputStream2 = con.getInputStream();
while ((len = inputStream2.read(data))!= -1) {
bos.write(data, 0, len);
}
String a = bos.toString();
result = a.substring(a.toString().indexOf("[")+1,a.toString().lastIndexOf("]"));
//System.out.println("result ="+bos.toString());
inputStream2.close();
bos.close();
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
java代码实现OCR---第三方接口
最新推荐文章于 2024-07-24 11:00:44 发布