This interface is free for 100 times a day, and it is enough to debug and use it by yourself~
The accuracy of the personal test is not bad.
request address
https://api.itapi.cn/api/ocr/v2
request parameters
parameter name | Parameter Description |
---|---|
key | The user requests a key, which can be applied on the key management page |
data | Image base64 encoded data or network image URL |
Request result parameter description
parameter name | Parameter Description |
---|---|
code | status code |
msg | status information |
debug | error message |
exec_time | system execution time |
user_ip | your ip |
data | request result dataset |
data.text | The complete text content recognized (note the line break) |
data.text_list[] | An array of text results for each line recognized |
POST supports both image url and base64 data submission, get only supports image url submission.
code example
/**
* 图片转Base64
*/
public static String getImageBase() throws Exception {
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream("本地文件路径");
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
/**
* 向指定 URL 发送POST方法的请求
*/
public static String sendPost() throws Exception {
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("key", "用户请求秘钥");
paramMap.put("data", getImageBase());
URL url = new URL("https://api.itapi.cn/api/ocr/v2");
StringBuilder postData = new StringBuilder();
//接口不支持json传参,处理参数
for (Map.Entry<String,String> param : paramMap.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
sb.append((char)c);
String response = sb.toString();
return response;
}