Allow result form to request OCR itself in this way it can re request after change language or used in tools

This commit is contained in:
Jaex 2016-05-13 03:13:28 +03:00
parent 0facef414b
commit c553fdf1fb
4 changed files with 95 additions and 24 deletions

View file

@ -33,6 +33,7 @@ private void InitializeComponent()
this.txtResult = new System.Windows.Forms.TextBox();
this.lblResult = new System.Windows.Forms.Label();
this.llAttribution = new System.Windows.Forms.LinkLabel();
this.btnStartOCR = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cbLanguages
@ -43,6 +44,7 @@ private void InitializeComponent()
this.cbLanguages.Name = "cbLanguages";
this.cbLanguages.Size = new System.Drawing.Size(152, 21);
this.cbLanguages.TabIndex = 0;
this.cbLanguages.SelectedIndexChanged += new System.EventHandler(this.cbLanguages_SelectedIndexChanged);
//
// lblLanguage
//
@ -85,19 +87,32 @@ private void InitializeComponent()
this.llAttribution.Text = "OCR.Space";
this.llAttribution.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llAttribution_LinkClicked);
//
// btnStartOCR
//
this.btnStartOCR.Location = new System.Drawing.Point(168, 22);
this.btnStartOCR.Name = "btnStartOCR";
this.btnStartOCR.Size = new System.Drawing.Size(136, 24);
this.btnStartOCR.TabIndex = 5;
this.btnStartOCR.Text = "Start OCR";
this.btnStartOCR.UseVisualStyleBackColor = true;
this.btnStartOCR.Click += new System.EventHandler(this.btnStartOCR_Click);
//
// 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.ClientSize = new System.Drawing.Size(560, 448);
this.Controls.Add(this.btnStartOCR);
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.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ShareX - Optical character recognition";
this.Shown += new System.EventHandler(this.OCRSpaceResultForm_Shown);
this.ResumeLayout(false);
this.PerformLayout();
@ -110,5 +125,6 @@ private void InitializeComponent()
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Label lblResult;
private System.Windows.Forms.LinkLabel llAttribution;
private System.Windows.Forms.Button btnStartOCR;
}
}

View file

@ -30,6 +30,7 @@
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
@ -38,19 +39,82 @@ namespace ShareX.UploadersLib
{
public partial class OCRSpaceResultForm : Form
{
public OCRSpaceResultForm(string result, OCRSpaceLanguages language = OCRSpaceLanguages.eng)
public Stream Data { get; private set; }
public string Filename { get; private set; }
public OCRSpaceLanguages Language { get; set; } = OCRSpaceLanguages.eng;
public string Result { get; set; }
public OCRSpaceResultForm()
{
InitializeComponent();
Icon = ShareXResources.Icon;
cbLanguages.Items.AddRange(Helpers.GetEnumDescriptions<OCRSpaceLanguages>());
cbLanguages.SelectedIndex = (int)language;
}
if (!string.IsNullOrEmpty(result))
public OCRSpaceResultForm(Stream data, string filename) : this()
{
Data = data;
Filename = filename;
}
private void OCRSpaceResultForm_Shown(object sender, EventArgs e)
{
UpdateValues();
if (string.IsNullOrEmpty(Result))
{
txtResult.Text = result;
StartOCR();
}
}
public void UpdateValues()
{
cbLanguages.SelectedIndex = (int)Language;
if (!string.IsNullOrEmpty(Result))
{
txtResult.Text = Result;
}
btnStartOCR.Visible = Data != null && Data.Length > 0 && !string.IsNullOrEmpty(Filename);
}
private void StartOCR()
{
if (Data != null && Data.Length > 0 && !string.IsNullOrEmpty(Filename))
{
cbLanguages.Enabled = btnStartOCR.Enabled = false;
try
{
OCRSpace ocr = new OCRSpace(Language, false);
OCRSpaceResponse response = ocr.DoOCR(Data, Filename);
if (response != null && !response.IsErroredOnProcessing && response.ParsedResults.Count > 0)
{
Result = response.ParsedResults[0].ParsedText;
UpdateValues();
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
cbLanguages.Enabled = btnStartOCR.Enabled = true;
}
}
private void cbLanguages_SelectedIndexChanged(object sender, EventArgs e)
{
Language = (OCRSpaceLanguages)cbLanguages.SelectedIndex;
}
private void btnStartOCR_Click(object sender, EventArgs e)
{
StartOCR();
}
private void llAttribution_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
URLHelpers.OpenURL("https://ocr.space");

View file

@ -88,7 +88,12 @@ public class OCRSpace : Uploader
public OCRSpaceLanguages Language { get; set; } = OCRSpaceLanguages.eng;
public bool Overlay { get; set; }
public bool ShowResultWindow { get; set; }
public OCRSpace(OCRSpaceLanguages language = OCRSpaceLanguages.eng, bool overlay = false)
{
Language = language;
Overlay = overlay;
}
public OCRSpaceResponse DoOCR(Stream stream, string fileName)
{
@ -102,17 +107,7 @@ public OCRSpaceResponse DoOCR(Stream stream, string fileName)
if (ur.IsSuccess)
{
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 JsonConvert.DeserializeObject<OCRSpaceResponse>(ur.Response);
}
return null;

View file

@ -968,14 +968,10 @@ private void DoOCR()
{
if (Data != null && Info.DataType == EDataType.Image)
{
OCRSpace ocr = new OCRSpace()
using (OCRSpaceResultForm ocrForm = new OCRSpaceResultForm(Data, Info.FileName))
{
Language = OCRSpaceLanguages.eng,
Overlay = false,
ShowResultWindow = true
};
ocr.DoOCR(Data, Info.FileName);
ocrForm.ShowDialog();
}
}
}