fasdfsa commited on
Commit
11aaab2
·
1 Parent(s): 74ab69d

add config_default.json

Browse files
.gitignore CHANGED
@@ -21,6 +21,7 @@ build/
21
  out.json
22
  tmp.jpg
23
  config.py
 
24
  # *dll
25
  imgui.ini
26
  7e644cf859a5e100a21ff67a63486ae0.txt
 
21
  out.json
22
  tmp.jpg
23
  config.py
24
+ config.json
25
  # *dll
26
  imgui.ini
27
  7e644cf859a5e100a21ff67a63486ae0.txt
src/WpfEditor/AliOcr.cs ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using Newtonsoft.Json.Linq;
2
+ using System;
3
+ using System.Collections.Generic;
4
+ using System.Linq;
5
+ using System.Runtime.InteropServices;
6
+ using System.Text;
7
+ using System.Threading.Tasks;
8
+ using System.Windows;
9
+ using System.Windows.Controls;
10
+ using System.Net.Http;
11
+ using Newtonsoft.Json;
12
+
13
+ using Formatting = Newtonsoft.Json.Formatting;
14
+
15
+ namespace WpfEditor
16
+ {
17
+ /// <summary>
18
+ /// 提供与WeChatOcrCpp.dll交互的功能
19
+ /// </summary>
20
+ public static class AliOcr
21
+ {
22
+ private static readonly HttpClient httpClient = new HttpClient();
23
+
24
+ private static async Task<string> ocrAsync(string api, string appCode, string imgData)
25
+ {
26
+ try
27
+ {
28
+ // 设置超时时间为 24 小时
29
+ httpClient.Timeout = TimeSpan.FromHours(24);
30
+
31
+ // 设置请求头
32
+ httpClient.DefaultRequestHeaders.Clear();
33
+ httpClient.DefaultRequestHeaders.Add("Authorization", $"APPCODE {appCode}");
34
+
35
+ // 创建请求体数据
36
+ var requestData = new
37
+ {
38
+ img = imgData,
39
+ prob = true,
40
+ charInfo = true,
41
+ table = true,
42
+ sortPage = true,
43
+ NeedRotate = true
44
+ };
45
+
46
+ // 将对象序列化为 JSON 字符串
47
+ string jsonContent = JsonConvert.SerializeObject(requestData);
48
+
49
+ // 创建 StringContent 对象
50
+ var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
51
+
52
+ // 发送 POST 请求
53
+ HttpResponseMessage response = await httpClient.PostAsync(api, content);
54
+
55
+ // 确保请求成功
56
+ response.EnsureSuccessStatusCode();
57
+
58
+ // 读取响应内容
59
+ string responseBody = await response.Content.ReadAsStringAsync();
60
+
61
+ return responseBody;
62
+ }
63
+ catch (HttpRequestException ex)
64
+ {
65
+ // 处理 HTTP 请求异常
66
+ throw new Exception($"HTTP 请求失败: {ex.Message}", ex);
67
+ }
68
+ catch (TaskCanceledException ex)
69
+ {
70
+ // 处理超时异常
71
+ throw new Exception($"请求超时: {ex.Message}", ex);
72
+ }
73
+ catch (Exception ex)
74
+ {
75
+ // 处理其他异常
76
+ throw new Exception($"请求过程中发生错误: {ex.Message}", ex);
77
+ }
78
+ }
79
+
80
+
81
+ public static string RecognizeImage(string imagePath)
82
+ {
83
+
84
+
85
+ try
86
+ {
87
+
88
+ // 将C字符串转换为C#字符串
89
+ string result = ""; // ocrAsync(string api, string appCode, string imgData);
90
+
91
+ // 给每个字符加上 guid
92
+ JObject jsn = null;
93
+ try
94
+ {
95
+ jsn = JObject.Parse(result);
96
+ }
97
+ catch (Exception ex)
98
+ {
99
+ MessageBox.Show("json 解析失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
100
+ return "";
101
+ }
102
+
103
+ int width = jsn.Value<int>("width");
104
+ int height = jsn.Value<int>("height");
105
+
106
+ var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
107
+ foreach (var item in prism_wordsInfo)
108
+ {
109
+ JArray charInfo = item["charInfo"].Value<JArray>();
110
+ string line_word = item["word"].Value<string>();
111
+
112
+ var textLine = new TextLine();
113
+ foreach (var item2 in charInfo)
114
+ {
115
+ item2["guid"] = Guid.NewGuid().ToString();
116
+ item2["isDeleted"] = 0;
117
+
118
+ string oneChar = item2["word"].Value<string>();
119
+ int x = item2["x"].Value<int>();
120
+ int y = item2["y"].Value<int>();
121
+ int w = item2["w"].Value<int>();
122
+ int h = item2["h"].Value<int>();
123
+
124
+ //// 遍历 oneChar 中的每个字符
125
+ //for (int i = 0; i < oneChar.Length;)
126
+ //{
127
+ // Rune rune = Rune.GetRuneAt(oneChar, i); // 4 个字节表示单个字符
128
+
129
+ // // 移动到下一个 Rune 的位置
130
+ // i += rune.Utf16SequenceLength;
131
+ //}
132
+
133
+ }
134
+ }
135
+
136
+ return jsn.ToString(Formatting.Indented);
137
+ }
138
+ catch (Exception ex)
139
+ {
140
+ return $"调用OCR函数时出错: {ex.Message}";
141
+ }
142
+ }
143
+
144
+ }
145
+
146
+ }
src/WpfEditor/config_default.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+
3
+ "aliOCR": {
4
+ "api": "https://gjbsb.market.alicloudapi.com/ocrservice/advanced",
5
+ "appKey": "",
6
+ "appCode": "",
7
+ "appSecret": ""
8
+ }
9
+ }
src/WpfEditor/global.cs CHANGED
@@ -236,7 +236,7 @@ namespace WpfEditor
236
  return jsn_new;
237
  }
238
 
239
- public static Tuple<JObject, string, string, string> img_md5_jsn(string pthimg)
240
  {
241
 
242
  string m5 = MD5.ComputeFileHash(pthimg);
@@ -245,7 +245,20 @@ namespace WpfEditor
245
 
246
  if (!Path.Exists(pth_json) || !Path.Exists(pth_img))
247
  {
248
- string result = WeChatOcr.RecognizeImage(pthimg);
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
  string b64_str = Base64.Base64Encode(pthimg);
251
 
 
236
  return jsn_new;
237
  }
238
 
239
+ public static Tuple<JObject, string, string, string> img_md5_jsn(string pthimg, string ocrEngine="wechat")
240
  {
241
 
242
  string m5 = MD5.ComputeFileHash(pthimg);
 
245
 
246
  if (!Path.Exists(pth_json) || !Path.Exists(pth_img))
247
  {
248
+ string result = "";
249
+
250
+ if (ocrEngine == "wechat")
251
+ {
252
+ result = WeChatOcr.RecognizeImage(pthimg);
253
+ }
254
+ else if (ocrEngine == "aliocr")
255
+ {
256
+
257
+ }
258
+ else if (ocrEngine == "ppocr")
259
+ {
260
+
261
+ }
262
 
263
  string b64_str = Base64.Base64Encode(pthimg);
264