public ManipulateTextWindow(string rawPassedString) { int lastCommaPosition = rawPassedString.AllIndexesOf(",").LastOrDefault(); CopiedText = rawPassedString.Substring(0, lastCommaPosition); InitializeComponent(); PassedTextControl.Text = CopiedText; string langString = rawPassedString.Substring(lastCommaPosition + 1, (rawPassedString.Length - (lastCommaPosition + 1))); XmlLanguage lang = XmlLanguage.GetLanguage(langString); CultureInfo culture = lang.GetEquivalentCulture(); if (culture.TextInfo.IsRightToLeft) { PassedTextControl.TextAlignment = TextAlignment.Right; } }
public static async Task <string> ExtractText(Bitmap bmp, System.Windows.Point?singlePoint = null) { Language?selectedLanguage = GetOCRLanguage(); if (selectedLanguage == null) { return(""); } XmlLanguage lang = XmlLanguage.GetLanguage(selectedLanguage.LanguageTag); CultureInfo culture = lang.GetEquivalentCulture(); bool scaleBMP = true; if (singlePoint != null || bmp.Width * 1.5 > OcrEngine.MaxImageDimension) { scaleBMP = false; } Bitmap scaledBitmap; if (scaleBMP) { scaledBitmap = ScaleBitmapUniform(bmp, 1.5); } else { scaledBitmap = ScaleBitmapUniform(bmp, 1.0); } StringBuilder text = new(); await using (MemoryStream memory = new()) { scaledBitmap.Save(memory, ImageFormat.Bmp); memory.Position = 0; BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(memory.AsRandomAccessStream()); SoftwareBitmap softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync(); OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(selectedLanguage); OcrResult ocrResult = await ocrEngine.RecognizeAsync(softwareBmp); if (singlePoint == null) { foreach (OcrLine line in ocrResult.Lines) { text.AppendLine(line.Text); } } else { Windows.Foundation.Point fPoint = new Windows.Foundation.Point(singlePoint.Value.X, singlePoint.Value.Y); foreach (OcrLine ocrLine in ocrResult.Lines) { foreach (OcrWord ocrWord in ocrLine.Words) { if (ocrWord.BoundingRect.Contains(fPoint)) { _ = text.Append(ocrWord.Text); } } } } } if (culture.TextInfo.IsRightToLeft) { string[] textListLines = text.ToString().Split(new char[] { '\n', '\r' }); _ = text.Clear(); foreach (string textLine in textListLines) { List <string> wordArray = textLine.Split().ToList(); wordArray.Reverse(); _ = text.Append(string.Join(' ', wordArray)); if (textLine.Length > 0) { _ = text.Append('\n'); } } return(text.ToString()); } else { return(text.ToString()); } }
internal LocalizedName(XmlLanguage language, string name) : this(language, name, language.GetEquivalentCulture().LCID) { }
public async Task <string> ExtractText(Bitmap bmp, string languageCode, System.Windows.Point?singlePoint = null) { Language selectedLanguage = new Language(languageCode); List <Language> possibleOCRLangs = OcrEngine.AvailableRecognizerLanguages.ToList(); if (possibleOCRLangs.Count < 1) { throw new ArgumentOutOfRangeException($"No possible OCR languages are installed."); } if (possibleOCRLangs.Where(l => l.LanguageTag == selectedLanguage.LanguageTag).Count() < 1) { List <Language> similarLanguages = possibleOCRLangs.Where(la => la.AbbreviatedName == selectedLanguage.AbbreviatedName).ToList(); if (similarLanguages.Count() > 0) { selectedLanguage = similarLanguages.FirstOrDefault(); } else { selectedLanguage = possibleOCRLangs.FirstOrDefault(); } } bool scaleBMP = true; if (singlePoint != null || bmp.Width * 1.5 > OcrEngine.MaxImageDimension) { scaleBMP = false; } Bitmap scaledBitmap; if (scaleBMP) { scaledBitmap = ScaleBitmapUniform(bmp, 1.5); } else { scaledBitmap = ScaleBitmapUniform(bmp, 1.0); } StringBuilder text = new StringBuilder(); XmlLanguage lang = XmlLanguage.GetLanguage(languageCode); CultureInfo culture = lang.GetEquivalentCulture(); await using (MemoryStream memory = new MemoryStream()) { scaledBitmap.Save(memory, ImageFormat.Bmp); memory.Position = 0; BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(memory.AsRandomAccessStream()); Windows.Graphics.Imaging.SoftwareBitmap softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync(); OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(selectedLanguage); OcrResult ocrResult = await ocrEngine.RecognizeAsync(softwareBmp); if (singlePoint == null) { foreach (OcrLine line in ocrResult.Lines) { text.AppendLine(line.Text); } } else { Windows.Foundation.Point fPoint = new Windows.Foundation.Point(singlePoint.Value.X, singlePoint.Value.Y); foreach (OcrLine ocrLine in ocrResult.Lines) { foreach (OcrWord ocrWord in ocrLine.Words) { if (ocrWord.BoundingRect.Contains(fPoint)) { text.Append(ocrWord.Text); } } } } } if (culture.TextInfo.IsRightToLeft) { List <string> textListLines = text.ToString().Split(new char[] { '\n', '\r' }).ToList(); text.Clear(); foreach (string textLine in textListLines) { List <string> wordArray = textLine.Split().ToList(); wordArray.Reverse(); text.Append(string.Join(' ', wordArray)); if (textLine.Length > 0) { text.Append('\n'); } } return(text.ToString()); } else { return(text.ToString()); } }