OCR Code cleanup, preparing for adding the word-rectangles to assist other Greenshot functionality (like blur/pixelate etc)

This commit is contained in:
Robin 2016-10-01 22:22:32 +02:00
parent 243a3e1418
commit cab0f21e26
19 changed files with 672 additions and 111 deletions

View file

@ -19,6 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Remoting; using System.Runtime.Remoting;
@ -79,13 +80,16 @@ namespace Greenshot.Interop {
try { try {
comObject = Marshal.GetActiveObject(progId); comObject = Marshal.GetActiveObject(progId);
} catch (COMException comE) { } catch (COMException comE) {
if (comE.ErrorCode == MK_E_UNAVAILABLE) { if (comE.ErrorCode == MK_E_UNAVAILABLE)
//LOG.DebugFormat("No current instance of {0} object available.", progId); {
} else if (comE.ErrorCode == CO_E_CLASSSTRING) { Debug.WriteLine($"No current instance of {progId} object available.");
//LOG.WarnFormat("Unknown progId {0}", progId);
} }
} catch (Exception) { else if (comE.ErrorCode == CO_E_CLASSSTRING)
//LOG.Warn("Error getting active object for " + progId, e); {
Debug.WriteLine($"Unknown progId {progId}");
}
} catch (Exception ex) {
Debug.WriteLine($"Error getting active object for {progId} {ex.Message}");
} }
if (comObject != null) { if (comObject != null) {
@ -123,30 +127,32 @@ namespace Greenshot.Interop {
try { try {
comObject = Marshal.GetActiveObject(progId); comObject = Marshal.GetActiveObject(progId);
} catch (COMException comE) { } catch (COMException comE) {
if (comE.ErrorCode == MK_E_UNAVAILABLE) { if (comE.ErrorCode == MK_E_UNAVAILABLE)
//LOG.DebugFormat("No current instance of {0} object available.", progId); {
} else if (comE.ErrorCode == CO_E_CLASSSTRING) { Debug.WriteLine($"No current instance of {progId} object available.");
//LOG.WarnFormat("Unknown progId {0} (application not installed)", progId);
return default(T);
} }
} catch (Exception) { else if (comE.ErrorCode == CO_E_CLASSSTRING)
//LOG.Warn("Error getting active object for " + progId, e); {
Debug.WriteLine($"Unknown progId {progId}");
}
} catch (Exception ex) {
Debug.WriteLine($"Error getting active object for {progId} {ex.Message}");
} }
// Did we get the current instance? If not, try to create a new // Did we get the current instance? If not, try to create a new
if (comObject == null) { if (comObject == null) {
try { try {
comType = Type.GetTypeFromProgID(progId, true); comType = Type.GetTypeFromProgID(progId, true);
} catch (Exception) { } catch (Exception) {
//LOG.Warn("Error type for " + progId, ex); Debug.WriteLine($"Error getting type for {progId}");
} }
if (comType != null) { if (comType != null) {
try { try {
comObject = Activator.CreateInstance(comType); comObject = Activator.CreateInstance(comType);
if (comObject != null) { if (comObject != null) {
//LOG.DebugFormat("Created new instance of {0} object.", progId); Debug.WriteLine($"Created new instance of {progId} object.");
} }
} catch (Exception) { } catch (Exception ex) {
//LOG.Warn("Error creating object for " + progId, e); Debug.WriteLine($"Error creating object for {progId} {ex.Message}");
} }
} }
} }
@ -225,9 +231,9 @@ namespace Greenshot.Interop {
while (Marshal.ReleaseComObject(_comObject) > 0) while (Marshal.ReleaseComObject(_comObject) > 0)
{ {
} }
} catch (Exception) { } catch (Exception ex) {
//LOG.WarnFormat("Problem releasing {0}", _COMType); Debug.WriteLine($"Problem releasing {_comType}");
//LOG.Warn("Error: ", ex); Debug.WriteLine("Error: " + ex);
} }
} }
} }
@ -318,8 +324,12 @@ namespace Greenshot.Interop {
IMethodCallMessage callMessage = myMessage as IMethodCallMessage; IMethodCallMessage callMessage = myMessage as IMethodCallMessage;
MethodInfo method = callMessage?.MethodBase as MethodInfo; MethodInfo method = callMessage?.MethodBase as MethodInfo;
if (method == null) { if (method == null)
//LOG.DebugFormat("Unrecognized Invoke call: {0}", callMessage.MethodBase.ToString()); {
if (callMessage != null)
{
Debug.WriteLine($"Unrecognized Invoke call: {callMessage.MethodBase}");
}
return null; return null;
} }

