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
This commit is contained in:
Robin Krom 2020-02-07 20:34:34 +01:00
commit dcc6407416
4 changed files with 45 additions and 57 deletions

View file

@ -1,20 +1,20 @@
/* /*
* Greenshot - a free and open source screenshot tool * Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
* *
* For more information see: http://getgreenshot.org/ * For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or * the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -89,17 +89,18 @@ namespace GreenshotPlugin.Controls {
private readonly IList<int> _needNonShiftModifier = new List<int>(); private readonly IList<int> _needNonShiftModifier = new List<int>();
private readonly IList<int> _needNonAltGrModifier = new List<int>(); private readonly IList<int> _needNonAltGrModifier = new List<int>();
private readonly ContextMenu _dummy = new ContextMenu(); private readonly ContextMenuStrip _dummy = new ContextMenuStrip();
/// <summary> /// <summary>
/// Used to make sure that there is no right-click menu available /// Used to make sure that there is no right-click menu available
/// </summary> /// </summary>
public override ContextMenu ContextMenu { public override ContextMenuStrip ContextMenuStrip
{
get { get {
return _dummy; return _dummy;
} }
set { set {
base.ContextMenu = _dummy; base.ContextMenuStrip = _dummy;
} }
} }
@ -120,7 +121,7 @@ namespace GreenshotPlugin.Controls {
/// Creates a new HotkeyControl /// Creates a new HotkeyControl
/// </summary> /// </summary>
public HotkeyControl() { public HotkeyControl() {
ContextMenu = _dummy; // Disable right-clicking ContextMenuStrip = _dummy; // Disable right-clicking
Text = "None"; Text = "None";
// Handle events that occurs when keys are pressed // Handle events that occurs when keys are pressed
@ -341,7 +342,7 @@ namespace GreenshotPlugin.Controls {
public override string ToString() { public override string ToString() {
return HotkeyToString(HotkeyModifiers, Hotkey); return HotkeyToString(HotkeyModifiers, Hotkey);
} }
public static string GetLocalizedHotkeyStringFromString(string hotkeyString) { public static string GetLocalizedHotkeyStringFromString(string hotkeyString) {
Keys virtualKeyCode = HotkeyFromString(hotkeyString); Keys virtualKeyCode = HotkeyFromString(hotkeyString);
Keys modifiers = HotkeyModifiersFromString(hotkeyString); Keys modifiers = HotkeyModifiersFromString(hotkeyString);
@ -373,7 +374,7 @@ namespace GreenshotPlugin.Controls {
public static string HotkeyToLocalizedString(Keys modifierKeyCode, Keys virtualKeyCode) { public static string HotkeyToLocalizedString(Keys modifierKeyCode, Keys virtualKeyCode) {
return HotkeyModifiersToLocalizedString(modifierKeyCode) + GetKeyName(virtualKeyCode); return HotkeyModifiersToLocalizedString(modifierKeyCode) + GetKeyName(virtualKeyCode);
} }
public static string HotkeyModifiersToLocalizedString(Keys modifierKeyCode) { public static string HotkeyModifiersToLocalizedString(Keys modifierKeyCode) {
StringBuilder hotkeyString = new StringBuilder(); StringBuilder hotkeyString = new StringBuilder();
if ((modifierKeyCode & Keys.Alt) > 0) { if ((modifierKeyCode & Keys.Alt) > 0) {
@ -468,7 +469,7 @@ namespace GreenshotPlugin.Controls {
return -1; return -1;
} }
} }
public static void UnregisterHotkeys() { public static void UnregisterHotkeys() {
foreach(int hotkey in KeyHandlers.Keys) { foreach(int hotkey in KeyHandlers.Keys) {
UnregisterHotKey(_hotkeyHwnd, hotkey); UnregisterHotKey(_hotkeyHwnd, hotkey);
@ -489,7 +490,7 @@ namespace GreenshotPlugin.Controls {
// Remove key handler // Remove key handler
KeyHandlers.Remove(hotkey); KeyHandlers.Remove(hotkey);
} }
} }
/// <summary> /// <summary>
/// Handle WndProc messages for the hotkey /// Handle WndProc messages for the hotkey
@ -549,7 +550,7 @@ namespace GreenshotPlugin.Controls {
return keyString + " /"; return keyString + " /";
} }
uint scanCode = MapVirtualKey((uint)virtualKey, (uint)MapType.MAPVK_VK_TO_VSC); uint scanCode = MapVirtualKey((uint)virtualKey, (uint)MapType.MAPVK_VK_TO_VSC);
// because MapVirtualKey strips the extended bit for some keys // because MapVirtualKey strips the extended bit for some keys
switch (virtualKey) { switch (virtualKey) {
case Keys.Left: case Keys.Up: case Keys.Right: case Keys.Down: // arrow keys case Keys.Left: case Keys.Up: case Keys.Right: case Keys.Down: // arrow keys

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
namespace System.Runtime.CompilerServices {
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute {}
}

View file

@ -58,6 +58,24 @@ namespace GreenshotWin10Plugin
} }
} }
/// <summary>
/// Scan the surface bitmap for text, and get the OcrResult
/// </summary>
/// <param name="surface">ISurface</param>
/// <returns>OcrResult</returns>
public async Task<OcrResult> 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);
}
/// <summary> /// <summary>
/// Run the Windows 10 OCR engine to process the text on the captured image /// Run the Windows 10 OCR engine to process the text on the captured image
/// </summary> /// </summary>
@ -70,24 +88,14 @@ namespace GreenshotWin10Plugin
var exportInformation = new ExportInformation(Designation, Description); var exportInformation = new ExportInformation(Designation, Description);
try 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(); result.AppendLine(line.Text);
using var imageStream = new MemoryStream(); }
ImageOutput.SaveToStream(surface, imageStream, new SurfaceOutputSettings()); var text = result.ToString();
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;
// Check if we found text // Check if we found text
if (!string.IsNullOrWhiteSpace(text)) if (!string.IsNullOrWhiteSpace(text))

View file

@ -36,6 +36,7 @@ using Greenshot.Plugin;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
using System.Drawing; using System.Drawing;
using GreenshotWin10Plugin.Native; using GreenshotWin10Plugin.Native;
using System.Windows.Media;
namespace GreenshotWin10Plugin namespace GreenshotWin10Plugin
{ {
@ -84,8 +85,12 @@ namespace GreenshotWin10Plugin
{ {
WindowState = WindowState.Normal, WindowState = WindowState.Normal,
WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStartupLocation = WindowStartupLocation.CenterScreen,
WindowStyle = WindowStyle.None,
// TODO: Define right size
Width = 400, Width = 400,
Height = 400 Height = 400,
AllowsTransparency = true,
Background = new SolidColorBrush(Colors.Transparent)
}; };
triggerWindow.Show(); triggerWindow.Show();