Fixed the build order, so we can build the installer.

Moved UnobservedTaskException handling from the Jira plugin to the MainForm.
Introduced an interface for doing the OCR, to enable building some functionality into the editor.
This commit is contained in:
Robin Krom 2020-02-08 23:23:19 +01:00
parent 6d692dd3a5
commit 2293c70610
8 changed files with 184 additions and 117 deletions

View file

@ -4,6 +4,21 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Greenshot", "Greenshot\Greenshot.csproj", "{CD642BF4-D815-4D67-A0B5-C69F0B8231AF}"
ProjectSection(ProjectDependencies) = postProject
{92599C09-FF29-4ABD-B6E6-C48ECD781BAB} = {92599C09-FF29-4ABD-B6E6-C48ECD781BAB}
{19FEEF09-313F-43C7-819D-F1BCA782B08B} = {19FEEF09-313F-43C7-819D-F1BCA782B08B}
{9801F62C-540F-4BFE-9211-6405DEDE563B} = {9801F62C-540F-4BFE-9211-6405DEDE563B}
{9C0ECC4C-7807-4111-916A-4F57BB29788A} = {9C0ECC4C-7807-4111-916A-4F57BB29788A}
{C3052651-598A-44E2-AAB3-2E41311D50F9} = {C3052651-598A-44E2-AAB3-2E41311D50F9}
{7EC72A5A-D73A-4B4B-9CA1-2216C7D92D5E} = {7EC72A5A-D73A-4B4B-9CA1-2216C7D92D5E}
{697CF066-9077-4F22-99D9-D989CCE7282B} = {697CF066-9077-4F22-99D9-D989CCE7282B}
{47F23C86-604E-4CC3-8767-B3D4088F30BB} = {47F23C86-604E-4CC3-8767-B3D4088F30BB}
{80D8DEB9-94E3-4876-8CCA-2DF1ED5F2C50} = {80D8DEB9-94E3-4876-8CCA-2DF1ED5F2C50}
{D61E6ECE-E0B6-4467-B492-F08A06BA8F02} = {D61E6ECE-E0B6-4467-B492-F08A06BA8F02}
{AD7CFFE2-40E7-46CF-A172-D48CF7AE9A12} = {AD7CFFE2-40E7-46CF-A172-D48CF7AE9A12}
{1893A2E4-A78A-4713-A8E7-E70058DABEE0} = {1893A2E4-A78A-4713-A8E7-E70058DABEE0}
{C6988EE8-2FEE-4349-9F09-F9628A0D8965} = {C6988EE8-2FEE-4349-9F09-F9628A0D8965}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GreenshotPlugin", "GreenshotPlugin\GreenshotPlugin.csproj", "{5B924697-4DCD-4F98-85F1-105CB84B7341}"
EndProject

View file

@ -45,6 +45,7 @@ using Greenshot.Destinations;
using Greenshot.Drawing;
using log4net;
using Timer = System.Timers.Timer;
using System.Threading.Tasks;
namespace Greenshot {
/// <summary>
@ -70,6 +71,8 @@ namespace Greenshot {
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += Task_UnhandledException;
// Initialize the IniConfig
IniConfig.Init();
@ -1217,6 +1220,20 @@ namespace Greenshot {
InitializeQuickSettingsMenu();
}
private static void Task_UnhandledException(object sender, UnobservedTaskExceptionEventArgs args)
{
try {
Exception exceptionToLog = args.Exception;
string exceptionText = EnvironmentInfo.BuildReport(exceptionToLog);
LOG.Error("Exception caught in the UnobservedTaskException handler.");
LOG.Error(exceptionText);
new BugReportForm(exceptionText).ShowDialog();
}
finally {
args.SetObserved();
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
Exception exceptionToLog = e.ExceptionObject as Exception;
string exceptionText = EnvironmentInfo.BuildReport(exceptionToLog);

View file

@ -12,7 +12,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GreenshotPlugin\GreenshotPlugin.csproj" />
<ProjectReference Include="..\Greenshot\Greenshot.csproj" />
<PackageReference Include="Dapplo.Jira" version="0.5.12" />
<PackageReference Include="Svg" version="2.3.0" />
</ItemGroup>

View file

@ -25,8 +25,6 @@ using Greenshot.Plugin;
using System;
using System.Threading.Tasks;
using Dapplo.Log;
using Greenshot.Forms;
using Greenshot.Helpers;
using GreenshotJiraPlugin.Forms;
using GreenshotPlugin.Core;
using log4net;
@ -59,22 +57,6 @@ namespace GreenshotJiraPlugin {
public JiraPlugin() {
_instance = this;
// Added to prevent Greenshot from shutting down when there was an exception in a Task
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
try
{
Exception exceptionToLog = args.Exception;
string exceptionText = EnvironmentInfo.BuildReport(exceptionToLog);
Log.Error("Exception caught in the UnobservedTaskException handler.");
Log.Error(exceptionText);
new BugReportForm(exceptionText).ShowDialog();
}
finally
{
args.SetObserved();
}
};
}
public IEnumerable<IDestination> Destinations() {

View file

@ -0,0 +1,40 @@
/*
* 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/>.
*/
using Greenshot.Plugin;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
namespace GreenshotPlugin.Interfaces
{
public interface IOcrProvider
{
Task<OcrInformation> DoOcrAsync(ISurface surface);
}
public class OcrInformation
{
public string Text { get; set; }
public IList<(string word, Rectangle location)> Words { get; } = new List<(string, Rectangle)>();
}
}

View file

@ -28,13 +28,14 @@ using Windows.Media.Ocr;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using System.Text;
using GreenshotPlugin.Interfaces;
namespace GreenshotWin10Plugin
{
/// <summary>
/// This uses the OcrEngine from Windows 10 to perform OCR on the captured image.
/// </summary>
public class Win10OcrDestination : AbstractDestination
public class Win10OcrDestination : AbstractDestination, IOcrProvider
{
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Win10OcrDestination));
@ -63,7 +64,7 @@ namespace GreenshotWin10Plugin
/// </summary>
/// <param name="surface">ISurface</param>
/// <returns>OcrResult</returns>
public async Task<OcrResult> DoOcrAsync(ISurface surface)
public async Task<OcrInformation> DoOcrAsync(ISurface surface)
{
var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages();
using var imageStream = new MemoryStream();
@ -73,7 +74,27 @@ namespace GreenshotWin10Plugin
var decoder = await BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream());
var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
return await ocrEngine.RecognizeAsync(softwareBitmap);
var ocrResult = await ocrEngine.RecognizeAsync(softwareBitmap);
var result = new OcrInformation();
// Build the text from the lines, otherwise it's just everything concated together
var text = new StringBuilder();
foreach (var line in ocrResult.Lines)
{
text.AppendLine(line.Text);
}
result.Text = result.ToString();
foreach (var line in ocrResult.Lines)
{
foreach (var word in line.Words)
{
var rectangle = new Rectangle((int)word.BoundingRect.X, (int)word.BoundingRect.Y, (int)word.BoundingRect.Width, (int)word.BoundingRect.Height);
result.Words.Add((word.Text, rectangle));
}
}
return result;
}
/// <summary>
@ -89,19 +110,12 @@ namespace GreenshotWin10Plugin
try
{
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);
}
var text = result.ToString();
// Check if we found text
if (!string.IsNullOrWhiteSpace(text))
if (!string.IsNullOrWhiteSpace(ocrResult.Text))
{
// Place the OCR text on the
ClipboardHelper.SetClipboardData(text);
ClipboardHelper.SetClipboardData(ocrResult.Text);
}
exportInformation.ExportMade = true;
}