View file

@ -18,6 +18,7 @@
* 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/>.
*/ */
using System; using System;
namespace Greenshot.Interop { namespace Greenshot.Interop {

View file

@ -53,10 +53,21 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
<Compile Include="Modi\ICommon.cs" />
<Compile Include="Modi\CompressionLevel.cs" />
<Compile Include="ComProgIdAttribute.cs" /> <Compile Include="ComProgIdAttribute.cs" />
<Compile Include="COMWrapper.cs" /> <Compile Include="COMWrapper.cs" />
<Compile Include="IDispatch.cs" /> <Compile Include="Modi\FileFormat.cs" />
<Compile Include="ModiInterop.cs" /> <Compile Include="Modi\IDispatch.cs" />
<Compile Include="Modi\IMiRect.cs" />
<Compile Include="Modi\IImage.cs" />
<Compile Include="Modi\IMiRects.cs" />
<Compile Include="Modi\IImages.cs" />
<Compile Include="Modi\ILayout.cs" />
<Compile Include="Modi\IDocument.cs" />
<Compile Include="Modi\ModiLanguage.cs" />
<Compile Include="Modi\IWord.cs" />
<Compile Include="Modi\IWords.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>

View file

@ -1,12 +0,0 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.CustomMarshalers;
namespace Greenshot.Interop {
[ComImport, Guid("00020400-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDispatch {
void Reserved();
[PreserveSig]
int GetTypeInfo(uint nInfo, int lcid, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TypeToTypeInfoMarshaler))] out Type typeInfo);
}
}

View file

@ -0,0 +1,29 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 GreenshotOCRCommand.Modi
{
public enum CompressionLevel {
miCOMP_LEVEL_LOW = 0,
miCOMP_LEVEL_MEDIUM = 1,
miCOMP_LEVEL_HIGH = 2
}
}

View file

@ -0,0 +1,30 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 GreenshotOCR
{
public enum FileFormat {
miFILE_FORMAT_DEFAULTVALUE = -1,
miFILE_FORMAT_TIFF = 1,
miFILE_FORMAT_TIFF_LOSSLESS = 2,
miFILE_FORMAT_MDI = 4
}
}

View file

@ -0,0 +1,32 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 System;
namespace GreenshotOCRCommand.Modi
{
/// <summary>
/// Base class for the common properties of the Modi interfaces
/// </summary>
public interface ICommon : IDisposable {
IDocument Application { get; }
}
}

View file

@ -0,0 +1,33 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.CustomMarshalers;
namespace GreenshotOCRCommand.Modi {
[ComImport, Guid("00020400-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDispatch {
void Reserved();
[PreserveSig]
int GetTypeInfo(uint nInfo, int lcid, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TypeToTypeInfoMarshaler))] out Type typeInfo);
}
}

View file

