fasdfsa commited on
Commit
65905ed
·
1 Parent(s): 44da2e2

加逻辑删除 not done yet.

Browse files
data/0055.jpg ADDED

Git LFS Details

  • SHA256: 08d0e74b5c1d6355898c211ffcb2e6391e0c14bf80aeaa0eebcef36b8bc75e34
  • Pointer size: 131 Bytes
  • Size of remote file: 297 kB
src/WpfEditor/TextEditorControl.cs CHANGED
@@ -34,6 +34,7 @@ namespace WpfEditor
34
 
35
  public JObject _jsn_OpenImg; // for 校对
36
  public string _md5_OpenImg;
 
37
 
38
  public static BitmapSource MatToBitmapSource(Mat mat)
39
  {
@@ -1049,6 +1050,7 @@ namespace WpfEditor
1049
  {
1050
  string oneChar = item2["word"].Value<string>();
1051
  string guid = item2["guid"].Value<string>();
 
1052
  // 就算是阿里云的 json ,也要先给它加入 guid
1053
  //if (item2["guid"] != null)
1054
  //{
@@ -1057,6 +1059,12 @@ namespace WpfEditor
1057
  //{
1058
  // guid = Guid.NewGuid().ToString();
1059
  //}
 
 
 
 
 
 
1060
  int x = item2["x"].Value<int>();
1061
  int y = item2["y"].Value<int>();
1062
  int w = item2["w"].Value<int>();
@@ -1223,6 +1231,11 @@ namespace WpfEditor
1223
  i += rune.Utf16SequenceLength;
1224
  }
1225
 
 
 
 
 
 
1226
  foreach (TextLine line in _lines)
1227
  {
1228
  bool line_modify = false;
@@ -1603,8 +1616,13 @@ namespace WpfEditor
1603
  DeleteSelection();
1604
  }
1605
  else if (_cursorColumn < _lines[_cursorLine].Characters.Count)
1606
- {
1607
- // 删除当前字符
 
 
 
 
 
1608
  _lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
1609
  _textChanged = true;
1610
  OnTextChanged();
@@ -1629,6 +1647,11 @@ namespace WpfEditor
1629
  {
1630
  // 删除前一个字符
1631
  _cursorColumn--;
 
 
 
 
 
1632
  _lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
1633
  _textChanged = true;
1634
  OnTextChanged();
 
34
 
35
  public JObject _jsn_OpenImg; // for 校对
36
  public string _md5_OpenImg;
37
+ public Dictionary<string, string> _dic_deleted_char = new Dictionary<string, string>(); // 被删除的字符存它的 guid,保存的时侯标记它逻辑删除
38
 
39
  public static BitmapSource MatToBitmapSource(Mat mat)
40
  {
 
1050
  {
1051
  string oneChar = item2["word"].Value<string>();
1052
  string guid = item2["guid"].Value<string>();
1053
+ int isDeleted = item2["isDeleted"].Value<int>();
1054
  // 就算是阿里云的 json ,也要先给它加入 guid
1055
  //if (item2["guid"] != null)
1056
  //{
 
1059
  //{
1060
  // guid = Guid.NewGuid().ToString();
1061
  //}
1062
+
1063
+ if (isDeleted == 1)
1064
+ {
1065
+ continue;
1066
+ }
1067
+
1068
  int x = item2["x"].Value<int>();
1069
  int y = item2["y"].Value<int>();
1070
  int w = item2["w"].Value<int>();
 
1231
  i += rune.Utf16SequenceLength;
1232
  }
1233
 
1234
+ if ( _dic_deleted_char.ContainsKey(guid) ) {
1235
+ // 是被标记逻辑删除的字符
1236
+ item2["isDeleted"] = 1;
1237
+ }
1238
+
1239
  foreach (TextLine line in _lines)
1240
  {
1241
  bool line_modify = false;
 
1616
  DeleteSelection();
1617
  }
1618
  else if (_cursorColumn < _lines[_cursorLine].Characters.Count)
1619
+ {
1620
+ // 删除当前字符
1621
+ TextCharacter c = _lines[_cursorLine].Characters[_cursorColumn];
1622
+ if (c.guid != "")
1623
+ {
1624
+ _dic_deleted_char[c.guid] = c.Character.Value.ToString();
1625
+ }
1626
  _lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
1627
  _textChanged = true;
1628
  OnTextChanged();
 
1647
  {
1648
  // 删除前一个字符
1649
  _cursorColumn--;
1650
+ TextCharacter c = _lines[_cursorLine].Characters[_cursorColumn];
1651
+ if (c.guid != "")
1652
+ {
1653
+ _dic_deleted_char[c.guid] = c.Character.Value.ToString();
1654
+ }
1655
  _lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
1656
  _textChanged = true;
1657
  OnTextChanged();
src/WpfEditor/WeChatOcr.cs CHANGED
@@ -1,178 +1,179 @@
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
-
11
- using Formatting = Newtonsoft.Json.Formatting;
12
-
13
- namespace WpfEditor
14
- {
15
- /// <summary>
16
- /// 提供与WeChatOcrCpp.dll交互的功能
17
- /// </summary>
18
- public static class WeChatOcr
19
- {
20
- // DLL句柄
21
- private static IntPtr _wechatOcrDllHandle = IntPtr.Zero;
22
-
23
- // 定义函数委托类型,对应C++中的FunctionPtr
24
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
25
- private delegate IntPtr RecFunctionDelegate(string pthImg);
26
-
27
- // 函数指针
28
- private static RecFunctionDelegate _recFunction;
29
-
30
- /// <summary>
31
- /// 加载WeChatOcrCpp.dll并获取函数指针
32
- /// </summary>
33
- /// <returns>是否成功加载</returns>
34
- public static bool LoadWeChatOcrDll()
35
- {
36
- try
37
- {
38
- // 加载DLL,相当于C++中的LoadLibrary
39
- _wechatOcrDllHandle = NativeMethods.LoadLibrary(Constant.WeChatOcrDllPath);
40
-
41
- if (_wechatOcrDllHandle == IntPtr.Zero)
42
- {
43
- Console.WriteLine($"无法加载DLL: {Constant.WeChatOcrDllPath}");
44
- return false;
45
- }
46
-
47
- // 获取函数地址,相当于C++中的GetProcAddress
48
- IntPtr recFuncPtr = NativeMethods.GetProcAddress(_wechatOcrDllHandle, "rec");
49
-
50
- if (recFuncPtr == IntPtr.Zero)
51
- {
52
- Console.WriteLine("无法找到函数'rec'");
53
- NativeMethods.FreeLibrary(_wechatOcrDllHandle);
54
- _wechatOcrDllHandle = IntPtr.Zero;
55
- return false;
56
- }
57
-
58
- // 将函数指针转换为委托
59
- _recFunction = Marshal.GetDelegateForFunctionPointer<RecFunctionDelegate>(recFuncPtr);
60
-
61
- return true;
62
- }
63
- catch (Exception ex)
64
- {
65
- Console.WriteLine($"加载DLL时出错: {ex.Message}");
66
- return false;
67
- }
68
- }
69
-
70
- /// <summary>
71
- /// 调用WeChatOcrCpp.dll中的rec函数进行OCR识别
72
- /// </summary>
73
- /// <param name="imagePath">图片路径</param>
74
- /// <returns>OCR识别结果</returns>
75
- public static string RecognizeImage(string imagePath)
76
- {
77
- if (_wechatOcrDllHandle == IntPtr.Zero || _recFunction == null)
78
- {
79
- if (!LoadWeChatOcrDll())
80
- {
81
- return "DLL未加载或函数未找到";
82
- }
83
- }
84
-
85
- try
86
- {
87
- // 调用函数
88
- IntPtr resultPtr = _recFunction(imagePath);
89
-
90
- if (resultPtr == IntPtr.Zero)
91
- {
92
- return string.Empty;
93
- }
94
-
95
- // 将C字符串转换为C#字符串
96
- string result = Marshal.PtrToStringAnsi(resultPtr);
97
-
98
- // 给每个字符加上 guid
99
- JObject jsn = null;
100
- try
101
- {
102
- jsn = JObject.Parse(result);
103
- }
104
- catch (Exception ex)
105
- {
106
- MessageBox.Show("json 解析失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
107
- return "";
108
- }
109
-
110
- int width = jsn.Value<int>("width");
111
- int height = jsn.Value<int>("height");
112
-
113
- var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
114
- foreach (var item in prism_wordsInfo)
115
- {
116
- JArray charInfo = item["charInfo"].Value<JArray>();
117
- string line_word = item["word"].Value<string>();
118
-
119
- var textLine = new TextLine();
120
- foreach (var item2 in charInfo)
121
- {
122
- item2["guid"] = Guid.NewGuid().ToString();
123
-
124
- string oneChar = item2["word"].Value<string>();
125
- int x = item2["x"].Value<int>();
126
- int y = item2["y"].Value<int>();
127
- int w = item2["w"].Value<int>();
128
- int h = item2["h"].Value<int>();
129
-
130
- //// 遍历 oneChar 中的每个字符
131
- //for (int i = 0; i < oneChar.Length;)
132
- //{
133
- // Rune rune = Rune.GetRuneAt(oneChar, i); // 4 个字节表示单个字符
134
-
135
- // // 移动到下一个 Rune 的位置
136
- // i += rune.Utf16SequenceLength;
137
- //}
138
-
139
- }
140
- }
141
-
142
- return jsn.ToString(Formatting.Indented);
143
- }
144
- catch (Exception ex)
145
- {
146
- return $"调用OCR函数时出错: {ex.Message}";
147
- }
148
- }
149
-
150
- /// <summary>
151
- /// 释放DLL资源
152
- /// </summary>
153
- public static void FreeDll()
154
- {
155
- if (_wechatOcrDllHandle != IntPtr.Zero)
156
- {
157
- NativeMethods.FreeLibrary(_wechatOcrDllHandle);
158
- _wechatOcrDllHandle = IntPtr.Zero;
159
- _recFunction = null;
160
- }
161
- }
162
- }
163
-
164
- /// <summary>
165
- /// 提供Windows API的P/Invoke定义
166
- /// </summary>
167
- internal static class NativeMethods
168
- {
169
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
170
- public static extern IntPtr LoadLibrary(string lpFileName);
171
-
172
- [DllImport("kernel32.dll", SetLastError = true)]
173
- public static extern bool FreeLibrary(IntPtr hModule);
174
-
175
- [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
176
- public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
177
- }
178
- }
 
 
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
+
11
+ using Formatting = Newtonsoft.Json.Formatting;
12
+
13
+ namespace WpfEditor
14
+ {
15
+ /// <summary>
16
+ /// 提供与WeChatOcrCpp.dll交互的功能
17
+ /// </summary>
18
+ public static class WeChatOcr
19
+ {
20
+ // DLL句柄
21
+ private static IntPtr _wechatOcrDllHandle = IntPtr.Zero;
22
+
23
+ // 定义函数委托类型,对应C++中的FunctionPtr
24
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
25
+ private delegate IntPtr RecFunctionDelegate(string pthImg);
26
+
27
+ // 函数指针
28
+ private static RecFunctionDelegate _recFunction;
29
+
30
+ /// <summary>
31
+ /// 加载WeChatOcrCpp.dll并获取函数指针
32
+ /// </summary>
33
+ /// <returns>是否成功加载</returns>
34
+ public static bool LoadWeChatOcrDll()
35
+ {
36
+ try
37
+ {
38
+ // 加载DLL,相当于C++中的LoadLibrary
39
+ _wechatOcrDllHandle = NativeMethods.LoadLibrary(Constant.WeChatOcrDllPath);
40
+
41
+ if (_wechatOcrDllHandle == IntPtr.Zero)
42
+ {
43
+ Console.WriteLine($"无法加载DLL: {Constant.WeChatOcrDllPath}");
44
+ return false;
45
+ }
46
+
47
+ // 获取函数地址,相当于C++中的GetProcAddress
48
+ IntPtr recFuncPtr = NativeMethods.GetProcAddress(_wechatOcrDllHandle, "rec");
49
+
50
+ if (recFuncPtr == IntPtr.Zero)
51
+ {
52
+ Console.WriteLine("无法找到函数'rec'");
53
+ NativeMethods.FreeLibrary(_wechatOcrDllHandle);
54
+ _wechatOcrDllHandle = IntPtr.Zero;
55
+ return false;
56
+ }
57
+
58
+ // 将函数指针转换为委托
59
+ _recFunction = Marshal.GetDelegateForFunctionPointer<RecFunctionDelegate>(recFuncPtr);
60
+
61
+ return true;
62
+ }
63
+ catch (Exception ex)
64
+ {
65
+ Console.WriteLine($"加载DLL时出错: {ex.Message}");
66
+ return false;
67
+ }
68
+ }
69
+
70
+ /// <summary>
71
+ /// 调用WeChatOcrCpp.dll中的rec函数进行OCR识别
72
+ /// </summary>
73
+ /// <param name="imagePath">图片路径</param>
74
+ /// <returns>OCR识别结果</returns>
75
+ public static string RecognizeImage(string imagePath)
76
+ {
77
+ if (_wechatOcrDllHandle == IntPtr.Zero || _recFunction == null)
78
+ {
79
+ if (!LoadWeChatOcrDll())
80
+ {
81
+ return "DLL未加载或函数未找到";
82
+ }
83
+ }
84
+
85
+ try
86
+ {
87
+ // 调用函数
88
+ IntPtr resultPtr = _recFunction(imagePath);
89
+
90
+ if (resultPtr == IntPtr.Zero)
91
+ {
92
+ return string.Empty;
93
+ }
94
+
95
+ // 将C字符串转换为C#字符串
96
+ string result = Marshal.PtrToStringAnsi(resultPtr);
97
+
98
+ // 给每个字符加上 guid
99
+ JObject jsn = null;
100
+ try
101
+ {
102
+ jsn = JObject.Parse(result);
103
+ }
104
+ catch (Exception ex)
105
+ {
106
+ MessageBox.Show("json 解析失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
107
+ return "";
108
+ }
109
+
110
+ int width = jsn.Value<int>("width");
111
+ int height = jsn.Value<int>("height");
112
+
113
+ var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
114
+ foreach (var item in prism_wordsInfo)
115
+ {
116
+ JArray charInfo = item["charInfo"].Value<JArray>();
117
+ string line_word = item["word"].Value<string>();
118
+
119
+ var textLine = new TextLine();
120
+ foreach (var item2 in charInfo)
121
+ {
122
+ item2["guid"] = Guid.NewGuid().ToString();
123
+ item2["isDeleted"] = 0;
124
+
125
+ string oneChar = item2["word"].Value<string>();
126
+ int x = item2["x"].Value<int>();
127
+ int y = item2["y"].Value<int>();
128
+ int w = item2["w"].Value<int>();
129
+ int h = item2["h"].Value<int>();
130
+
131
+ //// 遍历 oneChar 中的每个字符
132
+ //for (int i = 0; i < oneChar.Length;)
133
+ //{
134
+ // Rune rune = Rune.GetRuneAt(oneChar, i); // 4 个字节表示单个字符
135
+
136
+ // // 移动到下一个 Rune 的位置
137
+ // i += rune.Utf16SequenceLength;
138
+ //}
139
+
140
+ }
141
+ }
142
+
143
+ return jsn.ToString(Formatting.Indented);
144
+ }
145
+ catch (Exception ex)
146
+ {
147
+ return $"调用OCR函数时出错: {ex.Message}";
148
+ }
149
+ }
150
+
151
+ /// <summary>
152
+ /// 释放DLL资源
153
+ /// </summary>
154
+ public static void FreeDll()
155
+ {
156
+ if (_wechatOcrDllHandle != IntPtr.Zero)
157
+ {
158
+ NativeMethods.FreeLibrary(_wechatOcrDllHandle);
159
+ _wechatOcrDllHandle = IntPtr.Zero;
160
+ _recFunction = null;
161
+ }
162
+ }
163
+ }
164
+
165
+ /// <summary>
166
+ /// 提供Windows API的P/Invoke定义
167
+ /// </summary>
168
+ internal static class NativeMethods
169
+ {
170
+ [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
171
+ public static extern IntPtr LoadLibrary(string lpFileName);
172
+
173
+ [DllImport("kernel32.dll", SetLastError = true)]
174
+ public static extern bool FreeLibrary(IntPtr hModule);
175
+
176
+ [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
177
+ public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
178
+ }
179
+ }
src/WpfEditor/global.cs CHANGED
@@ -58,6 +58,7 @@ namespace WpfEditor
58
  {
59
  string oneChar = item2["word"].Value<string>();
60
  item2["guid"] = Guid.NewGuid().ToString();
 
61
  item2["ImageMD5"] = md5;
62
 
63
  if (angle == -90)
 
58
  {
59
  string oneChar = item2["word"].Value<string>();
60
  item2["guid"] = Guid.NewGuid().ToString();
61
+ item2["isDeleted"] = 0;
62
  item2["ImageMD5"] = md5;
63
 
64
  if (angle == -90)