|
@@ -1,16 +1,28 @@
|
|
package com.management.platform.util;
|
|
package com.management.platform.util;
|
|
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
import com.baidu.aip.util.Base64Util;
|
|
import com.baidu.aip.util.Base64Util;
|
|
|
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Author: 吴涛涛 cuiyi@itany.com
|
|
|
|
+ * Date : 2019 - 08 - 30 13:59
|
|
|
|
+ * Description:<描述> 百度文字识别api识别图片里的文字工具类
|
|
|
|
+ * Version: 1.0
|
|
|
|
+ */
|
|
public class CheckPicUtil {
|
|
public class CheckPicUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 重要提示代码中所需工具类
|
|
|
|
- * 下载
|
|
|
|
- */
|
|
|
|
|
|
+ * 调用百度文字识别api识别图片里的文字
|
|
|
|
+ * @param path 图片路径
|
|
|
|
+ * @param token 调用百度文字识别api的access_token
|
|
|
|
+ * @return 返回调用api识别出来的内容字符串
|
|
|
|
+ */
|
|
public static String general(String path,String token) {
|
|
public static String general(String path,String token) {
|
|
// 请求url
|
|
// 请求url
|
|
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
|
|
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
|
|
@@ -34,8 +46,45 @@ public class CheckPicUtil {
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+ /**
|
|
|
|
+ * 调用百度文字识别api识别图片里的文字
|
|
|
|
+ * @param path 图片路径
|
|
|
|
+ * @param token 调用百度文字识别api的access_token
|
|
|
|
+ * @return 返回调用api识别出来的内容的set集合
|
|
|
|
+ */
|
|
|
|
+ public static Set<String> generalPicTextContent (String path,String token) {
|
|
|
|
+ // 请求url
|
|
|
|
+ String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
|
|
|
|
+ try {
|
|
|
|
+ // 本地文件路径
|
|
|
|
+ String filePath = path;
|
|
|
|
+ byte[] imgData = FileUtil.readFileByBytes(filePath);
|
|
|
|
+ String imgStr = Base64Util.encode(imgData);
|
|
|
|
+ String imgParam = URLEncoder.encode(imgStr, "UTF-8");
|
|
|
|
+
|
|
|
|
+ String param = "image=" + imgParam;
|
|
|
|
+
|
|
|
|
+ // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
|
|
|
|
+ String accessToken = token;
|
|
|
|
+
|
|
|
|
+ String result = HttpUtil.post(url, accessToken, param);
|
|
|
|
+ Set<String> wordsList = new HashSet<>();
|
|
|
|
+ JSONObject object = JSONArray.parseObject(result);
|
|
|
|
+ JSONArray resultArray = object.getJSONArray("words_result");
|
|
|
|
+ for (int i = 0; i < resultArray.size(); i++) {
|
|
|
|
+ JSONObject json = resultArray.getJSONObject(i);
|
|
|
|
+ String words = (String)json.get("words");
|
|
|
|
+ wordsList.add(words);
|
|
|
|
+ }
|
|
|
|
+ System.out.println(wordsList);
|
|
|
|
+ return wordsList;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
- CheckPicUtil.general("D:\\1.png",(String)AuthService.getAuth().get("access_token"));
|
|
|
|
|
|
+ CheckPicUtil.generalPicTextContent("D:\\360MoveData\\Users\\Administrator\\Desktop\\idea.jpg",(String)AuthService.getAuth().get("access_token"));
|
|
}
|
|
}
|
|
}
|
|
}
|