整體的代碼在這裡就不貼出來了,阿裡雲圖檔文字識别api中有詳細代碼。
阿裡雲圖檔文字識别api中的代碼是識别本地圖檔,如果是伺服器或是ftp的可能需要做一些改動。
改動一:
阿裡雲api:
修改後:
URL url = new URL(imgFile);
URLConnection con = url.openConnection();
InputStream finputstream = con.getInputStream();
byte[] content = UploadUtil.readInputStream(finputstream);
将流轉換為byte資料的方法:
/**
* 将流轉換為位元組
* @param is
* @return
*/
public static byte[] readInputStream(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
try {
while ((length = is.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
baos.flush();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = baos.toByteArray();
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
其他的完全按照api來就可以了。