add local ocr

dev_ocr_translate
DESKTOP-3BO4HSG\ksat 2 years ago
parent 2afd51c783
commit dcaa07680e

@ -30,6 +30,8 @@
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
@ -75,6 +77,9 @@
<Reference Include="System.Xaml"> <Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework> <RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference> </Reference>
<Reference Include="Tesseract, Version=5.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Tesseract.5.2.0\lib\net48\Tesseract.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
@ -171,6 +176,15 @@
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource> </EmbeddedResource>
<None Include="tessdata\chi_sim.traineddata">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="tessdata\chi_sim_vert.traineddata">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="tessdata\eng.traineddata">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Resource Include="Images\iconfont.ttf"> <Resource Include="Images\iconfont.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource> </Resource>
@ -210,4 +224,11 @@
</COMReference> </COMReference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Tesseract.5.2.0\build\Tesseract.targets" Condition="Exists('..\packages\Tesseract.5.2.0\build\Tesseract.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Tesseract.5.2.0\build\Tesseract.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Tesseract.5.2.0\build\Tesseract.targets'))" />
</Target>
</Project> </Project>

@ -12,6 +12,7 @@ using System.Threading.Tasks;
using System.Web; using System.Web;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using Tesseract;
namespace STranslate.Util namespace STranslate.Util
{ {
@ -241,6 +242,33 @@ namespace STranslate.Util
#endregion #endregion
#region Screenshot #region Screenshot
/// <summary>
/// Tesseract 库获取文本
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
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) public static ImageBrush BitmapToImageBrush(Bitmap bmp)
{ {
ImageBrush brush = new ImageBrush(); ImageBrush brush = new ImageBrush();

@ -84,8 +84,11 @@ namespace STranslate.ViewModel
_ScreenShotWin.Close(); _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, (_) => ClosedCmd = new RelayCommand((_) => true, (_) =>
@ -94,6 +97,10 @@ namespace STranslate.ViewModel
}); });
} }
/// <summary>
/// 测试保存
/// </summary>
/// <param name="bmp"></param>
private void TestSaveBmp(Bitmap bmp) private void TestSaveBmp(Bitmap bmp)
{ {
bmp.Save("D:\\a.png", System.Drawing.Imaging.ImageFormat.Bmp); bmp.Save("D:\\a.png", System.Drawing.Imaging.ImageFormat.Bmp);

@ -3,5 +3,6 @@
<package id="Hardcodet.NotifyIcon.Wpf" version="1.1.0" targetFramework="net48" /> <package id="Hardcodet.NotifyIcon.Wpf" version="1.1.0" targetFramework="net48" />
<package id="Microsoft.Xaml.Behaviors.Wpf" version="1.1.39" targetFramework="net48" /> <package id="Microsoft.Xaml.Behaviors.Wpf" version="1.1.39" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" /> <package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" />
<package id="Tesseract" version="5.2.0" targetFramework="net48" />
<package id="WpfScreenHelper" version="2.1.0" targetFramework="net48" /> <package id="WpfScreenHelper" version="2.1.0" targetFramework="net48" />
</packages> </packages>
Loading…
Cancel
Save