Adding after capture tasks OCR support

This commit is contained in:
Jaex 2016-05-13 02:43:33 +03:00
parent c430351c75
commit 0facef414b
7 changed files with 348 additions and 8 deletions

View file

@ -0,0 +1,114 @@
namespace ShareX.UploadersLib
{
partial class OCRSpaceResultForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cbLanguages = new System.Windows.Forms.ComboBox();
this.lblLanguage = new System.Windows.Forms.Label();
this.txtResult = new System.Windows.Forms.TextBox();
this.lblResult = new System.Windows.Forms.Label();
this.llAttribution = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// cbLanguages
//
this.cbLanguages.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbLanguages.FormattingEnabled = true;
this.cbLanguages.Location = new System.Drawing.Point(8, 24);
this.cbLanguages.Name = "cbLanguages";
this.cbLanguages.Size = new System.Drawing.Size(152, 21);
this.cbLanguages.TabIndex = 0;
//
// lblLanguage
//
this.lblLanguage.AutoSize = true;
this.lblLanguage.Location = new System.Drawing.Point(5, 8);
this.lblLanguage.Name = "lblLanguage";
this.lblLanguage.Size = new System.Drawing.Size(58, 13);
this.lblLanguage.TabIndex = 1;
this.lblLanguage.Text = "Language:";
//
// txtResult
//
this.txtResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtResult.Location = new System.Drawing.Point(8, 72);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.Size = new System.Drawing.Size(544, 368);
this.txtResult.TabIndex = 2;
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Location = new System.Drawing.Point(5, 56);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(40, 13);
this.lblResult.TabIndex = 3;
this.lblResult.Text = "Result:";
//
// llAttribution
//
this.llAttribution.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.llAttribution.AutoSize = true;
this.llAttribution.Location = new System.Drawing.Point(488, 8);
this.llAttribution.Name = "llAttribution";
this.llAttribution.Size = new System.Drawing.Size(64, 13);
this.llAttribution.TabIndex = 4;
this.llAttribution.TabStop = true;
this.llAttribution.Text = "OCR.Space";
this.llAttribution.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llAttribution_LinkClicked);
//
// OCRSpaceResultForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(560, 446);
this.Controls.Add(this.llAttribution);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.lblLanguage);
this.Controls.Add(this.cbLanguages);
this.Name = "OCRSpaceResultForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ShareX - Optical character recognition";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ComboBox cbLanguages;
private System.Windows.Forms.Label lblLanguage;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Label lblResult;
private System.Windows.Forms.LinkLabel llAttribution;
}
}

View file

@ -0,0 +1,59 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2016 ShareX Team
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using ShareX.UploadersLib.OtherServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ShareX.UploadersLib
{
public partial class OCRSpaceResultForm : Form
{
public OCRSpaceResultForm(string result, OCRSpaceLanguages language = OCRSpaceLanguages.eng)
{
InitializeComponent();
Icon = ShareXResources.Icon;
cbLanguages.Items.AddRange(Helpers.GetEnumDescriptions<OCRSpaceLanguages>());
cbLanguages.SelectedIndex = (int)language;
if (!string.IsNullOrEmpty(result))
{
txtResult.Text = result;
}
}
private void llAttribution_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
URLHelpers.OpenURL("https://ocr.space");
}
}
}

