private async void TakePhoto_Click(object sender, RoutedEventArgs e) { try { photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo); if (photo == null) { return; } else { imageStream = await photo.OpenAsync(FileAccessMode.Read); Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imageStream); Windows.Graphics.Imaging.SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); Windows.Graphics.Imaging.SoftwareBitmap softwareBitmapBRG = Windows.Graphics.Imaging.SoftwareBitmap.Convert(softwareBitmap, Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBRG); image.Source = bitmapSource; } } catch { output.Text = "Error carnal"; } }
async Task <OcrResult> RecognizeBitmapAsync(Bitmap b) { // Need to marshall from Drawing.Bitmap to UWP SoftwareBitmap using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { b.Save(stream.AsStream(), System.Drawing.Imaging.ImageFormat.Bmp); //choose the specific image format by your own bitmap source Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream); Windows.Graphics.Imaging.SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); return(await engine.RecognizeAsync(softwareBitmap)); } }
public async Task <string> ExtractText(Bitmap bmp, string languageCode, System.Windows.Point?singlePoint = null) { if (!GlobalizationPreferences.Languages.Contains(languageCode)) { throw new ArgumentOutOfRangeException($"{languageCode} is not installed."); } StringBuilder text = new StringBuilder(); await using (MemoryStream memory = new MemoryStream()) { bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(memory.AsRandomAccessStream()); Windows.Graphics.Imaging.SoftwareBitmap softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync(); OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(new Language(languageCode)); 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); } } } } } return(text.ToString()); }
private async void GetPhotoButton_Click(object sender, RoutedEventArgs e) { // This is to select a file to open. //Windows.Storage.Pickers.FileOpenPicker openPicker = new Windows.Storage.Pickers.FileOpenPicker(); openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.ViewMode = PickerViewMode.Thumbnail; // Filter to include a sample subset of file types. openPicker.FileTypeFilter.Clear(); openPicker.FileTypeFilter.Add(".bmp"); openPicker.FileTypeFilter.Add(".png"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".jpg"); // Open the file picker . photo = await openPicker.PickSingleFileAsync(); // The file is null if user cancels the file picker. if (photo != null) { // Here we open a stream for the selected file. // The 'using' block ensures the stream is disposed // after the image is loaded. imageStream = await photo.OpenAsync(FileAccessMode.Read); Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imageStream); Windows.Graphics.Imaging.SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); Windows.Graphics.Imaging.SoftwareBitmap softwareBitmapBRG = Windows.Graphics.Imaging.SoftwareBitmap.Convert(softwareBitmap, Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBRG); image.Source = bitmapSource; } }
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()); } }