|
@@ -13,6 +13,7 @@ import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.luooqi.ocr.model.TextBlock;
|
|
|
+import com.sun.deploy.util.StringUtils;
|
|
|
|
|
|
import java.awt.*;
|
|
|
import java.util.*;
|
|
@@ -25,7 +26,7 @@ import java.util.List;
|
|
|
public class OcrUtils {
|
|
|
|
|
|
public static String ocrImg(byte[] imgData, int ocrType) {
|
|
|
- switch (ocrType){
|
|
|
+ switch (ocrType) {
|
|
|
case 0:
|
|
|
return bdGeneralOcr(imgData);
|
|
|
case 1:
|
|
@@ -37,15 +38,15 @@ public class OcrUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static String bdGeneralOcr(byte[] imgData){
|
|
|
+ private static String bdGeneralOcr(byte[] imgData) {
|
|
|
return bdBaseOcr(imgData, "general_location");
|
|
|
}
|
|
|
|
|
|
- private static String bdAccurateOcr(byte[] imgData){
|
|
|
+ private static String bdAccurateOcr(byte[] imgData) {
|
|
|
return bdBaseOcr(imgData, "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate");
|
|
|
}
|
|
|
|
|
|
- private static String bdBaseOcr(byte[] imgData, String type){
|
|
|
+ private static String bdBaseOcr(byte[] imgData, String type) {
|
|
|
String[] urlArr = new String[]{"http://ai.baidu.com/tech/ocr/general", "http://ai.baidu.com/index/seccode?action=show"};
|
|
|
StringBuilder cookie = new StringBuilder();
|
|
|
for (String url : urlArr) {
|
|
@@ -58,7 +59,7 @@ public class OcrUtils {
|
|
|
HashMap<String, String> header = new HashMap<>();
|
|
|
header.put("Referer", "http://ai.baidu.com/tech/ocr/general");
|
|
|
header.put("Cookie", cookie.toString());
|
|
|
- String data = "type="+URLUtil.encodeQuery(type)+"&detect_direction=false&image_url&image=" + URLUtil.encodeQuery("data:image/jpeg;base64," + Base64.encode(imgData)) + "&language_type=CHN_ENG";
|
|
|
+ String data = "type=" + URLUtil.encodeQuery(type) + "&detect_direction=false&image_url&image=" + URLUtil.encodeQuery("data:image/jpeg;base64," + Base64.encode(imgData)) + "&language_type=CHN_ENG";
|
|
|
HttpResponse response = WebUtils.postRaw("http://ai.baidu.com/aidemo", data, 0, header);
|
|
|
return extractBdResult(WebUtils.getSafeHtml(response));
|
|
|
}
|
|
@@ -72,6 +73,11 @@ public class OcrUtils {
|
|
|
return extractSogouResult(CommUtils.postMultiData(url, postData, boundary.substring(2)));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param imgData
|
|
|
+ * @return
|
|
|
+ * error 签名错误
|
|
|
+ */
|
|
|
public static String sogouWebOcr(byte[] imgData) {
|
|
|
String url = "https://deepi.sogou.com/api/sogouService";
|
|
|
String referer = "https://deepi.sogou.com/?from=picsearch&tdsourcetag=s_pctim_aiomsg";
|
|
@@ -96,6 +102,9 @@ public class OcrUtils {
|
|
|
if (StrUtil.isBlank(html)) {
|
|
|
return "";
|
|
|
}
|
|
|
+ if (JSONUtil.isJson(html) == false) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(html);
|
|
|
if (jsonObject.getInt("success", 0) != 1) {
|
|
|
return "";
|
|
@@ -123,6 +132,9 @@ public class OcrUtils {
|
|
|
if (StrUtil.isBlank(html)) {
|
|
|
return "";
|
|
|
}
|
|
|
+ if (JSONUtil.isJson(html) == false) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(html);
|
|
|
if (jsonObject.getInt("errno", 0) != 0) {
|
|
|
return "";
|