@ -0,0 +1,82 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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.Interop;
using GreenshotOCR;
namespace GreenshotOCRCommand.Modi {
/// <summary>
/// The MODI Document object represents an ordered collection of document images saved as a single file.
/// You can use the Create method to load an existing MDI or TIF file, or to create an empty document that you can populate with images from other documents.
/// The OCR method performs OCR on all pages in the document, and the OnOCRProgress event reports the status of the operation and allows the user to cancel it.
/// The Dirty property lets you know whether your document has unsaved OCR results or changes.
/// The SaveAs method allows you to specify an image file format and a compression level.
/// You can also use the PrintOut method to print the document to a printer or a file.
/// </summary>
[ComProgId("MODI.Document")]
public interface IDocument : ICommon {
/// <summary>
/// Closes the document.
/// </summary>
/// <param name="saveCall"></param>
void Close(bool saveCall);
/// <summary>
/// The document's collection of pages.
/// </summary>
IImages Images
{
get;
}
/// <summary>
/// Occurs periodically during an optical character recognition (OCR) operation. Returns the estimated percentage of the OCR operation that is complete, and allows the user to cancel the operation.
/// </summary>
// event OnOCRProgress { get; }
/// <summary>
/// Indicates whether the active document has unsaved changes.
/// </summary>
bool Dirty { get; }
/// <summary>
/// Creates a new document.
/// </summary>
/// <param name="file">Optional String. The path and filename of the optional document file that is to be loaded into the new document.</param>
void Create(string file);
/// <summary>
/// Performs optical character recognition (OCR) on the specified document or image.
/// </summary>
/// <param name="language">ModiLanguage</param>
/// <param name="orientimage">Optional Boolean. Specifies whether the OCR engine attempts to determine the orientation of the page. Default is true.</param>
/// <param name="straightenImage">Optional Boolean. Specifies whether the OCR engine attempts to "de-skew" the page to correct for small angles of misalignment from the vertical. Default is true.</param>
void OCR(ModiLanguage language, bool orientimage, bool straightenImage);
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
/// <param name="fileFormat"></param>
/// <param name="compressionLevel"></param>
void SaveAs(string filename, FileFormat fileFormat, CompressionLevel compressionLevel);
}
}

View file

@ -0,0 +1,41 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 GreenshotOCRCommand.Modi
{
/// <summary>
/// Describes the page in a scan
/// </summary>
public interface IImage : ICommon {
ILayout Layout {
get;
}
long BitsPerPixel { get; }
CompressionLevel Compression { get; }
//IPictureDisp Picture { get; }
int PixelHeight { get; }
int PixelWidth { get; }
//IPictureDisp Thumbnail { get; }
int XDPI { get; }
int YDPI { get; }
}
}

View file

@ -0,0 +1,41 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 System.Collections;
namespace GreenshotOCRCommand.Modi
{
/// <summary>
/// Use the Images accessor property of the Document object to return an Images collection.
/// Use the Item property of the Images collection to return an Image object and gain access to its OCR Layout
/// (including recognized Text and Words), the properties that describe its dimensions and format (BitsPerPixel, Compression, PixelHeight, PixelWidth, XDPI, and YDPI),
/// and its Picture and Thumbnail images.
/// </summary>
public interface IImages : ICommon, IEnumerable {
int Count {
get;
}
IImage this [int index] {
get;
}
new IEnumerator GetEnumerator();
}
}

View file

@ -0,0 +1,55 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 GreenshotOCRCommand.Modi
{
/// <summary>
/// Layout of the IImage
/// </summary>
public interface ILayout : ICommon {
/// <summary>
/// Returns the recognized text as a Unicode string.
/// </summary>
string Text {
get;
}
/// <summary>
/// An accessor property that returns the Words collection recognized in the text during an optical character recognition (OCR) operation.
/// </summary>
IWords Words { get; }
/// <summary>
/// Returns the number of characters in the recognized text.
/// </summary>
int NumChars { get; }
/// <summary>
/// Returns the number of words in the recognized text.
/// </summary>
int NumWords { get; }
/// <summary>
/// Returns the language identifier for the recognized text. Read-only Long.
/// </summary>
ModiLanguage Language { get; }
}
}

View file

@ -0,0 +1,48 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 GreenshotOCRCommand.Modi
{
/// <summary>
/// Represents a bounding rectangle in the optical character recognition (OCR) layout.
/// </summary>
public interface IMiRect : ICommon {
/// <summary>
/// The Bottom property represent the distance in pixels from the top edge of the containing image.
/// </summary>
int Bottom { get; }
/// <summary>
/// The Left property represent the distance in pixels from the left edge of the containing image.
/// </summary>
int Left { get; }
/// <summary>
/// The Right property represent the distance in pixels from the left edge of the containing image.
/// </summary>
int Right { get; }
/// <summary>
/// The Top property represent the distance in pixels from the top edge of the containing image.
/// </summary>
int Top { get; }
}
}

