public void Transform(ScaleTransform scale) { // Scale word box bounding rect and update properties. UpdateProps(scale.TransformBounds(word.BoundingRect)); }
/// <summary> /// This is click handler for Extract Text button. /// If image size is supported text is extracted and overlaid over displayed image. /// Supported image dimensions are between 40 and 2600 pixels. /// </summary> private async void ExtractTextButton_Click(object sender, RoutedEventArgs e) { // Prevent another OCR request, since only image can be processed at the time at same OCR engine instance. ExtractTextButton.IsEnabled = false; // Check whether is loaded image supported for processing. // Supported image dimensions are between 40 and 2600 pixels. if (bitmap.PixelHeight < 40 || bitmap.PixelHeight > 2600 || bitmap.PixelWidth < 40 || bitmap.PixelWidth > 2600) { ImageText.Text = "Resim Boyutu Desteklenmiyor." + Environment.NewLine + "Seçilen resim Boyutu " + bitmap.PixelWidth + "x" + bitmap.PixelHeight + "." + Environment.NewLine + " 40 ve 2600 pixels Resim Boyutları Desteklenmektedir."; ImageText.Style = (Style)Application.Current.Resources["RedTextStyle"]; rootPage.NotifyUser( String.Format("Desteklenmeyen Görüntü Boyutu. " + Environment.NewLine + "Desteklenen dosya boyutu 40 ve 2600 pixel aralığıdır."), NotifyType.ErrorMessage); return; } // This main API call to extract text from image. var ocrResult = await ocrEngine.RecognizeAsync((uint)bitmap.PixelHeight, (uint)bitmap.PixelWidth, bitmap.PixelBuffer.ToArray()); // OCR result does not contain any lines, no text was recognized. if (ocrResult.Lines != null) { // Used for text overlay. // Prepare scale transform for words since image is not displayed in original format. var scaleTrasform = new ScaleTransform { CenterX = 0, CenterY = 0, ScaleX = PreviewImage.ActualWidth / bitmap.PixelWidth, ScaleY = PreviewImage.ActualHeight / bitmap.PixelHeight, }; if (ocrResult.TextAngle != null) { // If text is detected under some angle then // apply a transform rotate on image around center. PreviewImage.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } string extractedText = ""; // Iterate over recognized lines of text. foreach (var line in ocrResult.Lines) { // Iterate over words in line. foreach (var word in line.Words) { var originalRect = new Rect(word.Left, word.Top, word.Width, word.Height); var overlayRect = scaleTrasform.TransformBounds(originalRect); // Define the TextBlock. var wordTextBlock = new TextBlock() { Height = overlayRect.Height, Width = overlayRect.Width, FontSize = overlayRect.Height * 0.8, Text = word.Text, Style = (Style)Application.Current.Resources["ExtractedWordTextStyle"] }; // Define position, background, etc. var border = new Border() { Margin = new Thickness(overlayRect.Left, overlayRect.Top, 0, 0), Height = overlayRect.Height, Width = overlayRect.Width, Child = wordTextBlock, Style = (Style)Application.Current.Resources["ExtractedWordBorderStyle"] }; // Put the filled textblock in the results grid. TextOverlay.Children.Add(border); extractedText += word.Text + " "; } extractedText += Environment.NewLine; } ImageText.Text = extractedText; ImageText.Style = (Style)Application.Current.Resources["GreenTextStyle"]; ImageTextbox.Text = extractedText; } else { ImageText.Text = "Metin Yok."; ImageText.Style = (Style)Application.Current.Resources["RedTextStyle"]; } rootPage.NotifyUser( String.Format("Görüntü Başarıyla {0} Dilinde Okundu.", ocrEngine.Language.ToString()), NotifyType.StatusMessage); }
private async void ExtractText_Click(object sender, RoutedEventArgs e) { //// Prevent another OCR request, since only image can be processed at the time at same OCR engine instance. //ExtractTextButton.IsEnabled = false; // Check whether is loaded image supported for processing. // Supported image dimensions are between 40 and 2600 pixels. if (bitmap.PixelHeight < 40 || bitmap.PixelHeight > 2600 || bitmap.PixelWidth < 40 || bitmap.PixelWidth > 2600) { ImageText.Text = "Image size is not supported." + Environment.NewLine + "Loaded image size is " + bitmap.PixelWidth + "x" + bitmap.PixelHeight + "." + Environment.NewLine + "Supported image dimensions are between 40 and 2600 pixels."; return; } // This main API call to extract text from image. var ocrResult = await ocrEngine.RecognizeAsync((uint)bitmap.PixelHeight, (uint)bitmap.PixelWidth, bitmap.PixelBuffer.ToArray()); // OCR result does not contain any lines, no text was recognized. if (ocrResult.Lines != null) { // Used for text overlay. // Prepare scale transform for words since image is not displayed in original format. var scaleTrasform = new ScaleTransform { CenterX = 0, CenterY = 0, ScaleX = PreviewImage.ActualWidth / bitmap.PixelWidth, ScaleY = PreviewImage.ActualHeight / bitmap.PixelHeight, }; if (ocrResult.TextAngle != null) { PreviewImage.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } string extractedText = ""; // Iterate over recognized lines of text. foreach (var line in ocrResult.Lines) { // Iterate over words in line. foreach (var word in line.Words) { var originalRect = new Rect(word.Left, word.Top, word.Width, word.Height); var overlayRect = scaleTrasform.TransformBounds(originalRect); var wordTextBlock = new TextBlock() { Height = overlayRect.Height, Width = overlayRect.Width, FontSize = overlayRect.Height * 0.8, Text = word.Text, }; // Define position, background, etc. var border = new Border() { Margin = new Thickness(overlayRect.Left, overlayRect.Top, 0, 0), Height = overlayRect.Height, Width = overlayRect.Width, Background = new SolidColorBrush(Colors.Orange), Opacity = 0.5, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Child = wordTextBlock, }; OverlayTextButton.IsEnabled = true; // Put the filled textblock in the results grid. TextOverlay.Children.Add(border); extractedText += word.Text + " "; } extractedText += Environment.NewLine; } ImageText.Text = extractedText; } else { ImageText.Text = "No text."; } }