diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index c53c735..2e41fe5 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -30,6 +30,8 @@ false true + + AnyCPU @@ -75,6 +77,9 @@ 4.0 + + ..\packages\Tesseract.5.2.0\lib\net48\Tesseract.dll + @@ -171,6 +176,15 @@ ResXFileCodeGenerator Resources.Designer.cs + + Always + + + Always + + + Always + PreserveNewest @@ -210,4 +224,11 @@ + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + \ No newline at end of file diff --git a/STranslate/Util/Util.cs b/STranslate/Util/Util.cs index 77f5b31..a3efbfc 100644 --- a/STranslate/Util/Util.cs +++ b/STranslate/Util/Util.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using System.Web; using System.Windows; using System.Windows.Media; +using Tesseract; namespace STranslate.Util { @@ -241,6 +242,33 @@ namespace STranslate.Util #endregion #region Screenshot + /// + /// Tesseract 库获取文本 + /// + /// + /// + public static string TesseractGetText(Bitmap bmp) + { + try + { + using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) + //using (var engine = new TesseractEngine(@"./tessdata", "chi_sim", EngineMode.Default)) + { + using(var pix = PixConverter.ToPix(bmp)) + { + using (var page = engine.Process(pix)) + { + return page.GetText(); + } + } + } + } + catch (Exception ex) + { + throw ex; + } + } + public static ImageBrush BitmapToImageBrush(Bitmap bmp) { ImageBrush brush = new ImageBrush(); diff --git a/STranslate/ViewModel/ScreenShotVM.cs b/STranslate/ViewModel/ScreenShotVM.cs index b534071..fe20a40 100644 --- a/STranslate/ViewModel/ScreenShotVM.cs +++ b/STranslate/ViewModel/ScreenShotVM.cs @@ -84,8 +84,11 @@ namespace STranslate.ViewModel _ScreenShotWin.Close(); - - TestSaveBmp(bmpOut); + //TestSaveBmp(bmpOut); + + var xx = Util.Util.TesseractGetText(bmpOut).Trim(); + MessageBox.Show(xx); + System.Diagnostics.Debug.Print(xx); }); ClosedCmd = new RelayCommand((_) => true, (_) => @@ -94,6 +97,10 @@ namespace STranslate.ViewModel }); } + /// + /// 测试保存 + /// + /// private void TestSaveBmp(Bitmap bmp) { bmp.Save("D:\\a.png", System.Drawing.Imaging.ImageFormat.Bmp); diff --git a/STranslate/packages.config b/STranslate/packages.config index e2754e3..b123d0f 100644 --- a/STranslate/packages.config +++ b/STranslate/packages.config @@ -3,5 +3,6 @@ + \ No newline at end of file diff --git a/STranslate/tessdata/eng.traineddata b/STranslate/tessdata/eng.traineddata new file mode 100644 index 0000000..f4744c2 Binary files /dev/null and b/STranslate/tessdata/eng.traineddata differ