View file

@ -0,0 +1,38 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 System.Collections;
namespace GreenshotOCRCommand.Modi
{
/// <summary>
/// Represents the collection of bounding rectangles in the optical character recognition (OCR) layout. A collection of MiRect objects.
/// </summary>
public interface IMiRects : ICommon, IEnumerable {
int Count {
get;
}
IMiRect this [int index] {
get;
}
new IEnumerator GetEnumerator();
}
}

View file

@ -0,0 +1,65 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 GreenshotOCRCommand.Modi
{
/// <summary>
/// Represents a word recognized in the text during an optical character recognition (OCR) operation.
/// </summary>
public interface IWord : ICommon
{
/// <summary>
/// Returns the index of the specified word in the Words collection of the Layout or IMiSelectableItem object.
/// </summary>
long Id { get; }
/// <summary>
/// Returns the number of the region in the optical character recognition (OCR) layout where the word occurs.
/// </summary>
long RegionId { get; }
/// <summary>
/// Returns the number of the line in the optical character recognition (OCR) layout where the word occurs.
/// </summary>
long LineId { get; }
/// <summary>
/// Returns the recognized text as a Unicode string.
/// </summary>
string Text { get; }
/// <summary>
/// Returns the relative confidence factor reported by the optical character recognition (OCR) engine (on a scale of 0 to 999) after recognizing the specified word.
/// </summary>
short RecognitionConfidence { get; }
/// <summary>
/// Returns the index of the font used by the specified wordthis is the font that was recognized in the text during an optical character recognition (OCR) operation.
/// </summary>
long FontId { get; }
/// <summary>
/// Rectangles
/// </summary>
IMiRects Rects { get; }
}
}

View file

@ -0,0 +1,43 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 System.Collections;
namespace GreenshotOCRCommand.Modi
{
/// <summary>
/// The Words collection recognized in the text during an optical character recognition (OCR) operation.
/// </summary>
public interface IWords : ICommon, IEnumerable
{
int Count
{
get;
}
IWord this[int index]
{
get;
}
new IEnumerator GetEnumerator();
}
}

View file

