Skip to content

Commit 9ba6244

Browse files
committed
add BaiDu OCR interface
1 parent 01bd29a commit 9ba6244

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
## 树洞 OCR 文字识别
22
一款跨平台的 OCR 小工具
33

4-
百度网盘:[https://pan.baidu.com/s/1gVVQ58fZ8ori-O7rWKpRPQ](https://pan.baidu.com/s/1gVVQ58fZ8ori-O7rWKpRPQ)
5-
提取码:m6d8
4+
下载地址:[百度网盘](https://pan.baidu.com/s/1gVVQ58fZ8ori-O7rWKpRPQ) 提取码:`m6d8`
65

76
> - `xxx-with-jre.xx` 是完整版,带运行环境;如果精简版不能正常工作,请下载完整版使用;
87
> - 文字识别使用了各云平台开发的识别接口,因此需要联网才能正常使用;
@@ -38,6 +37,7 @@
3837
- [x] 识别结果文本对齐(暂未实现多分栏)
3938
- [x] 全屏模式下截图
4039
- [x] 添加正在识别动画
40+
- [x] 多屏支持
4141
- [ ] 文本翻译
4242
- [ ] 公式识别
4343
- [ ] 表格识别

src/main/java/com/luooqi/ocr/MainFm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static void doOcr(BufferedImage image){
182182
processController.show();
183183
Thread ocrThread = new Thread(()->{
184184
byte[] bytes = CommUtils.imageToBytes(image);
185-
String text = OcrUtils.sogouWebOcr(bytes);
185+
String text = OcrUtils.ocrImg(bytes);
186186
Platform.runLater(()-> {
187187
processController.close();
188188
textArea.setText(text);

src/main/java/com/luooqi/ocr/utils/OcrUtils.java

+75-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.luooqi.ocr.utils;
22

33
import cn.hutool.core.codec.Base64;
4+
import cn.hutool.core.lang.UUID;
45
import cn.hutool.core.util.CharsetUtil;
56
import cn.hutool.core.util.StrUtil;
7+
import cn.hutool.core.util.URLUtil;
68
import cn.hutool.crypto.SecureUtil;
79
import cn.hutool.http.HttpRequest;
810
import cn.hutool.http.HttpResponse;
@@ -12,14 +14,56 @@
1214
import cn.hutool.json.JSONUtil;
1315
import com.luooqi.ocr.model.TextBlock;
1416

17+
import java.awt.*;
1518
import java.util.*;
19+
import java.util.List;
1620

1721
/**
1822
* tools-ocr
1923
* Created by 何志龙 on 2019-03-22.
2024
*/
2125
public class OcrUtils {
2226

27+
public static String ocrImg(byte[] imgData) {
28+
int i = Math.abs(UUID.randomUUID().hashCode()) % 4;
29+
switch (i){
30+
case 0:
31+
return bdGeneralOcr(imgData);
32+
case 1:
33+
return bdAccurateOcr(imgData);
34+
case 2:
35+
return sogouMobileOcr(imgData);
36+
default:
37+
return sogouWebOcr(imgData);
38+
}
39+
}
40+
41+
private static String bdGeneralOcr(byte[] imgData){
42+
return bdBaseOcr(imgData, "general_location");
43+
}
44+
45+
private static String bdAccurateOcr(byte[] imgData){
46+
return bdBaseOcr(imgData, "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate");
47+
}
48+
49+
private static String bdBaseOcr(byte[] imgData, String type){
50+
String[] urlArr = new String[]{"http://ai.baidu.com/tech/ocr/general", "http://ai.baidu.com/index/seccode?action=show"};
51+
StringBuilder cookie = new StringBuilder();
52+
for (String url : urlArr) {
53+
HttpResponse cookieResp = WebUtils.get(url);
54+
List<String> ckList = cookieResp.headerList("Set-Cookie");
55+
for (String s : ckList) {
56+
cookie.append(s.replaceAll("expires[\\S\\s]+", ""));
57+
}
58+
}
59+
HashMap<String, String> header = new HashMap<>();
60+
header.put("Referer", "http://ai.baidu.com/tech/ocr/general");
61+
header.put("Cookie", cookie.toString());
62+
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";
63+
HttpResponse response = WebUtils.postRaw("http://ai.baidu.com/aidemo", data, 0, header);
64+
return extractBdResult(WebUtils.getSafeHtml(response));
65+
}
66+
2367
public static String sogouMobileOcr(byte[] imgData) {
2468
String boundary = "------WebKitFormBoundary8orYTmcj8BHvQpVU";
2569
String url = "http://ocr.shouji.sogou.com/v2/ocr/json";
@@ -59,7 +103,7 @@ private static String extractSogouResult(String html) {
59103
}
60104
JSONArray jsonArray = jsonObject.getJSONArray("result");
61105
List<TextBlock> textBlocks = new ArrayList<>();
62-
boolean isEng = false;
106+
boolean isEng;
63107
for (int i = 0; i < jsonArray.size(); i++) {
64108
JSONObject jObj = jsonArray.getJSONObject(i);
65109
TextBlock textBlock = new TextBlock();
@@ -76,4 +120,34 @@ private static String extractSogouResult(String html) {
76120
return CommUtils.combineTextBlocks(textBlocks, isEng);
77121
}
78122

123+
private static String extractBdResult(String html) {
124+
if (StrUtil.isBlank(html)) {
125+
return "";
126+
}
127+
JSONObject jsonObject = JSONUtil.parseObj(html);
128+
if (jsonObject.getInt("errno", 0) != 0) {
129+
return "";
130+
}
131+
JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("words_result");
132+
List<TextBlock> textBlocks = new ArrayList<>();
133+
boolean isEng = false;
134+
for (int i = 0; i < jsonArray.size(); i++) {
135+
JSONObject jObj = jsonArray.getJSONObject(i);
136+
TextBlock textBlock = new TextBlock();
137+
textBlock.setText(jObj.getStr("words").trim());
138+
//noinspection SuspiciousToArrayCall
139+
JSONObject location = jObj.getJSONObject("location");
140+
int top = location.getInt("top");
141+
int left = location.getInt("left");
142+
int width = location.getInt("width");
143+
int height = location.getInt("height");
144+
textBlock.setTopLeft(new Point(top, left));
145+
textBlock.setTopRight(new Point(top, left + width));
146+
textBlock.setBottomLeft(new Point(top + height, left));
147+
textBlock.setBottomRight(new Point(top + height, left + width));
148+
textBlocks.add(textBlock);
149+
}
150+
return CommUtils.combineTextBlocks(textBlocks, isEng);
151+
}
152+
79153
}

0 commit comments

Comments
 (0)