服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java实现百度云文字识别接口代码

java实现百度云文字识别接口代码

2021-06-17 11:25syy363250763 Java教程

这篇文章主要为大家详细介绍了java实现百度云文字识别的接口代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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
65
66
67
68
69
70
71
72
73
public class images {
 
  public static string getresult() {
  string otherhost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
   // 本地图片路径
 
  string str="你的本地图片路径"
 
   string filepath = "str";
   try {
    byte[] imgdata = fileutil.readfilebybytes(filepath);
    string imgstr = base64util.encode(imgdata);
    string params = urlencoder.encode("image", "utf-8") + "=" + urlencoder.encode(imgstr, "utf-8");
    /**
    * access_token有过期时间, 客户端可自行缓存,过期后重新获取。
    */
    string accesstoken = getauth("申请的api key", "申请的secret key");
 
    //system.out.println("wwwwwwwwwwwwww");
    string result = httputil.post(otherhost, accesstoken, params);
    //system.out.println("sssssssssssssssssss");
    return result;
    //system.out.println(result);
   } catch (exception e) {
    e.printstacktrace();
    return null;
   }
  
  }
  public static string getauth(string ak, string sk) {
   // 获取token地址
   string authhost = "https://aip.baidubce.com/oauth/2.0/token?";
   string getaccesstokenurl = authhost
     // 1. grant_type为固定参数
     + "grant_type=client_credentials"
     // 2. 官网获取的 api key
     + "&client_id=" + ak
     // 3. 官网获取的 secret key
     + "&client_secret=" + sk;
   try {
    url realurl = new url(getaccesstokenurl);
    // 打开和url之间的连接
    httpurlconnection connection = (httpurlconnection) realurl.openconnection();
    connection.setrequestmethod("get");
    connection.connect();
    // 获取所有响应头字段
    map<string, list<string>> map = connection.getheaderfields();
    // 遍历所有的响应头字段
    for (string key : map.keyset()) {
     system.err.println(key + "--->" + map.get(key));
    }
    // 定义 bufferedreader输入流来读取url的响应
    bufferedreader in = new bufferedreader(new inputstreamreader(connection.getinputstream()));
    string result = "";
    string line;
    while ((line = in.readline()) != null) {
     result += line;
    }
    /**
     * 返回结果示例
     */
    system.out.println("result:" + result);
    jsonobject jsonobject = new jsonobject(result);
    string access_token = jsonobject.getstring("access_token");
    return access_token;
    
   } catch (exception e) {
    system.err.printf("获取token失败!");
    e.printstacktrace(system.err);
   }
   return null;
  }
}

测试:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(string[] args) {
   
  string otherhost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
   // 本地图片路径
   string filepath = "本地图片路径";
   try {
    byte[] imgdata = fileutil.readfilebybytes(filepath);
    string imgstr = base64util.encode(imgdata);
    string params = urlencoder.encode("image", "utf-8") + "=" + urlencoder.encode(imgstr, "utf-8");
    *//**
     * 线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
     *//*
    string accesstoken = getauth("api key", "secret key");
    //system.out.println("wwwwwwwwwwwwww");
    string result = httputil.post(otherhost, accesstoken, params);
    //system.out.println("sssssssssssssssssss");
    system.out.println(result);
   } catch (exception e) {
    e.printstacktrace();
   }
 }

小编再另分享一份网上找到的代码,百度云ocr文字识别功能,作者是:笑释一切。

?
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
65
import java.util.hashmap;
import java.util.iterator;
 
import org.json.jsonarray;
import org.json.jsonobject;
import com.baidu.aip.ocr.aipocr;
 
/**
 * 测试百度云ocr的文字识别功能 <br>
 * 打开百度云ai的官网: <br>
 * https://console.bce.baidu.com/ai/?_=1517288853048#/ai/ocr/overview/index <br>
 */
public class testocr {
 
 //设置app id/ak/sk
 public static final string app_id = "10736110";
 public static final string api_key = "4nguig7odphzfhdfnz2abvhx";
 public static final string secret_key = "8gnuzj19h0nie5noc7hsgsh2vigju9vl";
 
 public static void main(string[] args) {
 
  // 初始化一个aipocr
  aipocr client = new aipocr(app_id, api_key, secret_key);
 
  // 传入可选参数调用接口
  hashmap<string, string> options = new hashmap<string, string>();
  // 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
  options.put("recognize_granularity", "big");
  // 识别语言类型,默认为chn_eng。可选值包括:
  // chn_eng:中英文混合;
  // eng:英文;
  // por:葡萄牙语;
  // fre:法语;
  // ger:德语;
  // ita:意大利语;
  // spa:西班牙语;
  // rus:俄语;
  // jap:日语;
  // kor:韩语;
  options.put("language_type", "chn_eng");
  // 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。
  options.put("detect_direction", "true");
  // 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
  options.put("detect_language", "true");
  // 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false
  options.put("vertexes_location", "false");
  // 是否返回识别结果中每一行的置信度
  options.put("probability", "false");
 
  // 可选:设置网络连接参数
  client.setconnectiontimeoutinmillis(2000);
  client.setsockettimeoutinmillis(60000);
 
  // 调用接口
  string path = "d:\\qq截图20180130134257.png";
  jsonobject res = client.accurategeneral(path, options);
  jsonarray myjson = res.getjsonarray("words_result");
  iterator<object> iterator = myjson.iterator();
  while(iterator.hasnext()){
   object value = iterator.next();
   jsonobject obj = new jsonobject(value.tostring());
   system.out.println(obj.get("words"));
  }
 
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/syy363250763/article/details/80843480

延伸 · 阅读

精彩推荐