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

@ -89,17 +89,18 @@ namespace GreenshotPlugin.Controls {
private readonly IList<int> _needNonShiftModifier = new List<int>();
private readonly IList<int> _needNonAltGrModifier = new List<int>();
private readonly ContextMenu _dummy = new ContextMenu();
private readonly ContextMenuStrip _dummy = new ContextMenuStrip();
/// <summary>
/// Used to make sure that there is no right-click menu available
/// </summary>
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
/// </summary>
public HotkeyControl() {
ContextMenu = _dummy; // Disable right-clicking
ContextMenuStrip = _dummy; // Disable right-clicking
Text = "None";
// Handle events that occurs when keys are pressed

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>
/// Run the Windows 10 OCR engine to process the text on the captured image
/// </summary>
@ -70,24 +88,14 @@ namespace GreenshotWin10Plugin
var exportInformation = new ExportInformation(Designation, Description);
try
{
var text = Task.Run(async () =>
{
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 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)
{
result.AppendLine(line.Text);
}
return result.ToString();
}).Result;
var text = result.ToString();
// Check if we found text
if (!string.IsNullOrWhiteSpace(text))

View file

@ -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();