对接有道OCR

2022-09-10

java

挑选朋友要慎重,更换朋友更要慎重——富兰克林

有道智云文档

代码如下:

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
54
55
56
57
58
59
60
61
62
63
64
package com.ruben;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.tokenizer.Word;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.setting.Setting;

import javax.sound.sampled.Line;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* YouDaoOcr
*
* @author VampireAchao
* @since 2022/9/10
*/
public class YouDaoOcr {

/**
* 地址与鉴权信息,配置文件中
*/
public static final Setting SETTING = new Setting("app.setting");


public static void main(String[] args) {
recognize(Base64.encode(HttpUtil.downloadBytes("https://VampireAchao.github.io/imgs/preview/3051_3.jpg")));
}

public static void recognize(String base64Image) {
String salt = UUID.fastUUID().toString();
String curTime = String.valueOf(System.currentTimeMillis() / 1000);
HttpRequest request = HttpUtil.createPost(SETTING.get("YOU_DAO_OCR_URL"))
.form("detectType", "10012")
.form("imageType", "1")
.form("langType", "auto")
.form("img", base64Image)
.form("docType", "json")
.form("signType", "v3")
.form("curtime", curTime)
.form("appKey", SETTING.get("YOU_DAO_APP_KEY"))
.form("salt", salt)
.form("sign", DigestUtil.sha256Hex(SETTING.get("YOU_DAO_APP_KEY") + truncate(base64Image) + salt + curTime + SETTING.get("YOU_DAO_APP_SECRET")));
try (HttpResponse response = request.execute()) {
Console.log(response.body());
}
}

private static String truncate(String q) {
if (q == null) {
return null;
}
int len = q.length();
return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len));
}
}

配置文件如下:

1
2
3
YOU_DAO_OCR_URL = https://openapi.youdao.com/ocrapi
YOU_DAO_APP_KEY = <YOUR_YOU_DAO_APP_KEY>
YOU_DAO_APP_SECRET = <YOUR_YOU_DAO_APP_SECRET>

执行结果如下:

image-20220910125752346