From dcc6407416a529670889892e6bab16bbb5820a45 Mon Sep 17 00:00:00 2001 From: Robin Krom Date: Fri, 7 Feb 2020 20:34:34 +0100 Subject: [PATCH] Moved OCR functionality to a single method. Removed the weird window behind the sharing window. Removed the ExtensionAttribute, this was for .NET 2.0 / 3.5 Removed a deprecated usage of the ContextMenu in favor of the ContextMenuStrip --- GreenshotPlugin/Controls/HotkeyControl.cs | 27 ++++++------ GreenshotPlugin/Core/ExtensionAttribute.cs | 26 ------------ GreenshotWin10Plugin/Win10OcrDestination.cs | 42 +++++++++++-------- GreenshotWin10Plugin/Win10ShareDestination.cs | 7 +++- 4 files changed, 45 insertions(+), 57 deletions(-) delete mode 100644 GreenshotPlugin/Core/ExtensionAttribute.cs diff --git a/GreenshotPlugin/Controls/HotkeyControl.cs b/GreenshotPlugin/Controls/HotkeyControl.cs index 69eb825e3..62969d212 100644 --- a/GreenshotPlugin/Controls/HotkeyControl.cs +++ b/GreenshotPlugin/Controls/HotkeyControl.cs @@ -1,20 +1,20 @@ /* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom - * + * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -89,17 +89,18 @@ namespace GreenshotPlugin.Controls { private readonly IList _needNonShiftModifier = new List(); private readonly IList _needNonAltGrModifier = new List(); - private readonly ContextMenu _dummy = new ContextMenu(); + private readonly ContextMenuStrip _dummy = new ContextMenuStrip(); /// /// Used to make sure that there is no right-click menu available /// - public override ContextMenu ContextMenu { + public override ContextMenuStrip ContextMenuStrip + { get { return _dummy; } set { - base.ContextMenu = _dummy; + base.ContextMenuStrip = _dummy; } } @@ -120,7 +121,7 @@ namespace GreenshotPlugin.Controls { /// Creates a new HotkeyControl /// public HotkeyControl() { - ContextMenu = _dummy; // Disable right-clicking + ContextMenuStrip = _dummy; // Disable right-clicking Text = "None"; // Handle events that occurs when keys are pressed @@ -341,7 +342,7 @@ namespace GreenshotPlugin.Controls { public override string ToString() { return HotkeyToString(HotkeyModifiers, Hotkey); } - + public static string GetLocalizedHotkeyStringFromString(string hotkeyString) { Keys virtualKeyCode = HotkeyFromString(hotkeyString); Keys modifiers = HotkeyModifiersFromString(hotkeyString); @@ -373,7 +374,7 @@ namespace GreenshotPlugin.Controls { public static string HotkeyToLocalizedString(Keys modifierKeyCode, Keys virtualKeyCode) { return HotkeyModifiersToLocalizedString(modifierKeyCode) + GetKeyName(virtualKeyCode); } - + public static string HotkeyModifiersToLocalizedString(Keys modifierKeyCode) { StringBuilder hotkeyString = new StringBuilder(); if ((modifierKeyCode & Keys.Alt) > 0) { @@ -468,7 +469,7 @@ namespace GreenshotPlugin.Controls { return -1; } } - + public static void UnregisterHotkeys() { foreach(int hotkey in KeyHandlers.Keys) { UnregisterHotKey(_hotkeyHwnd, hotkey); @@ -489,7 +490,7 @@ namespace GreenshotPlugin.Controls { // Remove key handler KeyHandlers.Remove(hotkey); } - } + } /// /// Handle WndProc messages for the hotkey @@ -549,7 +550,7 @@ namespace GreenshotPlugin.Controls { return keyString + " /"; } uint scanCode = MapVirtualKey((uint)virtualKey, (uint)MapType.MAPVK_VK_TO_VSC); - + // because MapVirtualKey strips the extended bit for some keys switch (virtualKey) { case Keys.Left: case Keys.Up: case Keys.Right: case Keys.Down: // arrow keys diff --git a/GreenshotPlugin/Core/ExtensionAttribute.cs b/GreenshotPlugin/Core/ExtensionAttribute.cs deleted file mode 100644 index 2e47f4962..000000000 --- a/GreenshotPlugin/Core/ExtensionAttribute.cs +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Greenshot - a free and open source screenshot tool - * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom - * - * For more information see: http://getgreenshot.org/ - * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -namespace System.Runtime.CompilerServices { - [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] - public sealed class ExtensionAttribute : Attribute {} -} diff --git a/GreenshotWin10Plugin/Win10OcrDestination.cs b/GreenshotWin10Plugin/Win10OcrDestination.cs index 6aeb5c7ac..c21f6e966 100644 --- a/GreenshotWin10Plugin/Win10OcrDestination.cs +++ b/GreenshotWin10Plugin/Win10OcrDestination.cs @@ -58,6 +58,24 @@ namespace GreenshotWin10Plugin } } + /// + /// Scan the surface bitmap for text, and get the OcrResult + /// + /// ISurface + /// OcrResult + public async Task DoOcrAsync(ISurface surface) + { + var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages(); + using var imageStream = new MemoryStream(); + ImageOutput.SaveToStream(surface, imageStream, new SurfaceOutputSettings()); + imageStream.Position = 0; + + var decoder = await BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream()); + var softwareBitmap = await decoder.GetSoftwareBitmapAsync(); + + return await ocrEngine.RecognizeAsync(softwareBitmap); + } + /// /// Run the Windows 10 OCR engine to process the text on the captured image /// @@ -70,24 +88,14 @@ namespace GreenshotWin10Plugin var exportInformation = new ExportInformation(Designation, Description); try { - var text = Task.Run(async () => + var ocrResult = Task.Run(async () => await DoOcrAsync(surface)).Result; + // Build the text from the lines, otherwise it's just everything concated together + var result = new StringBuilder(); + foreach(var line in ocrResult.Lines) { - var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages(); - using var imageStream = new MemoryStream(); - ImageOutput.SaveToStream(surface, imageStream, new SurfaceOutputSettings()); - imageStream.Position = 0; - - var decoder = await BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream()); - var softwareBitmap = await decoder.GetSoftwareBitmapAsync(); - - var ocrResult = await ocrEngine.RecognizeAsync(softwareBitmap); - var result = new StringBuilder(); - foreach(var line in ocrResult.Lines) - { - result.AppendLine(line.Text); - } - return result.ToString(); - }).Result; + result.AppendLine(line.Text); + } + var text = result.ToString(); // Check if we found text if (!string.IsNullOrWhiteSpace(text)) diff --git a/GreenshotWin10Plugin/Win10ShareDestination.cs b/GreenshotWin10Plugin/Win10ShareDestination.cs index cb6d3f3cc..2a86b585b 100644 --- a/GreenshotWin10Plugin/Win10ShareDestination.cs +++ b/GreenshotWin10Plugin/Win10ShareDestination.cs @@ -36,6 +36,7 @@ using Greenshot.Plugin; using GreenshotPlugin.Core; using System.Drawing; using GreenshotWin10Plugin.Native; +using System.Windows.Media; namespace GreenshotWin10Plugin { @@ -84,8 +85,12 @@ namespace GreenshotWin10Plugin { WindowState = WindowState.Normal, WindowStartupLocation = WindowStartupLocation.CenterScreen, + WindowStyle = WindowStyle.None, + // TODO: Define right size Width = 400, - Height = 400 + Height = 400, + AllowsTransparency = true, + Background = new SolidColorBrush(Colors.Transparent) }; triggerWindow.Show();