diff --git a/ShareX/OCR/OCRForm.Designer.cs b/ShareX/OCR/OCRForm.Designer.cs index 3d9a813ea..6060cbfe5 100644 --- a/ShareX/OCR/OCRForm.Designer.cs +++ b/ShareX/OCR/OCRForm.Designer.cs @@ -28,6 +28,7 @@ private void InitializeComponent() this.btnOpenOCRHelp = new System.Windows.Forms.Button(); this.lblService = new System.Windows.Forms.Label(); this.btnSelectRegion = new System.Windows.Forms.Button(); + this.cbSingleLine = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.nudScaleFactor)).BeginInit(); this.SuspendLayout(); // @@ -130,10 +131,18 @@ private void InitializeComponent() this.btnSelectRegion.UseVisualStyleBackColor = true; this.btnSelectRegion.Click += new System.EventHandler(this.btnSelectRegion_Click); // + // cbSingleLine + // + resources.ApplyResources(this.cbSingleLine, "cbSingleLine"); + this.cbSingleLine.Name = "cbSingleLine"; + this.cbSingleLine.UseVisualStyleBackColor = true; + this.cbSingleLine.CheckedChanged += new System.EventHandler(this.cbSingleLine_CheckedChanged); + // // OCRForm // resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.cbSingleLine); this.Controls.Add(this.lblService); this.Controls.Add(this.btnOpenOCRHelp); this.Controls.Add(this.btnSelectRegion); @@ -169,5 +178,6 @@ private void InitializeComponent() private System.Windows.Forms.Button btnOpenOCRHelp; private System.Windows.Forms.Label lblService; private System.Windows.Forms.Button btnSelectRegion; + private System.Windows.Forms.CheckBox cbSingleLine; } } \ No newline at end of file diff --git a/ShareX/OCR/OCRForm.cs b/ShareX/OCR/OCRForm.cs index c0ec33e62..3c4cbd9bf 100644 --- a/ShareX/OCR/OCRForm.cs +++ b/ShareX/OCR/OCRForm.cs @@ -82,6 +82,7 @@ public OCRForm(Bitmap bmp, OCROptions options) } nudScaleFactor.SetValue((decimal)Options.ScaleFactor); + cbSingleLine.Checked = Options.SingleLine; if (Options.ServiceLinks == null || Options.IsDefaultServiceLinks()) { @@ -141,7 +142,7 @@ private async Task OCR(Bitmap bmp) try { - Result = await OCRHelper.OCR(bmp, Options.Language, Options.ScaleFactor); + Result = await OCRHelper.OCR(bmp, Options.Language, Options.ScaleFactor, Options.SingleLine); if (Options.AutoCopy && !string.IsNullOrEmpty(Result)) { @@ -211,6 +212,16 @@ private async void nudScaleFactor_ValueChanged(object sender, EventArgs e) } } + private async void cbSingleLine_CheckedChanged(object sender, EventArgs e) + { + if (loaded) + { + Options.SingleLine = cbSingleLine.Checked; + + await OCR(bmpSource); + } + } + private void cbServices_SelectedIndexChanged(object sender, EventArgs e) { Options.SelectedServiceLink = cbServices.SelectedIndex; diff --git a/ShareX/OCR/OCRForm.resx b/ShareX/OCR/OCRForm.resx index c007aebac..ef01e5e3c 100644 --- a/ShareX/OCR/OCRForm.resx +++ b/ShareX/OCR/OCRForm.resx @@ -147,7 +147,7 @@ $this - 11 + 12 Microsoft Sans Serif, 9.75pt @@ -171,7 +171,7 @@ $this - 10 + 11 True @@ -201,7 +201,7 @@ $this - 9 + 10 @@ -235,7 +235,7 @@ $this - 8 + 9 True @@ -265,7 +265,7 @@ $this - 7 + 8 Microsoft Sans Serif, 9.75pt @@ -292,13 +292,13 @@ $this - 6 + 7 Microsoft Sans Serif, 9.75pt - 16, 192 + 16, 216 272, 24 @@ -316,7 +316,7 @@ $this - 5 + 6 False @@ -325,7 +325,7 @@ Microsoft Sans Serif, 9.75pt - 16, 224 + 16, 248 272, 32 @@ -346,10 +346,10 @@ $this - 4 + 5 - 296, 190 + 296, 214 28, 28 @@ -367,7 +367,7 @@ $this - 3 + 4 Microsoft Sans Serif, 9.75pt @@ -391,7 +391,7 @@ $this - 1 + 2 True @@ -400,7 +400,7 @@ Microsoft Sans Serif, 9.75pt - 13, 168 + 13, 192 56, 16 @@ -421,7 +421,7 @@ $this - 0 + 1 Microsoft Sans Serif, 9.75pt @@ -451,7 +451,37 @@ $this - 2 + 3 + + + True + + + Microsoft Sans Serif, 9.75pt + + + 16, 168 + + + 88, 20 + + + 12 + + + Single line + + + cbSingleLine + + + System.Windows.Forms.CheckBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 0 True diff --git a/ShareX/OCR/OCRHelper.cs b/ShareX/OCR/OCRHelper.cs index 41ba44ff0..199961c36 100644 --- a/ShareX/OCR/OCRHelper.cs +++ b/ShareX/OCR/OCRHelper.cs @@ -69,7 +69,7 @@ public static void ThrowIfNotSupported() } } - public static async Task OCR(Bitmap bmp, string languageTag = "en", float scaleFactor = 1f) + public static async Task OCR(Bitmap bmp, string languageTag = "en", float scaleFactor = 1f, bool singleLine = false) { ThrowIfNotSupported(); @@ -80,12 +80,12 @@ public static async Task OCR(Bitmap bmp, string languageTag = "en", floa using (Bitmap bmpClone = (Bitmap)bmp.Clone()) using (Bitmap bmpScaled = ImageHelpers.ResizeImage(bmpClone, (int)(bmpClone.Width * scaleFactor), (int)(bmpClone.Height * scaleFactor))) { - return await OCRInternal(bmpScaled, languageTag); + return await OCRInternal(bmpScaled, languageTag, singleLine); } }); } - private static async Task OCRInternal(Bitmap bmp, string languageTag) + private static async Task OCRInternal(Bitmap bmp, string languageTag, bool singleLine = false) { Language language = new Language(languageTag); @@ -105,21 +105,32 @@ private static async Task OCRInternal(Bitmap bmp, string languageTag) { OcrResult ocrResult = await engine.RecognizeAsync(softwareBitmap); + string separator; + + if (singleLine) + { + separator = " "; + } + else + { + separator = Environment.NewLine; + } + if (language.LanguageTag.StartsWith("zh", StringComparison.OrdinalIgnoreCase) || // Chinese language.LanguageTag.StartsWith("ja", StringComparison.OrdinalIgnoreCase) || // Japanese language.LanguageTag.StartsWith("ko", StringComparison.OrdinalIgnoreCase)) // Korean { // If CJK language then remove spaces between words. - return string.Join("\r\n", ocrResult.Lines.Select(line => string.Concat(line.Words.Select(word => word.Text)))); + return string.Join(separator, ocrResult.Lines.Select(line => string.Concat(line.Words.Select(word => word.Text)))); } else if (language.LayoutDirection == LanguageLayoutDirection.Rtl) { // If RTL language then reverse order of words. - return string.Join("\r\n", ocrResult.Lines.Select(line => string.Join(" ", line.Words.Reverse().Select(word => word.Text)))); + return string.Join(separator, ocrResult.Lines.Select(line => string.Join(" ", line.Words.Reverse().Select(word => word.Text)))); } else { - return string.Join("\r\n", ocrResult.Lines.Select(line => line.Text)); + return string.Join(separator, ocrResult.Lines.Select(line => line.Text)); } } } diff --git a/ShareX/OCR/OCROptions.cs b/ShareX/OCR/OCROptions.cs index 32504732e..e3e403b6f 100644 --- a/ShareX/OCR/OCROptions.cs +++ b/ShareX/OCR/OCROptions.cs @@ -32,6 +32,7 @@ public class OCROptions { public string Language { get; set; } = "en"; public float ScaleFactor { get; set; } = 2f; + public bool SingleLine { get; set; } = false; public bool Silent { get; set; } = false; public bool AutoCopy { get; set; } = false; public List ServiceLinks { get; set; } = DefaultServiceLinks; diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index eee2126e8..2b874487e 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -1250,7 +1250,7 @@ private static async Task AsyncOCRImage(Bitmap bmp, OCROptions options, string f if (bmp != null) { - result = await OCRHelper.OCR(bmp, options.Language, options.ScaleFactor); + result = await OCRHelper.OCR(bmp, options.Language, options.ScaleFactor, options.SingleLine); } if (!string.IsNullOrEmpty(result))