@ -18,47 +18,9 @@
* 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/>.
*/ */
using System;
using System.Collections;
using Greenshot.Interop;
namespace GreenshotOCR {
[ComProgId("MODI.Document")]
public interface ModiDocu : Common {
void Close(bool SaveCall);
IImages Images{
get;
}
void Create(string file);
void OCR(ModiLanguage language, bool Orientimage, bool StraightenImage);
void SaveAs(string filename, FileFormat fileFormat, CompressionLevel compressionLevel);
}
public interface Common : IDisposable {
ModiDocu Application { get; }
}
public interface ILayout : Common{
string Text {
get;
}
}
public interface IImage : Common {
ILayout Layout {
get;
}
}
public interface IImages : Common, IEnumerable{
int Count {
get;
}
IImage this [int index] {
get;
}
new IEnumerator GetEnumerator();
}
namespace GreenshotOCRCommand.Modi
{
public enum ModiLanguage { public enum ModiLanguage {
CHINESE_SIMPLIFIED = 2052, CHINESE_SIMPLIFIED = 2052,
CHINESE_TRADITIONAL = 1028, CHINESE_TRADITIONAL = 1028,
@ -83,16 +45,4 @@ namespace GreenshotOCR {
TURKISH = 31, TURKISH = 31,
SYSDEFAULT = 2048 SYSDEFAULT = 2048
} }
public enum CompressionLevel {
miCOMP_LEVEL_LOW = 0,
miCOMP_LEVEL_MEDIUM = 1,
miCOMP_LEVEL_HIGH = 2
}
public enum FileFormat {
miFILE_FORMAT_DEFAULTVALUE = -1,
miFILE_FORMAT_TIFF = 1,
miFILE_FORMAT_TIFF_LOSSLESS = 2,
miFILE_FORMAT_MDI = 4
}
} }

View file

@ -1,16 +1,37 @@
using System; /*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2016 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 System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using Greenshot.Interop; using Greenshot.Interop;
using GreenshotOCR; using GreenshotOCRCommand.Modi;
namespace GreenshotOCRCommand { namespace GreenshotOCRCommand {
public class Program { public class Program {
private const string USAGE = "<-c> | <path to image.bmp> [language] [orientimage] [straightenImage]"; private const string Usage = "<-c> | <path to image.bmp> [language] [orientimage] [straightenImage]";
public static int Main(string[] args) { public static int Main(string[] args) {
// to test
//args = new string[] { @"C:\localdata\test.bmp"};
if (args.Length == 0) { if (args.Length == 0) {
Console.WriteLine(USAGE); Console.WriteLine(Usage);
return -1; return -1;
} }
string filename = args[0]; string filename = args[0];
@ -28,25 +49,43 @@ namespace GreenshotOCRCommand {
} }
try { try {
if (File.Exists(filename) || "-c".Equals(filename)) { if (File.Exists(filename) || "-c".Equals(filename)) {
using (ModiDocu modiDocument = COMWrapper.GetOrCreateInstance<ModiDocu>()) { using (var document = COMWrapper.GetOrCreateInstance<IDocument>()) {
if (modiDocument == null) { if (document == null) {
Console.WriteLine("MODI not installed"); Console.WriteLine("MODI not installed");
return -2; return -2;
} }
if ("-c".Equals(filename)) { if ("-c".Equals(filename)) {
return 0; return 0;
} }
modiDocument.Create(filename); document.Create(filename);
modiDocument.OCR(language, orientimage, straightenImage); document.OCR(language, orientimage, straightenImage);
IImage modiImage = modiDocument.Images[0]; var modiImage = document.Images[0];
ILayout layout = modiImage.Layout; var layout = modiImage.Layout;
if (layout.Text != null) if (layout != null)
{ {
// For for BUG-1884: #if DEBUG
// Although this is done in the OCR Plugin, it does make sense in the command too. if (layout.Words != null)
Console.WriteLine(layout.Text.Trim()); {
foreach (var word in ToEnumerable(layout.Words))
{
if (word.Rects != null)
{
foreach (var rect in ToEnumerable(word.Rects))
{
Debug.WriteLine($"Rect {rect.Left},{rect.Top},{rect.Right},{rect.Bottom} - Word {word.Text} : Confidence: {word.RecognitionConfidence}");
}
}
}
}
#endif
if (layout.Text != null)
{
// For for BUG-1884:
// Although trim is done in the OCR Plugin, it does make sense in the command too.
Console.WriteLine(layout.Text.Trim());
}
} }
modiDocument.Close(false); document.Close(false);
return 0; return 0;
} }
} }
@ -55,5 +94,29 @@ namespace GreenshotOCRCommand {
} }
return -1; return -1;
} }
/// <summary>
/// Helper method
/// </summary>
/// <returns>IEnumerable of IMiRect</returns>
private static IEnumerable<IMiRect> ToEnumerable(IMiRects rects)
{
for (int i = 0; i < rects.Count; i++)
{
yield return rects[i];
}
}
/// <summary>
/// Helper method
/// </summary>
/// <returns>IEnumerable of IWord</returns>
private static IEnumerable<IWord> ToEnumerable(IWords words)
{
for (int i = 0; i < words.Count; i++)
{
yield return words[i];
}
}
} }
} }

View file

@ -101,6 +101,7 @@ namespace GreenshotWin10Plugin
var filename = FilenameHelper.GetFilename(OutputFormat.png, captureDetails); var filename = FilenameHelper.GetFilename(OutputFormat.png, captureDetails);
var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest => var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest =>
{ {
// Information on how was found here: https://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/
Log.DebugFormat("Creating deferred file {0}", filename); Log.DebugFormat("Creating deferred file {0}", filename);
try try
{ {
@ -116,7 +117,7 @@ namespace GreenshotWin10Plugin
{ {
streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete); streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete);
} }
// Signal transfer ready // Signal transfer ready to the await down below
taskCompletionSource.TrySetResult(applicationName); taskCompletionSource.TrySetResult(applicationName);
}, imageRandomAccessStreamReference); }, imageRandomAccessStreamReference);