fasdfsa commited on
Commit
bde4b41
·
1 Parent(s): 05efe39

C# 调 C++ dll ok

Browse files
imradv3.sln CHANGED
@@ -107,8 +107,8 @@ Global
107
  {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|x86.Build.0 = Debug|Any CPU
108
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|Any CPU.ActiveCfg = Release|Any CPU
109
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|Any CPU.Build.0 = Release|Any CPU
110
- {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.ActiveCfg = Debug|Any CPU
111
- {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.Build.0 = Debug|Any CPU
112
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.ActiveCfg = Release|Any CPU
113
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.Build.0 = Release|Any CPU
114
  EndGlobalSection
 
107
  {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|x86.Build.0 = Debug|Any CPU
108
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|Any CPU.ActiveCfg = Release|Any CPU
109
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|Any CPU.Build.0 = Release|Any CPU
110
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.ActiveCfg = Debug|x64
111
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.Build.0 = Debug|x64
112
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.ActiveCfg = Release|Any CPU
113
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.Build.0 = Release|Any CPU
114
  EndGlobalSection
src/WpfEditor/Constant.cs CHANGED
@@ -13,5 +13,15 @@ namespace WpfEditor
13
  /// 可执行文件所在目录的路径
14
  /// </summary>
15
  public static readonly string ExecutablePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
 
 
 
 
 
 
 
 
 
 
16
  }
17
  }
 
13
  /// 可执行文件所在目录的路径
14
  /// </summary>
15
  public static readonly string ExecutablePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
16
+
17
+ /// <summary>
18
+ /// WeChatOcrCpp.dll的路径
19
+ /// </summary>
20
+ public static readonly string WeChatOcrDllPath = Path.GetFullPath(Path.Combine(ExecutablePath, "WeChatOcrCpp.dll"));
21
+
22
+ /// <summary>
23
+ /// 当前工作目录(可能是解决方案所在目录)
24
+ /// </summary>
25
+ public static readonly string CurrentWorkingDirectory = Directory.GetCurrentDirectory();
26
  }
27
  }
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -61,15 +61,15 @@ namespace WpfEditor
61
  public static (string ImagePath, string JsonPath) get_img_json_pth(string m5)
62
  {
63
  // 这是工作目录
64
- string pthImg = Path.Combine(Constant.ExecutablePath, "data", "img", $"{m5}.txt"); // .parent_path().parent_path() 在C#中不需要
65
- string pthJson = Path.Combine(Constant.ExecutablePath, "data", "json", $"{m5}.json"); // .parent_path().parent_path() 在C#中不需要
66
 
67
  // C#中不需要lexically_normal(),Path.GetFullPath可以规范化路径
68
- pthImg = Path.GetFullPath(pthImg);
69
- pthJson = Path.GetFullPath(pthJson);
70
 
71
  // 使用C# 7.0引入的命名元组返回结果
72
- return (ImagePath: pthImg, JsonPath: pthJson);
73
  }
74
 
75
  private void OpenFile_Click(object sender, RoutedEventArgs e)
