/// <summary> /// This is event handler for 'Extract' button. /// Captures image from camera ,recognizes text and displays it. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ExtractButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { //Get information about the preview. var previewProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; int videoFrameWidth = (int)previewProperties.Width; int videoFrameHeight = (int)previewProperties.Height; // In portrait modes, the width and height must be swapped for the VideoFrame to have the correct aspect ratio and avoid letterboxing / black bars. if (!externalCamera && (displayInformation.CurrentOrientation == DisplayOrientations.Portrait || displayInformation.CurrentOrientation == DisplayOrientations.PortraitFlipped)) { videoFrameWidth = (int)previewProperties.Height; videoFrameHeight = (int)previewProperties.Width; } // Create the video frame to request a SoftwareBitmap preview frame. var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, videoFrameWidth, videoFrameHeight); // Capture the preview frame. using (var currentFrame = await mediaCapture.GetPreviewFrameAsync(videoFrame)) { // Collect the resulting frame. SoftwareBitmap bitmap = currentFrame.SoftwareBitmap; OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(ocrLanguage); if (ocrEngine == null) { rootPage.NotifyUser(ocrLanguage.DisplayName + " is not supported.", NotifyType.ErrorMessage); return; } var imgSource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(imgSource.PixelBuffer); PreviewImage.Source = imgSource; var ocrResult = await ocrEngine.RecognizeAsync(bitmap); // 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 = PreviewControl.ActualWidth / bitmap.PixelWidth, ScaleY = PreviewControl.ActualHeight / bitmap.PixelHeight }; 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. TextOverlay.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } // Iterate over recognized lines of text. foreach (var line in ocrResult.Lines) { // Iterate over words in line. foreach (var word in line.Words) { WordOverlay wordBoxOverlay = new WordOverlay(word); // Keep references to word boxes. wordBoxes.Add(wordBoxOverlay); // Create a box with the word inside it. var textBlock = new TextBlock() { Text = word.Text, Style = ExtractedWordTextStyle }; TextOverlay.Children.Add(wordBoxOverlay.CreateBorder(HighlightedWordBoxHorizontalLineStyle, textBlock)); } } rootPage.NotifyUser("Image processed using " + ocrEngine.RecognizerLanguage.DisplayName + " language.", NotifyType.StatusMessage); } UpdateWordBoxTransform(); PreviewControl.Visibility = Visibility.Collapsed; Image.Visibility = Visibility.Visible; ExtractButton.Visibility = Visibility.Collapsed; CameraButton.Visibility = Visibility.Visible; }
/// <summary> /// This is event handler for 'Extract' button. /// Recognizes text from image and displays it. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ExtractButton_Click(object sender, RoutedEventArgs e) { ClearResults(); // Check if OcrEngine supports image resolution. if (bitmap.PixelWidth > OcrEngine.MaxImageDimension || bitmap.PixelHeight > OcrEngine.MaxImageDimension) { rootPage.NotifyUser( String.Format("Bitmap dimensions ({0}x{1}) are too big for OCR.", bitmap.PixelWidth, bitmap.PixelHeight) + "Max image dimension is " + OcrEngine.MaxImageDimension + ".", NotifyType.ErrorMessage); return; } OcrEngine ocrEngine = null; if (UserLanguageToggle.IsOn) { // Try to create OcrEngine for first supported language from UserProfile.GlobalizationPreferences.Languages list. // If none of the languages are available on device, method returns null. ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages(); } else { // Try to create OcrEngine for specified language. // If language is not supported on device, method returns null. ocrEngine = OcrEngine.TryCreateFromLanguage(LanguageList.SelectedValue as Language); } if (ocrEngine != null) { // Recognize text from image. var ocrResult = await ocrEngine.RecognizeAsync(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. TextOverlay.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } // Create overlay boxes over recognized words. foreach (var line in ocrResult.Lines) { // Determine if line is horizontal or vertical. // Vertical lines are supported only in Chinese Traditional and Japanese languages. Rect lineRect = Rect.Empty; foreach (var word in line.Words) { lineRect.Union(word.BoundingRect); } bool isVerticalLine = lineRect.Height > lineRect.Width; var style = isVerticalLine ? HighlightedWordBoxVerticalLineStyle : HighlightedWordBoxHorizontalLineStyle; foreach (var word in line.Words) { WordOverlay wordBoxOverlay = new WordOverlay(word); // Keep references to word boxes. wordBoxes.Add(wordBoxOverlay); // Create a box to highlight the word. TextOverlay.Children.Add(wordBoxOverlay.CreateBorder(style)); } } // Rescale word boxes to match current UI size. UpdateWordBoxTransform(); rootPage.NotifyUser( "Image is OCRed for " + ocrEngine.RecognizerLanguage.DisplayName + " language.", NotifyType.StatusMessage); } else { rootPage.NotifyUser("Selected language is not available.", NotifyType.ErrorMessage); } }
/// <summary> /// This is event handler for 'Extract' button. /// Captures image from camera ,recognizes text and displays it. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ExtractButton_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e) { //Get information about the preview. var previewProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; int videoFrameWidth = (int)previewProperties.Width; int videoFrameHeight = (int)previewProperties.Height; // In portrait modes, the width and height must be swapped for the VideoFrame to have the correct aspect ratio and avoid letterboxing / black bars. if (!externalCamera && (displayInformation.CurrentOrientation == DisplayOrientations.Portrait || displayInformation.CurrentOrientation == DisplayOrientations.PortraitFlipped)) { videoFrameWidth = (int)previewProperties.Height; videoFrameHeight = (int)previewProperties.Width; } // Create the video frame to request a SoftwareBitmap preview frame. var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, videoFrameWidth, videoFrameHeight); // Capture the preview frame. using (var currentFrame = await mediaCapture.GetPreviewFrameAsync(videoFrame)) { // Collect the resulting frame. SoftwareBitmap bitmap = currentFrame.SoftwareBitmap; OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(ocrLanguage); if (ocrEngine == null) { rootPage.NotifyUser(ocrLanguage.DisplayName + " is not supported.", NotifyType.ErrorMessage); return; } var imgSource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(imgSource.PixelBuffer); PreviewImage.Source = imgSource; var ocrResult = await ocrEngine.RecognizeAsync(bitmap); // 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 = PreviewControl.ActualWidth / bitmap.PixelWidth, ScaleY = PreviewControl.ActualHeight / bitmap.PixelHeight }; 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. TextOverlay.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } // Iterate over recognized lines of text. foreach (var line in ocrResult.Lines) { // Iterate over words in line. foreach (var word in line.Words) { // Define the TextBlock. var wordTextBlock = new TextBlock() { Text = word.Text, Style = (Style)this.Resources["ExtractedWordTextStyle"] }; WordOverlay wordBoxOverlay = new WordOverlay(word); // Keep references to word boxes. wordBoxes.Add(wordBoxOverlay); // Define position, background, etc. var overlay = new Border() { Child = wordTextBlock, Style = (Style)this.Resources["HighlightedWordBoxHorizontalLine"] }; // Bind word boxes to UI. overlay.SetBinding(Border.MarginProperty, wordBoxOverlay.CreateWordPositionBinding()); overlay.SetBinding(Border.WidthProperty, wordBoxOverlay.CreateWordWidthBinding()); overlay.SetBinding(Border.HeightProperty, wordBoxOverlay.CreateWordHeightBinding()); // Put the filled textblock in the results grid. TextOverlay.Children.Add(overlay); } } rootPage.NotifyUser("Image processed using " + ocrEngine.RecognizerLanguage.DisplayName + " language.", NotifyType.StatusMessage); } UpdateWordBoxTransform(); PreviewControl.Visibility = Visibility.Collapsed; Image.Visibility = Visibility.Visible; ExtractButton.Visibility = Visibility.Collapsed; CameraButton.Visibility = Visibility.Visible; }
private async void takePicture() { //Get information about the preview. bool foundLicensePlate = false; bool foundValidPermit = false; var previewProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; int videoFrameWidth = (int)previewProperties.Width; int videoFrameHeight = (int)previewProperties.Height; // In portrait modes, the width and height must be swapped for the VideoFrame to have the correct aspect ratio and avoid letterboxing / black bars. if (!externalCamera && (displayInformation.CurrentOrientation == DisplayOrientations.Portrait || displayInformation.CurrentOrientation == DisplayOrientations.PortraitFlipped)) { videoFrameWidth = (int)previewProperties.Height; videoFrameHeight = (int)previewProperties.Width; } // Create the video frame to request a SoftwareBitmap preview frame. var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, videoFrameWidth, videoFrameHeight); // Capture the preview frame. using (var currentFrame = await mediaCapture.GetPreviewFrameAsync(videoFrame)) { // Collect the resulting frame. SoftwareBitmap bitmap = currentFrame.SoftwareBitmap; OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(ocrLanguage); if (ocrEngine == null) { rootPage.NotifyUser(ocrLanguage.DisplayName + " is not supported.", NotifyType.ErrorMessage); return; } var imgSource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(imgSource.PixelBuffer); PreviewImage.Source = imgSource; var ocrResult = await ocrEngine.RecognizeAsync(bitmap); // 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 = PreviewControl.ActualWidth / bitmap.PixelWidth, ScaleY = PreviewControl.ActualHeight / bitmap.PixelHeight }; 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. TextOverlay.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } // Iterate over recognized lines of text. foreach (var line in ocrResult.Lines) { // Iterate over words in line. foreach (var word in line.Words) { Debug.WriteLine(word.Text); //harrison start if (word.Text.Equals("Parking")) { foundValidPermit = true; } if (word.Text.Equals("ONTARIO")) { foundLicensePlate = true; } //harrison end // Define the TextBlock. var wordTextBlock = new TextBlock() { Text = word.Text, Style = (Style)this.Resources["ExtractedWordTextStyle"] }; WordOverlay wordBoxOverlay = new WordOverlay(word); // Keep references to word boxes. wordBoxes.Add(wordBoxOverlay); // Define position, background, etc. var overlay = new Border() { Child = wordTextBlock, Style = (Style)this.Resources["HighlightedWordBoxHorizontalLine"] }; // Bind word boxes to UI. overlay.SetBinding(Border.MarginProperty, wordBoxOverlay.CreateWordPositionBinding()); overlay.SetBinding(Border.WidthProperty, wordBoxOverlay.CreateWordWidthBinding()); overlay.SetBinding(Border.HeightProperty, wordBoxOverlay.CreateWordHeightBinding()); // Put the filled textblock in the results grid. TextOverlay.Children.Add(overlay); } } rootPage.NotifyUser("Image processed using " + ocrEngine.RecognizerLanguage.DisplayName + " language.", NotifyType.StatusMessage); } UpdateWordBoxTransform(); //harrison start if (foundValidPermit && foundLicensePlate) { showValidPermit(); } else if (foundLicensePlate) { showNoValidPermit(); } //PreviewControl.Visibility = Visibility.Collapsed; //Image.Visibility = Visibility.Visible; //harrison end //ExtractButton.Visibility = Visibility.Collapsed; //CameraButton.Visibility = Visibility.Visible; }
/// <summary> /// This is event handler for 'Extract' button. /// Recognizes text from image and displays it. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ExtractButton_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e) { ClearResults(); // Check if OcrEngine supports image resoulution. if (bitmap.PixelWidth > OcrEngine.MaxImageDimension || bitmap.PixelHeight > OcrEngine.MaxImageDimension) { rootPage.NotifyUser( String.Format("Bitmap dimensions ({0}x{1}) are too big for OCR.", bitmap.PixelWidth, bitmap.PixelHeight) + "Max image dimension is " + OcrEngine.MaxImageDimension + ".", NotifyType.ErrorMessage); return; } OcrEngine ocrEngine = null; if (UserLanguageToggle.IsOn) { // Try to create OcrEngine for first supported language from UserProfile.GlobalizationPreferences.Languages list. // If none of the languages are available on device, method returns null. ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages(); } else { // Try to create OcrEngine for specified language. // If language is not supported on device, method returns null. ocrEngine = OcrEngine.TryCreateFromLanguage(LanguageList.SelectedValue as Language); } if (ocrEngine != null) { // Recognize text from image. var ocrResult = await ocrEngine.RecognizeAsync(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. TextOverlay.RenderTransform = new RotateTransform { Angle = (double)ocrResult.TextAngle, CenterX = PreviewImage.ActualWidth / 2, CenterY = PreviewImage.ActualHeight / 2 }; } // Create overlay boxes over recognized words. foreach (var line in ocrResult.Lines) { Rect lineRect = Rect.Empty; foreach (var word in line.Words) { lineRect.Union(word.BoundingRect); } // Determine if line is horizontal or vertical. // Vertical lines are supported only in Chinese Traditional and Japanese languages. bool isVerticalLine = lineRect.Height > lineRect.Width; foreach (var word in line.Words) { WordOverlay wordBoxOverlay = new WordOverlay(word); // Keep references to word boxes. wordBoxes.Add(wordBoxOverlay); // Define overlay style. var overlay = new Border() { Style = isVerticalLine ? (Style)this.Resources["HighlightedWordBoxVerticalLine"] : (Style)this.Resources["HighlightedWordBoxHorizontalLine"] }; // Bind word boxes to UI. overlay.SetBinding(Border.MarginProperty, wordBoxOverlay.CreateWordPositionBinding()); overlay.SetBinding(Border.WidthProperty, wordBoxOverlay.CreateWordWidthBinding()); overlay.SetBinding(Border.HeightProperty, wordBoxOverlay.CreateWordHeightBinding()); // Put the filled textblock in the results grid. TextOverlay.Children.Add(overlay); } } // Rescale word boxes to match current UI size. UpdateWordBoxTransform(); rootPage.NotifyUser( "Image is OCRed for " + ocrEngine.RecognizerLanguage.DisplayName + " language.", NotifyType.StatusMessage); } else { rootPage.NotifyUser("Selected language is not available.", NotifyType.ErrorMessage); } }