2016-05-13 11:43:33 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2021-07-29 15:22:51 +12:00
|
|
|
|
Copyright (c) 2007-2021 ShareX Team
|
2016-05-13 11:43:33 +12:00
|
|
|
|
|
|
|
|
|
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;
|
2021-10-23 09:32:04 +13:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
2016-05-13 12:13:28 +12:00
|
|
|
|
using System.IO;
|
2018-08-04 01:38:18 +12:00
|
|
|
|
using System.Threading.Tasks;
|
2016-05-13 11:43:33 +12:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.UploadersLib
|
|
|
|
|
{
|
2021-10-30 09:22:06 +13:00
|
|
|
|
public enum OCRSpaceSites
|
2021-10-23 09:32:04 +13:00
|
|
|
|
{
|
|
|
|
|
[Description("Google Translate")]
|
2021-10-30 09:22:06 +13:00
|
|
|
|
GoogleTranslate,
|
2021-10-23 09:32:04 +13:00
|
|
|
|
[Description("DeepL Translate")]
|
2021-10-30 09:22:06 +13:00
|
|
|
|
DeepL,
|
|
|
|
|
[Description("Jisho")]
|
|
|
|
|
Jisho,
|
|
|
|
|
[Description("ichi.moe")]
|
|
|
|
|
Ichi,
|
|
|
|
|
[Description("Google")]
|
2021-10-31 05:26:56 +13:00
|
|
|
|
Google
|
2021-10-23 09:32:04 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 17:56:33 +12:00
|
|
|
|
public partial class OCRSpaceForm : Form
|
2016-05-13 11:43:33 +12:00
|
|
|
|
{
|
2016-05-13 17:56:33 +12:00
|
|
|
|
public string Result { get; private set; }
|
2016-05-13 12:13:28 +12:00
|
|
|
|
|
2016-05-13 17:56:33 +12:00
|
|
|
|
private Stream data;
|
2018-11-29 04:23:06 +13:00
|
|
|
|
private string fileName;
|
2018-11-15 14:10:54 +13:00
|
|
|
|
private OCROptions ocrOptions;
|
2021-10-23 09:32:04 +13:00
|
|
|
|
private OCRSpaceLanguages Language { get; set; }
|
|
|
|
|
|
2021-10-30 09:22:06 +13:00
|
|
|
|
private Dictionary<OCRSpaceSites, string> SiteLinks =
|
|
|
|
|
new Dictionary<OCRSpaceSites, string>()
|
2021-10-23 09:32:04 +13:00
|
|
|
|
{
|
2021-10-30 09:22:06 +13:00
|
|
|
|
{ OCRSpaceSites.GoogleTranslate, "https://translate.google.com/#auto/en/" },
|
|
|
|
|
{ OCRSpaceSites.DeepL, "https://www.deepl.com/translator#auto/en/" },
|
|
|
|
|
{ OCRSpaceSites.Jisho, "https://jisho.org/search/" },
|
|
|
|
|
{ OCRSpaceSites.Ichi, "https://ichi.moe/cl/qr/?q=" },
|
2021-10-31 05:26:56 +13:00
|
|
|
|
{ OCRSpaceSites.Google, "https://www.google.com/search?q=" }
|
2021-10-23 09:32:04 +13:00
|
|
|
|
};
|
2016-05-13 17:56:33 +12:00
|
|
|
|
|
2018-11-15 14:10:54 +13:00
|
|
|
|
public OCRSpaceForm(OCROptions ocrOptions)
|
2016-05-13 11:43:33 +12:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2019-06-25 06:36:16 +12:00
|
|
|
|
ShareXResources.ApplyTheme(this);
|
2018-11-29 02:14:19 +13:00
|
|
|
|
|
|
|
|
|
this.ocrOptions = ocrOptions;
|
2016-05-13 11:43:33 +12:00
|
|
|
|
cbLanguages.Items.AddRange(Helpers.GetEnumDescriptions<OCRSpaceLanguages>());
|
2018-11-15 14:10:54 +13:00
|
|
|
|
cbLanguages.SelectedIndex = (int)ocrOptions.DefaultLanguage;
|
2021-10-23 09:32:04 +13:00
|
|
|
|
|
2021-10-30 09:22:06 +13:00
|
|
|
|
cbDefaultSite.Items.AddRange(Helpers.GetEnumDescriptions<OCRSpaceSites>());
|
|
|
|
|
cbDefaultSite.SelectedIndex = (int)ocrOptions.DefaultSite;
|
2021-10-23 09:32:04 +13:00
|
|
|
|
|
2018-11-15 14:10:54 +13:00
|
|
|
|
Language = ocrOptions.DefaultLanguage;
|
2018-01-04 10:39:10 +13:00
|
|
|
|
txtResult.SupportSelectAll();
|
2016-05-13 12:13:28 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 20:52:06 +13:00
|
|
|
|
public OCRSpaceForm(Stream data, string fileName, OCROptions ocrOptions) : this(ocrOptions)
|
2016-05-13 12:13:28 +12:00
|
|
|
|
{
|
2016-05-13 17:56:33 +12:00
|
|
|
|
this.data = data;
|
2018-11-30 20:52:06 +13:00
|
|
|
|
this.fileName = fileName;
|
2016-05-13 12:13:28 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-04 01:38:18 +12:00
|
|
|
|
private async void OCRSpaceResultForm_Shown(object sender, EventArgs e)
|
2016-05-13 12:13:28 +12:00
|
|
|
|
{
|
2016-05-13 12:22:47 +12:00
|
|
|
|
UpdateControls();
|
2016-05-13 12:13:28 +12:00
|
|
|
|
|
2018-11-29 02:14:19 +13:00
|
|
|
|
if (ocrOptions.ProcessOnLoad && string.IsNullOrEmpty(Result))
|
2016-05-13 12:13:28 +12:00
|
|
|
|
{
|
2018-11-29 04:23:06 +13:00
|
|
|
|
await StartOCR(data, fileName);
|
2016-05-13 12:13:28 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 12:22:47 +12:00
|
|
|
|
private void UpdateControls()
|
2016-05-13 12:13:28 +12:00
|
|
|
|
{
|
|
|
|
|
cbLanguages.SelectedIndex = (int)Language;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Result))
|
|
|
|
|
{
|
|
|
|
|
txtResult.Text = Result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 04:23:06 +13:00
|
|
|
|
btnStartOCR.Visible = data != null && data.Length > 0 && !string.IsNullOrEmpty(fileName);
|
2016-05-13 12:13:28 +12:00
|
|
|
|
}
|
2016-05-13 11:43:33 +12:00
|
|
|
|
|
2018-11-29 04:23:06 +13:00
|
|
|
|
public async Task StartOCR(Stream stream, string fileName)
|
2016-05-13 12:13:28 +12:00
|
|
|
|
{
|
2018-11-29 04:23:06 +13:00
|
|
|
|
if (stream != null && stream.Length > 0 && !string.IsNullOrEmpty(fileName))
|
2016-05-13 11:43:33 +12:00
|
|
|
|
{
|
2021-10-30 09:22:06 +13:00
|
|
|
|
cbLanguages.Enabled = btnStartOCR.Enabled = txtResult.Enabled = btnOpenInBrowser.Enabled = cbDefaultSite.Enabled = false;
|
2016-05-13 18:28:04 +12:00
|
|
|
|
pbProgress.Visible = true;
|
2016-05-13 12:13:28 +12:00
|
|
|
|
|
2018-11-29 04:23:06 +13:00
|
|
|
|
Result = await OCRSpace.DoOCRAsync(Language, stream, fileName);
|
2018-08-04 01:38:18 +12:00
|
|
|
|
|
2018-11-30 20:52:06 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(Result) && ocrOptions.AutoCopy)
|
|
|
|
|
{
|
|
|
|
|
ClipboardHelpers.CopyText(Result);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-04 01:38:18 +12:00
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
UpdateControls();
|
2021-10-30 09:22:06 +13:00
|
|
|
|
cbLanguages.Enabled = btnStartOCR.Enabled = txtResult.Enabled = btnOpenInBrowser.Enabled = cbDefaultSite.Enabled = true;
|
2018-08-04 01:38:18 +12:00
|
|
|
|
pbProgress.Visible = false;
|
|
|
|
|
txtResult.Focus();
|
|
|
|
|
}
|
2016-05-13 11:43:33 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 12:13:28 +12:00
|
|
|
|
private void cbLanguages_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Language = (OCRSpaceLanguages)cbLanguages.SelectedIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-04 01:38:18 +12:00
|
|
|
|
private async void btnStartOCR_Click(object sender, EventArgs e)
|
2016-05-13 12:13:28 +12:00
|
|
|
|
{
|
2018-11-29 04:23:06 +13:00
|
|
|
|
await StartOCR(data, fileName);
|
2016-05-13 12:13:28 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 09:32:04 +13:00
|
|
|
|
private void btnOpenInBrowser_Click(object sender, EventArgs e)
|
2018-02-24 04:49:04 +13:00
|
|
|
|
{
|
2021-10-30 09:22:06 +13:00
|
|
|
|
URLHelpers.OpenURL(SiteLinks[ocrOptions.DefaultSite] + Uri.EscapeDataString(txtResult.Text));
|
2018-10-10 00:30:22 +13:00
|
|
|
|
Close();
|
2018-02-24 04:49:04 +13:00
|
|
|
|
}
|
2021-10-23 09:32:04 +13:00
|
|
|
|
|
2021-10-30 09:22:06 +13:00
|
|
|
|
private void cbDefaultSite_SelectedIndexChanged(object sender, EventArgs e)
|
2021-10-23 09:32:04 +13:00
|
|
|
|
{
|
|
|
|
|
// This doesn't work
|
2021-10-30 09:22:06 +13:00
|
|
|
|
ocrOptions.DefaultSite = (OCRSpaceSites)cbDefaultSite.SelectedIndex;
|
2021-10-23 09:32:04 +13:00
|
|
|
|
}
|
2016-05-13 11:43:33 +12:00
|
|
|
|
}
|
2018-03-01 11:52:11 +13:00
|
|
|
|
}
|