View file

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -86,19 +86,33 @@ public class OCRSpace : Uploader
private const string APIURLEurope = "https://apipro3.ocr.space/parse/image"; // Frankfurt
private const string APIURLAsia = "https://apipro8.ocr.space/parse/image"; // Tokyo
public OCRSpaceResponse DoOCR(Stream stream, string fileName, OCRSpaceLanguages language, bool overlay = false)
public OCRSpaceLanguages Language { get; set; } = OCRSpaceLanguages.eng;
public bool Overlay { get; set; }
public bool ShowResultWindow { get; set; }
public OCRSpaceResponse DoOCR(Stream stream, string fileName)
{
Dictionary<string, string> arguments = new Dictionary<string, string>();
arguments.Add("apikey", APIKeys.OCRSpaceAPIKey);
//arguments.Add("url", "");
arguments.Add("language", language.ToString());
arguments.Add("isOverlayRequired", overlay.ToString());
arguments.Add("language", Language.ToString());
arguments.Add("isOverlayRequired", Overlay.ToString());
UploadResult ur = UploadData(stream, APIURLEurope, fileName, "file", arguments);
if (ur.IsSuccess)
{
return JsonConvert.DeserializeObject<OCRSpaceResponse>(ur.Response);
OCRSpaceResponse response = JsonConvert.DeserializeObject<OCRSpaceResponse>(ur.Response);
if (response != null && !response.IsErroredOnProcessing && ShowResultWindow)
{
using (OCRSpaceResultForm resultForm = new OCRSpaceResultForm(response.ParsedResults[0].ParsedText, Language))
{
resultForm.ShowDialog();
}
}
return response;
}
return null;

View file

@ -102,6 +102,7 @@
<HintPath>..\Lib\Security.Cryptography.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.FtpClient, Version=1.0.5281.14359, Culture=neutral, PublicKeyToken=fa4be07daa57c2b7, processorArchitecture=MSIL">
@ -150,6 +151,12 @@
<Compile Include="FileUploaders\SharedFolderUploader.cs" />
<Compile Include="FileUploaders\Uguu.cs" />
<Compile Include="FileUploaders\VideoBin.cs" />
<Compile Include="Forms\OCRSpaceResultForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\OCRSpaceResultForm.Designer.cs">
<DependentUpon>OCRSpaceResultForm.cs</DependentUpon>
</Compile>
<Compile Include="FTPClient\FTPClientForm.cs">
<SubType>Form</SubType>
</Compile>
@ -579,6 +586,9 @@
<EmbeddedResource Include="Forms\OAuthWebForm.zh-CN.resx">
<DependentUpon>OAuthWebForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\OCRSpaceResultForm.resx">
<DependentUpon>OCRSpaceResultForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\ResponseForm.de.resx">
<DependentUpon>ResponseForm.cs</DependentUpon>
</EmbeddedResource>

View file

@ -104,9 +104,10 @@ public enum AfterCaptureTasks // Localized
CopyFileToClipboard = 1 << 10,
CopyFilePathToClipboard = 1 << 11,
ShowInExplorer = 1 << 12,
ShowBeforeUploadWindow = 1 << 13,
UploadImageToHost = 1 << 14,
DeleteFile = 1 << 15
DoOCR = 1 << 13,
ShowBeforeUploadWindow = 1 << 14,
UploadImageToHost = 1 << 15,
DeleteFile = 1 << 16
}
[Flags]

View file

@ -26,6 +26,7 @@
using ShareX.HelpersLib;
using ShareX.Properties;
using ShareX.UploadersLib;
using ShareX.UploadersLib.OtherServices;
using System;
using System.Drawing;
using System.IO;
@ -548,6 +549,11 @@ private bool DoThreadJob()
DoFileJobs();
}
if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.DoOCR))
{
DoOCR();
}
if (Info.IsUploadJob && Data != null && Data.CanSeek)
{
Data.Position = 0;
@ -595,7 +601,8 @@ private bool DoAfterCaptureJobs()
TaskHelpers.PrintImage(tempImage);
}
if (Info.TaskSettings.AfterCaptureJob.HasFlagAny(AfterCaptureTasks.SaveImageToFile, AfterCaptureTasks.SaveImageToFileWithDialog, AfterCaptureTasks.UploadImageToHost))
if (Info.TaskSettings.AfterCaptureJob.HasFlagAny(AfterCaptureTasks.SaveImageToFile, AfterCaptureTasks.SaveImageToFileWithDialog, AfterCaptureTasks.DoOCR,
AfterCaptureTasks.UploadImageToHost))
{
using (tempImage)
{
@ -957,6 +964,21 @@ private bool DownloadAndUpload()
return false;
}
private void DoOCR()
{
if (Data != null && Info.DataType == EDataType.Image)
{
OCRSpace ocr = new OCRSpace()
{
Language = OCRSpaceLanguages.eng,
Overlay = false,
ShowResultWindow = true
};
ocr.DoOCR(Data, Info.FileName);
}
}
private bool LoadFileStream()
{
try