此接口为每天100次免费,应对平时自己调试使用也够了~
亲测准确度还不错。
请求地址
https://api.itapi.cn/api/ocr/v2
请求参数
参数名 | 参数说明 |
---|---|
key | 用户请求密钥,可在 密钥管理页面 申请 |
data | 图片base64编码数据 或 网络图片URL |
请求结果参数说明
参数名 | 参数说明 |
---|---|
code | 状态码 |
msg | 状态信息 |
debug | 错误信息 |
exec_time | 系统执行时间 |
user_ip | 你的ip |
data | 请求结果数据集 |
data.text | 识别到的完整文本内容(注意换行符) |
data.text_list[] | 识别到的每行文本结果数组 |
POST同时支持图片url和base64数据提交,get仅支持图片url提交。
代码案例
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53/** * 图片转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; }
最后
以上就是可爱猫咪最近收集整理的关于分享一个免费的OCR图片文字识别接口的全部内容,更多相关分享一个免费内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复