feat: add screenshot translate

dev_ocr_translate
DESKTOP-3BO4HSG\ksat 2 years ago
parent feef0e3d8b
commit 1ac7c152e2

@ -279,7 +279,7 @@ namespace STranslate.Util
Int32Rect.Empty, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
brush.ImageSource = wpfBitmap; brush.ImageSource = wpfBitmap;
//TODO: flush Memory FlushMemory();
return brush; return brush;
} }
/// <summary> /// <summary>

@ -11,13 +11,13 @@ namespace STranslate.View
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private MainVM vm; private MainVM vm = MainVM.Instance;
public MainWindow() public MainWindow()
{ {
//InitializeComponent(); //InitializeComponent();
DataContext = new MainVM(this); DataContext = vm;
vm = (MainVM)DataContext; vm.Mainwin = this;
//if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict)
//{ //{

@ -40,9 +40,9 @@
</i:Interaction.Triggers> </i:Interaction.Triggers>
<Canvas x:Name="ScreenCanvas"> <Canvas x:Name="ScreenCanvas">
<Rectangle x:Name="LeftMask" Fill="#111410" Opacity="0.6" /> <Rectangle x:Name="LeftMask" Fill="#7f7f7f" Opacity="0.6" />
<Rectangle x:Name="RightMask" Fill="#111410" Opacity="0.6" /> <Rectangle x:Name="RightMask" Fill="#7f7f7f" Opacity="0.6" />
<Rectangle x:Name="UpMask" Fill="#111410" Opacity="0.6" /> <Rectangle x:Name="UpMask" Fill="#7f7f7f" Opacity="0.6" />
<Rectangle x:Name="DownMask" Fill="#111410" Opacity="0.6" /> <Rectangle x:Name="DownMask" Fill="#7f7f7f" Opacity="0.6" />
</Canvas> </Canvas>
</Window> </Window>

@ -14,11 +14,8 @@ namespace STranslate.ViewModel
{ {
public class MainVM : BaseVM public class MainVM : BaseVM
{ {
public MainVM()
public MainVM(Window win)
{ {
_Mainwin = (MainWindow)win;
if (!ReadConfig()) if (!ReadConfig())
{ {
ExitApp(-1); ExitApp(-1);
@ -68,12 +65,12 @@ namespace STranslate.ViewModel
{ {
if (IsTopmost) if (IsTopmost)
{ {
(_Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) (Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button)
.SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _UnTopmostTemplateName); .SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _UnTopmostTemplateName);
} }
else else
{ {
(_Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) (Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button)
.SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _TopmostTemplateName); .SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _TopmostTemplateName);
} }
IsTopmost = !IsTopmost; IsTopmost = !IsTopmost;
@ -82,12 +79,12 @@ namespace STranslate.ViewModel
//ESC //ESC
EscCmd = new RelayCommand((_) => true, (_) => EscCmd = new RelayCommand((_) => true, (_) =>
{ {
_Mainwin.Hide(); Mainwin.Hide();
//取消置顶 //取消置顶
if (IsTopmost) if (IsTopmost)
{ {
(_Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) (Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button)
.SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _UnTopmostTemplateName); .SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _UnTopmostTemplateName);
IsTopmost = !IsTopmost; IsTopmost = !IsTopmost;
} }
@ -109,7 +106,7 @@ namespace STranslate.ViewModel
//移动 //移动
MouseLeftDownCmd = new RelayCommand((_) => true, (_) => MouseLeftDownCmd = new RelayCommand((_) => true, (_) =>
{ {
_Mainwin.DragMove(); Mainwin.DragMove();
}); });
//失去焦点 //失去焦点
DeactivatedCmd = new RelayCommand((_) => true, (_) => DeactivatedCmd = new RelayCommand((_) => true, (_) =>
@ -117,7 +114,7 @@ namespace STranslate.ViewModel
if (!IsTopmost) if (!IsTopmost)
{ {
Speech.SpeakAsyncCancelAll(); Speech.SpeakAsyncCancelAll();
_Mainwin.Hide(); Mainwin.Hide();
} }
}); });
//source speak //source speak
@ -194,9 +191,9 @@ namespace STranslate.ViewModel
/// </summary> /// </summary>
public void OpenMainWin() public void OpenMainWin()
{ {
_Mainwin.Show(); Mainwin.Show();
_Mainwin.Activate(); Mainwin.Activate();
(_Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox).Focus(); (Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox).Focus();
} }
/// <summary> /// <summary>
/// 输入翻译 /// 输入翻译
@ -214,7 +211,7 @@ namespace STranslate.ViewModel
ClearAll(); ClearAll();
var sentence = GetWordsHelper.Get(); var sentence = GetWordsHelper.Get();
OpenMainWin(); OpenMainWin();
(_Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox) (Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox)
.Text = sentence.Trim(); .Text = sentence.Trim();
_ = Translate(); _ = Translate();
} }
@ -223,10 +220,18 @@ namespace STranslate.ViewModel
/// </summary> /// </summary>
public void ScreenShotTranslate() public void ScreenShotTranslate()
{ {
//MessageBox.Show("aaa"); var screen = new ScreenShotWindow();
var xx = new ScreenShotWindow(); screen.Show();
xx.Show(); screen.Activate();
xx.Activate(); }
/// <summary>
/// 截屏翻译Ex
/// </summary>
public void ScreenShotTranslateEx(string text)
{
InputTranslate();
(Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox).Text = text;
_ = Translate();
} }
/// <summary> /// <summary>
@ -434,10 +439,20 @@ namespace STranslate.ViewModel
/// </summary> /// </summary>
private bool _IsStartup; private bool _IsStartup;
public bool IsStartup { get => _IsStartup; set => UpdateProperty(ref _IsStartup, value); } public bool IsStartup { get => _IsStartup; set => UpdateProperty(ref _IsStartup, value); }
/// <summary>
/// 托盘图标可见
/// </summary>
private bool _IsVisibility = true; private bool _IsVisibility = true;
public bool IsVisibility { get => _IsVisibility; set => UpdateProperty(ref _IsVisibility, value); } public bool IsVisibility { get => _IsVisibility; set => UpdateProperty(ref _IsVisibility, value); }
private MainWindow _Mainwin; /// <summary>
/// view传递至viewmodel
/// </summary>
public MainWindow Mainwin;
private static MainVM _Instance;
public static MainVM Instance => _Instance ?? (_Instance = new MainVM());
public bool IsTopmost { get; set; } public bool IsTopmost { get; set; }
private readonly string _TopmostTemplateName = "ButtonTemplateTopmost"; private readonly string _TopmostTemplateName = "ButtonTemplateTopmost";
private readonly string _UnTopmostTemplateName = "ButtonTemplateUnTopmost"; private readonly string _UnTopmostTemplateName = "ButtonTemplateUnTopmost";

@ -86,9 +86,9 @@ namespace STranslate.ViewModel
//TestSaveBmp(bmpOut); //TestSaveBmp(bmpOut);
var xx = Util.Util.TesseractGetText(bmpOut).Trim(); var getText = Util.Util.TesseractGetText(bmpOut).Trim();
MessageBox.Show(xx);
System.Diagnostics.Debug.Print(xx); MainVM.Instance.ScreenShotTranslateEx(getText);
}); });
ClosedCmd = new RelayCommand((_) => true, (_) => ClosedCmd = new RelayCommand((_) => true, (_) =>

Loading…
Cancel
Save