private void PreviewImage_SizeChanged(object sender, SizeChangedEventArgs e) { ScaleTransform scaleTransform = ScaleTrasform(); foreach (UIElement canvasChild in OverlayCanvas.Children) { if (canvasChild is Rectangle rectangle) { var word = (OcrWord)rectangle.Tag; if (word == null) { continue; } Rect rect = scaleTransform.TransformBounds(word.BoundingRect); rectangle.Width = rect.Width; rectangle.Height = rect.Height; Canvas.SetTop(rectangle, rect.Top); Canvas.SetLeft(rectangle, rect.Left); } } //// Update image rotation center. //var rotate = OverlayCanvas.RenderTransform as RotateTransform; //if (rotate != null) //{ // rotate.CenterX = PreviewImage.ActualWidth / 2; // rotate.CenterY = PreviewImage.ActualHeight / 2; //} }
//拡大倍率変更時はImageを乗せているCanvasのサイズを変更する private void SliderScale_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { //ScaleTransformの拡大倍率変更 MyScale.ScaleX = e.NewValue; MyScale.ScaleY = e.NewValue; //拡大後Imageのサイズを取得 var bounds = MyScale.TransformBounds(new Rect(MyImage1.RenderSize)); //Imageが乗っかっているCanvasのサイズを変更すると //正しく表示され、スクロールバーも期待通りになる MyCanvas1.Height = bounds.Height; MyCanvas1.Width = bounds.Width; //Image2も同様 bounds = MyScale.TransformBounds(new Rect(MyImage2.RenderSize)); MyCanvas2.Height = bounds.Height; MyCanvas2.Width = bounds.Width; }
private void TransformItems(ScaleTransform scaleTransform) { Bounds = scaleTransform.TransformBounds(Bounds); TransformGroup tg = new TransformGroup(); tg.Children.Add(_reverseRotateTransform); tg.Children.Add(scaleTransform); tg.Children.Add(_rotateTransform); vm.TransformSelectedItems(tg); }
public void TransformBounds() { var rect = new Rect(1, 1, 100, 110); var transform = new ScaleTransform { ScaleX = -1.5, ScaleY = -2 }; var result = transform.TransformBounds(rect); result.X.Should().Be(-151.5); result.Y.Should().Be(-222); result.Width.Should().Be(150); result.Height.Should().Be(220); }
private void TransformItems(ScaleTransform scaleTransform) { Bounds = scaleTransform.TransformBounds(Bounds); TransformGroup tg = new TransformGroup(); tg.Children.Add(_reverseRotateTransform); tg.Children.Add(scaleTransform); tg.Children.Add(_rotateTransform); _vm.TransformSelectedItems(tg); // If we haven't rotated yet, then it is safe to update the rotation center if (_rotationAngle == 0) { RotationCenter = Center(Bounds); } }
Transform ComputeModelResizeTransform() { var r = _ui._adorner.Bounds; if (r == _originalUiPos) { return(null); } var delta = r.Location - _originalUiPos.Location; var scaleX = r.Width / _originalUiPos.Width; var scaleY = r.Height / _originalUiPos.Height; var scaleTransform = new ScaleTransform(scaleX, scaleY); var scaledOriginal = scaleTransform.TransformBounds(_origialRect); var scalTranslation = scaledOriginal.Location - _origialRect.Location; var tg = new TransformGroup(); tg.Children.Add(scaleTransform); tg.Children.Add(new TranslateTransform(_origialRect.Left * (1.0 - scaleX) + delta.X, _origialRect.Top * (1.0 - scaleY) + delta.Y)); // return(tg); }
public void Transform(ScaleTransform scale) { // Scale word box bounding rect and update properties. UpdateProps(scale.TransformBounds(word.BoundingRect)); }
private async void Ocr_OnClick(object sender, RoutedEventArgs e) { _drawTool?.Close(); ClearResults(); Rectangle itemsRectangle = _viewModel.ItemsRectangle; Rectangle quantityRectangle = _viewModel.QuantityRectangle; BitmapSource bitmapSource = _viewModel.ImgSource; if (bitmapSource == null || itemsRectangle == null || quantityRectangle == null) { return; } // Check if OcrEngine supports image resoulution. if (bitmapSource.PixelWidth > OcrEngine.MaxImageDimension || bitmapSource.PixelHeight > OcrEngine.MaxImageDimension) { return; } OcrEngine ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages(); if (ocrEngine == null) { // Display message to user! return; } OcrResult ocrResult = await ocrEngine.RecognizeAsync(_viewModel.Bitmap); // Display recognized text. //ExtractedTextBox.Text = ocrResult.Text; if (ocrResult.TextAngle != null) { // If text is detected under some angle in this sample scenario we want to // overlay word boxes over original image, so we rotate overlay boxes. OverlayCanvas.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } ScaleTransform scaleTrasform = ScaleTrasform(); var itemsRect = new Rect(Canvas.GetLeft(itemsRectangle), Canvas.GetTop(itemsRectangle), itemsRectangle.Width, itemsRectangle.Height); var quantityRect = new Rect(Canvas.GetLeft(quantityRectangle), Canvas.GetTop(quantityRectangle), quantityRectangle.Width, quantityRectangle.Height); var items = new List <OcrWord>(); var quantities = new List <OcrWord>(); foreach (OcrLine line in ocrResult.Lines) { //bool isHorizontal = IsHorizontal(line); foreach (OcrWord word in line.Words) { Rect rect = scaleTrasform.TransformBounds(word.BoundingRect); Rect intersectRect1 = itemsRect; intersectRect1.Intersect(rect); Rect intersectRect2 = quantityRect; intersectRect2.Intersect(rect); bool itemsWord = intersectRect1 == rect; bool quantityWord = intersectRect2 == rect; if (!itemsWord && !quantityWord) { continue; } var overlay = new Rectangle { Fill = new SolidColorBrush(Color.FromArgb(125, 0, 0, 255)), Width = rect.Width, Height = rect.Height, Tag = word }; Canvas.SetTop(overlay, rect.Top); Canvas.SetLeft(overlay, rect.Left); OverlayCanvas.Children.Add(overlay); if (itemsWord) { items.Add(word); } else { quantities.Add(word); } } _lines.Add(line); } _viewModel.Items = items; _viewModel.Quantities = quantities; }
public void Transform(ScaleTransform scale) { // Skalowanie i zaktualizowanie wymiarow UpdateProps(scale.TransformBounds(word.BoundingRect)); }
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."; //ImageText.Style = (Style)Application.Current.Resources["RedTextStyle"]; 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."; } }
/// <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 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."; ImageText.Style = (Style)Application.Current.Resources["RedTextStyle"]; rootPage.NotifyUser( String.Format("OCR was attempted on image with unsupported size. " + Environment.NewLine + "Supported image dimensions are between 40 and 2600 pixels."), 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"]; } else { ImageText.Text = "No text."; ImageText.Style = (Style)Application.Current.Resources["RedTextStyle"]; } rootPage.NotifyUser( String.Format("Image successfully processed in {0} language.", ocrEngine.Language.ToString()), NotifyType.StatusMessage); }
private Rect TransformItems(ScaleTransform scaleTransform, Rect bounds) { bounds = scaleTransform.TransformBounds(bounds); return(bounds); }