@@ -89,16 +89,21 @@ namespace WpfEditor
89
  {
90
  string m5 = MD5.ComputeFileHash(openFileDialog.FileName);
91
 
92
- var (pthImg, pthJson) = get_img_json_pth(m5);
93
 
94
- if (!Path.Exists(pthJson) || !Path.Exists(pthImg))
95
  {
 
 
 
 
 
 
 
96
  MessageBox.Show("不存在 json 或 image", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
97
  return;
98
  }
99
 
100
- string pth_img = Path.Combine(Constant.ExecutablePath, "data", "img", $"{m5}.txt"); // .parent_path().parent_path() 在C#中不需要
101
- string pth_json = Path.Combine(Constant.ExecutablePath, "data", "json", $"{m5}.json");
102
 
103
  try
104
  {
 
61
  public static (string ImagePath, string JsonPath) get_img_json_pth(string m5)
62
  {
63
  // 这是工作目录
64
+ string pth_img = Path.Combine(Constant.ExecutablePath, "data", "img", $"{m5}.txt"); // .parent_path().parent_path() 在C#中不需要
65
+ string pth_json = Path.Combine(Constant.ExecutablePath, "data", "json", $"{m5}.json"); // .parent_path().parent_path() 在C#中不需要
66
 
67
  // C#中不需要lexically_normal(),Path.GetFullPath可以规范化路径
68
+ pth_img = Path.GetFullPath(pth_img);
69
+ pth_json = Path.GetFullPath(pth_json);
70
 
71
  // 使用C# 7.0引入的命名元组返回结果
72
+ return (ImagePath: pth_img, JsonPath: pth_json);
73
  }
74
 
75
  private void OpenFile_Click(object sender, RoutedEventArgs e)
 
89
  {
90
  string m5 = MD5.ComputeFileHash(openFileDialog.FileName);
91
 
92
+ var (pth_img, pth_json) = get_img_json_pth(m5);
93
 
94
+ if (!Path.Exists(pth_json) || !Path.Exists(pth_img))
95
  {
96
+
97
+ string result = WeChatOcr.RecognizeImage(openFileDialog.FileName);
98
+ Console.WriteLine(result);
99
+
100
+ // 应用程序退出时释放资源
101
+ WeChatOcr.FreeDll();
102
+
103
  MessageBox.Show("不存在 json 或 image", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
104
  return;
105
  }
106
 
 
 
107
 
108
  try
109
  {
src/WpfEditor/Properties/launchSettings.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "profiles": {
3
+ "WpfEditor": {
4
+ "commandName": "Project",
5
+ "hotReloadEnabled": false,
6
+ "nativeDebugging": true
7
+ }
8
+ }
9
+ }
src/WpfEditor/WeChatOcr.cs ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Linq;
4
+ using System.Runtime.InteropServices;
5
+ using System.Text;
6
+ using System.Threading.Tasks;
7
+
8
+ namespace WpfEditor
9
+ {
10
+ /// <summary>
11
+ /// 提供与WeChatOcrCpp.dll交互的功能
12
+ /// </summary>
13
+ public static class WeChatOcr
14
+ {
15
+ // DLL句柄
16
+ private static IntPtr _wechatOcrDllHandle = IntPtr.Zero;
17
+
18
+ // 定义函数委托类型,对应C++中的FunctionPtr
19
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
20
+ private delegate IntPtr RecFunctionDelegate(string pthImg);
21
+
22
+ // 函数指针
23
+ private static RecFunctionDelegate _recFunction;
24
+
25
+ /// <summary>
26
+ /// 加载WeChatOcrCpp.dll并获取函数指针
27
+ /// </summary>
28
+ /// <returns>是否成功加载</returns>
29
+ public static bool LoadWeChatOcrDll()
30
+ {
31
+ try
32
+ {
33
+ // 加载DLL,相当于C++中的LoadLibrary
34
+ _wechatOcrDllHandle = NativeMethods.LoadLibrary(Constant.WeChatOcrDllPath);
35
+
36
+ if (_wechatOcrDllHandle == IntPtr.Zero)
37
+ {
38
+ Console.WriteLine($"无法加载DLL: {Constant.WeChatOcrDllPath}");
39
+ return false;
40
+ }
41
+
42
+ // 获取函数地址,相当于C++中的GetProcAddress
43
+ IntPtr recFuncPtr = NativeMethods.GetProcAddress(_wechatOcrDllHandle, "rec");
44
+
45
+ if (recFuncPtr == IntPtr.Zero)
46
+ {
47
+ Console.WriteLine("无法找到函数'rec'");
48
+ NativeMethods.FreeLibrary(_wechatOcrDllHandle);
49
+ _wechatOcrDllHandle = IntPtr.Zero;
50
+ return false;
51
+ }
52
+
53
+ // 将函数指针转换为委托
54
+ _recFunction = Marshal.GetDelegateForFunctionPointer<RecFunctionDelegate>(recFuncPtr);
55
+
56
+ return true;
57
+ }
58
+ catch (Exception ex)
59
+ {
60
+ Console.WriteLine($"加载DLL时出错: {ex.Message}");
61
+ return false;
62
+ }
63
+ }
64
+
65
+ /// <summary>
66
+ /// 调用WeChatOcrCpp.dll中的rec函数进行OCR识别
67
+ /// </summary>
68
+ /// <param name="imagePath">图片路径</param>
69
+ /// <returns>OCR识别结果</returns>
70
+ public static string RecognizeImage(string imagePath)
71
+ {
72
+ if (_wechatOcrDllHandle == IntPtr.Zero || _recFunction == null)
73
+ {
74
+ if (!LoadWeChatOcrDll())
75
+ {
76
+ return "DLL未加载或函数未找到";
77
+ }
78
+ }
79
+
80
+ try
81
+ {
82
+ // 调用函数
83
+ IntPtr resultPtr = _recFunction(imagePath);
84
+
85
+ if (resultPtr == IntPtr.Zero)
86
+ {
87
+ return string.Empty;
88
+ }
89
+
90
+ // 将C字符串转换为C#字符串
91
+ return Marshal.PtrToStringAnsi(resultPtr);
92
+ }
93
+ catch (Exception ex)
94
+ {
95
+ return $"调用OCR函数时出错: {ex.Message}";
96
+ }
97
+ }
98
+
99
+ /// <summary>
100
+ /// 释放DLL资源
101
+ /// </summary>
102
+ public static void FreeDll()
103
+ {
104
+ if (_wechatOcrDllHandle != IntPtr.Zero)
105
+ {
106
+ NativeMethods.FreeLibrary(_wechatOcrDllHandle);
107
+ _wechatOcrDllHandle = IntPtr.Zero;
108
+ _recFunction = null;
109
+ }
110
+ }
111
+ }
112
+
113
+ /// <summary>
114
+ /// 提供Windows API的P/Invoke定义
115
+ /// </summary>
116
+ internal static class NativeMethods
117
+ {
118
+ [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
119
+ public static extern IntPtr LoadLibrary(string lpFileName);
120
+
121
+ [DllImport("kernel32.dll", SetLastError = true)]
122
+ public static extern bool FreeLibrary(IntPtr hModule);
123
+
124
+ [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
125
+ public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
126
+ }
127
+ }
src/WpfEditor/WpfEditor.csproj CHANGED
@@ -6,6 +6,7 @@
6
  <Nullable>enable</Nullable>
7
  <ImplicitUsings>enable</ImplicitUsings>
8
  <UseWPF>true</UseWPF>
 
9
  </PropertyGroup>
10
 
11
  <ItemGroup>
@@ -15,4 +16,15 @@
15
  </None>
16
  </ItemGroup>
17
 
 
 
 
 
 
 
 
 
 
 
 
18
  </Project>
 
6
  <Nullable>enable</Nullable>
7
  <ImplicitUsings>enable</ImplicitUsings>
8
  <UseWPF>true</UseWPF>
9
+ <Platforms>x64</Platforms>
10
  </PropertyGroup>
11
 
12
  <ItemGroup>
 
16
  </None>
17
  </ItemGroup>
18
 
19
+ <ItemGroup>
20
+ <None Include="..\..\x64\Release\*.dll">
21
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22
+ <Link>%(Filename)%(Extension)</Link>
23
+ </None>
24
+ </ItemGroup>
25
+
26
+ <ItemGroup>
27
+ <ProjectReference Include="..\WeChatOcrCpp\WeChatOcrCpp.vcxproj" />
28
+ </ItemGroup>
29
+
30
  </Project>