当前位置:   article > 正文

百度API连接_baiduapiauth

baiduapiauth

百度API连接分为两步:

1、使用API key和secret key获取token

2、连接调用方法。


1、获取token。

  1. public static String getAuth() {
  2. // 官网获取的 API Key 更新为你注册的
  3. String clientId = "XP8YaCKuqZW7GIfEf05iFa";
  4. // 官网获取的 Secret Key 更新为你注册的
  5. String clientSecret = "jrFmEH4VX9MsSUaEFm7LH0tGZt1B7";
  6. return getAuth(clientId, clientSecret);
  7. }
  8. /**
  9. * 获取API访问token
  10. * 该token有一定的有效期,需要自行管理,当失效时需重新获取.
  11. * @param ak - 百度云官网获取的 API Key
  12. * @param sk - 百度云官网获取的 Securet Key
  13. * @return assess_token 示例:
  14. * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
  15. */
  16. public static String getAuth(String ak, String sk) {
  17. // 获取token地址
  18. String authHost = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials";
  19. String getAccessTokenUrl = authHost+ "&client_id=" + ak + "&client_secret=" + sk;
  20. try {
  21. URL realUrl = new URL(getAccessTokenUrl);
  22. HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
  23. connection.setRequestMethod("GET");
  24. connection.connect();
  25. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  26. String result = "";
  27. String line;
  28. while ((line = in.readLine()) != null) {
  29. result += line;
  30. }
  31. JSONObject jsonObject = new JSONObject(result);
  32. String access_token = jsonObject.getString("access_token");
  33. return access_token;
  34. } catch (Exception e) {
  35. e.printStackTrace(System.err);
  36. }
  37. return null;
  38. }

2、连接:识别图片中的文字

  1. public static void main(String[] args) {
  2. File file = new File("d:/test.jpg");
  3. String imageBase = encodeImgageToBase64(file);
  4. imageBase = imageBase.replaceAll("\r\n","");
  5. imageBase = imageBase.replaceAll("\\+","%2B");
  6. String httpUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=24.6cc81615b0f0940b002f39bb3ee08.2592000.1519375458.282335-10719029";
  7. String httpArg = "image="+imageBase;
  8. String res = request(httpUrl, httpArg);
  9. }
  10. public static String request(String httpUrl, String httpArg) {
  11. BufferedReader reader = null;
  12. StringBuffer sbf = new StringBuffer();
  13. try {
  14. URL url = new URL(httpUrl);
  15. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  16. connection.setRequestMethod("POST");
  17. connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  18. connection.setDoOutput(true);
  19. connection.getOutputStream().write(httpArg.getBytes("UTF-8"));
  20. connection.connect();
  21. InputStream is = connection.getInputStream();
  22. reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  23. String strRead = null;
  24. while ((strRead = reader.readLine()) != null) {
  25. sbf.append(strRead);
  26. sbf.append("\r\n");
  27. }
  28. reader.close();
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. return sbf.toString();
  33. }
  34. public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
  35. // 其进行Base64编码处理
  36. byte[] data = null;
  37. // 读取图片字节数组
  38. try {
  39. InputStream in = new FileInputStream(imageFile);
  40. data = new byte[in.available()];
  41. in.read(data);
  42. in.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. // 对字节数组Base64编码
  47. return new BASE64Encoder().encode(data);// 返回Base64编码过的字节数组字符串
  48. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/737386
推荐阅读
相关标签
  

闽ICP备14008679号