diff --git a/.gitignore b/.gitignore index 3c8f6a9..ce002e8 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ packages/ .DS_Store -publish/ \ No newline at end of file +publish/ +artifacts/ \ No newline at end of file diff --git a/sample/WeChatOcr.Sample/MainWindow.xaml.cs b/sample/WeChatOcr.Sample/MainWindow.xaml.cs index 32cccf9..e389932 100644 --- a/sample/WeChatOcr.Sample/MainWindow.xaml.cs +++ b/sample/WeChatOcr.Sample/MainWindow.xaml.cs @@ -8,7 +8,6 @@ using System.Windows.Input; using ScreenGrab.Extensions; using Wpf.Ui.Tray.Controls; -using System.Windows.Media.Imaging; using System.Text; namespace WeChatOcr.Sample; @@ -108,6 +107,7 @@ private void Ocr_Click(object sender, RoutedEventArgs e) ResultTb.Text = "Please wait for the previous OCR operation to complete."; return; } + _tcs = new TaskCompletionSource(); try { var bytes = ImageUtilities.ConvertBitmap2Bytes(bitmap, ImageFormat.Png); diff --git a/src/WeChatOcr/ImageOcr.cs b/src/WeChatOcr/ImageOcr.cs index 30c97ec..61417f1 100644 --- a/src/WeChatOcr/ImageOcr.cs +++ b/src/WeChatOcr/ImageOcr.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using System.Diagnostics; +using System.Runtime.InteropServices; namespace WeChatOcr; @@ -14,7 +15,7 @@ public ImageOcr(string? path = default) var wechatDir = Utilities.GetWeChatDir(path) ?? throw new Exception($"get wechat path failed: {path ?? "NULL"}"); - Utilities.CopyMmmojoDll(wechatDir, AppDomain.CurrentDomain.BaseDirectory); + Utilities.CopyMmmojoDll(wechatDir); var ocrPtr = GCHandle.ToIntPtr(GCHandle.Alloc(_ocrManager)); _ocrManager = (GCHandle.FromIntPtr(ocrPtr).Target as OcrManager)!; @@ -45,7 +46,7 @@ public void Run(string imagePath, Action? callback) } catch (Exception ex) { - Console.WriteLine(ex.Message); + Debug.WriteLine(ex.Message); return; } } diff --git a/src/WeChatOcr/Utilities.cs b/src/WeChatOcr/Utilities.cs index eb662c7..2cf48bc 100644 --- a/src/WeChatOcr/Utilities.cs +++ b/src/WeChatOcr/Utilities.cs @@ -9,8 +9,9 @@ public partial class Utilities /// 微信默认安装目录 /// private const string WeChatDefaultPath = @"C:\Program Files\Tencent\WeChat"; - private const string MmmojoDll = "mmmojo.dll"; - private const string Mmmojo64Dll = "mmmojo_64.dll"; + + private const string MmMojoDll = "mmmojo.dll"; + private const string MmMojo64Dll = "mmmojo_64.dll"; /// /// 获取微信安装目录 @@ -67,17 +68,47 @@ public static void WriteBytesToFile(string filePath, byte[] bytes) /// /// 复制微信安装目录下的mmmojo.dll、mmmojo_64.dll到软件目录 /// - /// - /// 目标目录 - public static void CopyMmmojoDll(string wechatDir, string targetPath) + /// + /// 微信完整路径包含版本号 + /// * C:\Program Files\Tencent\WeChat\[3.9.12.11] + /// + public static void CopyMmmojoDll(string wechatFullDir) + { + var targetPath = AppDomain.CurrentDomain.BaseDirectory; + var mmMojoFullPath = Path.Combine(wechatFullDir, MmMojoDll); + var mmMojo64FullPath = Path.Combine(wechatFullDir, MmMojo64Dll); + var targetMmMojoFullPath = Path.Combine(targetPath, MmMojoDll); + var targetMmMojo64FullPath = Path.Combine(targetPath, MmMojo64Dll); + if (!File.Exists(targetMmMojoFullPath)) + File.Copy(mmMojoFullPath, targetMmMojoFullPath); + if (!File.Exists(targetMmMojo64FullPath)) + File.Copy(mmMojo64FullPath, targetMmMojo64FullPath); + } + + /// + /// 移除软件目录下的mmmojo.dll、mmmojo_64.dll + /// + /// + public static bool RemoveMmmojoDll(out string error) { - var mmmojoFullPath = Path.Combine(wechatDir, MmmojoDll); - var mmmojo64FullPath = Path.Combine(wechatDir, Mmmojo64Dll); - var targetMmmojoFullPath = Path.Combine(targetPath, MmmojoDll); - var targetMmmojo64FullPath = Path.Combine(targetPath, Mmmojo64Dll); - if (!File.Exists(targetMmmojoFullPath)) - File.Copy(mmmojoFullPath, targetMmmojoFullPath); - if (!File.Exists(targetMmmojo64FullPath)) - File.Copy(mmmojo64FullPath, targetMmmojo64FullPath); + var ret = true; + error = string.Empty; + var targetPath = AppDomain.CurrentDomain.BaseDirectory; + var targetMmMojoFullPath = Path.Combine(targetPath, MmMojoDll); + var targetMmMojo64FullPath = Path.Combine(targetPath, MmMojo64Dll); + try + { + if (File.Exists(targetMmMojoFullPath)) + File.Delete(targetMmMojoFullPath); + if (File.Exists(targetMmMojo64FullPath)) + File.Delete(targetMmMojo64FullPath); + } + catch (Exception ex) + { + error = ex.Message; + ret = false; + } + + return ret; } } \ No newline at end of file