/// <summary> /// Capture a photo. /// </summary> /// <param name="bitmapPixelFormat"><see cref="BitmapPixelFormat"/></param> /// <returns>Captured image</returns> public async Task <SoftwareBitmap> CapturePhotoAsync(BitmapPixelFormat bitmapPixelFormat) { SoftwareBitmap convert = null; try { CapturedPhoto asyncOperation = null; try { asyncOperation = await _lowLagPhotoCapture.CaptureAsync(); } catch (Exception exception) { await OnProcessNotice(exception.ToString()); } using (var asyncOperationFrame = asyncOperation.Frame) { convert = SoftwareBitmap.Convert(asyncOperationFrame.SoftwareBitmap, bitmapPixelFormat); } } catch (Exception e) { await OnProcessNotice(e.ToString()); } return(convert); }
/// <summary> /// Will capture a single image and store it in the baseline images list. Each image will be used to determine if we have an alert or not. /// </summary> private async void CaptureBaseLineImage() { try { lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; WriteableBitmap writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer); byte[] imageBytes = new byte[4 * softwareBitmap.PixelWidth * softwareBitmap.PixelHeight]; softwareBitmap.CopyToBuffer(imageBytes.AsBuffer()); if (baselineImages.Count > 6) { baselineImages.Clear(); DisplayImages.Clear(); } baselineImages.Add(imageBytes); DisplayImages.Add(writeableBitmap); await lowLagCapture.FinishAsync(); } catch (Exception) { // Sometimes when you serial click the capture button we get an explosion... // Eat it and move on } }
public async Task <StorageFile> Save(CapturedPhoto capturedPhoto, StorageFolder storageFolder) { var softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; StorageFile outputFile = await storageFolder.CreateFileAsync("image.jpg", CreationCollisionOption.GenerateUniqueName); await SaveSoftwareBitmapToFile(softwareBitmap, outputFile); return(outputFile); }
/// <summary> /// Takes the camera output and displays the Clarifai predictions on the pane. /// </summary> /// <returns>a task</returns> private async Task RunPredictionsAndDisplayResponse() { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { try { if (_predictionApi == null) { return; } LowLagPhotoCapture lowLagCapture = await _mediaCapture.PrepareLowLagPhotoCaptureAsync( ImageEncodingProperties.CreateUncompressed(MediaPixelFormat .Bgra8)); CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; byte[] data = await EncodedBytes(softwareBitmap, BitmapEncoder.JpegEncoderId); try { ConceptsTextBlock.Text = await _predictionApi .PredictConcepts(data, _selectedModel); } catch (Exception ex) { ShowMessageToUser("Error: " + ex.Message); } try { (double camWidth, double camHeight) = CameraOutputDimensions(); List <Rect> rects = await _predictionApi.PredictFaces(data, camWidth, camHeight); CameraGrid.Children.Clear(); foreach (Rect r in rects) { CameraGrid.Children.Add(CreateGuiRectangle(r, camWidth, camHeight)); } } catch (Exception ex) { ShowMessageToUser("Error: " + ex.Message); } await lowLagCapture.FinishAsync(); } catch (COMException) // This is thrown when application exits. { // No need to handle this since the application is exiting. } });
/// <summary> /// photo lowlag capture /// </summary> /// <returns></returns> private async Task CaptureLowLagPhotoAsync() { try { var folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("Cam360", CreationCollisionOption.OpenIfExists); var file = await folder.CreateFileAsync("PhotoWithRecord.jpg", CreationCollisionOption.GenerateUniqueName); var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite); if (_lowLagPhotoCapture != null) { CapturedPhoto photo = await _lowLagPhotoCapture.CaptureAsync(); var properties = photo.Frame.BitmapProperties; var softwareBitmap = photo.Frame.SoftwareBitmap; if (softwareBitmap != null) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream); if (properties != null) { await encoder.BitmapProperties.SetPropertiesAsync(properties); } encoder.SetSoftwareBitmap(softwareBitmap); await encoder.FlushAsync(); } else { // This is a jpeg frame var decoder = await BitmapDecoder.CreateAsync(photo.Frame.CloneStream()); var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder); await encoder.FlushAsync(); } } } catch (Exception ex) { string errorMessage = string.Format("Photo capture did not complete: {0}", ex.Message); Debug.Write(errorMessage); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { await new MessageDialog(errorMessage).ShowAsync(); }); } }
internal async Task <SoftwareBitmap> CaptureImage() { if (isPreviewing) { CapturedPhoto capturedPhoto = null; capturedPhoto = await lowLagCapture.CaptureAsync(); var softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; return(softwareBitmap); } return(null); }
async private void CaptureLagPhotoCapture_Click(object sender, RoutedEventArgs e) { // Take photo CapturedPhoto photo = await lowLagCaptureMgr.CaptureAsync(); // Get photo as a BitmapImage BitmapImage bitmap = new BitmapImage(); await bitmap.SetSourceAsync(photo.Frame); // Get thumbnail as a BitmapImage BitmapImage bitmapThumbnail = new BitmapImage(); await bitmapThumbnail.SetSourceAsync(photo.Thumbnail); // imageLowLagPhoto is a <Image> object defined in XAML imageLowLagPhoto.Source = bitmap; // imageLowLagThumbnail is a <Image> object defined in XAML imageLowLagThumbnail.Source = bitmapThumbnail; }
private async void M_videoTimer_Tick(object sender, object e) { if (m_mediaCapture == null) { m_videoTimer.Stop(); m_videoTimer = null; return; } lock (m_mediaCapture) { if (isrunning) { return; } isrunning = true; } LowLagPhotoCapture cap = await m_mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateBmp()); CapturedPhoto photo = await cap.CaptureAsync(); WriteableBitmap bitmap = new WriteableBitmap((int)photo.Frame.Width, (int)photo.Frame.Height); await bitmap.SetSourceAsync(photo.Frame.AsStreamForRead().AsRandomAccessStream()); bitmap.Invalidate(); byte[] imageArray = bitmap.PixelBuffer.ToArray(); int starting = (bitmap.PixelHeight / 2) * bitmap.PixelWidth * 4 + (bitmap.PixelWidth * 2); byte blue = imageArray[starting]; byte green = imageArray[starting + 1]; byte red = imageArray[starting + 2]; m_liveColor[0, 0] = red; m_liveColor[0, 1] = green; m_liveColor[0, 2] = blue; MakeLiveSettingsUpdate(); isrunning = false; }
/// <summary> /// Starts the current photo capture. /// </summary> /// <returns>Awaitable task.</returns> /// <exception cref="InvalidOperationException">The current capture is not initialized.</exception> public override Task StartAsync() { return(Task.Run(() => { if (this.captureOperation != null) { Tracing.Trace("LowLagCapture: Capture is already started."); return; } // We should notify listeners before the actual frame is captured. this.NotifyStarted(); // Save operation to a temporary variable, so it won't be null in the task we start next. IAsyncOperation <CapturedPhoto> operation = this.photoCapture.CaptureAsync(); this.captureOperation = operation; Task.Run(async() => { try { CapturedPhoto photo = await operation; // No need to cancel the completed operation. this.captureOperation = null; EventHandler <CapturedPhoto> handler = this.PhotoCaptured; if (handler != null) { handler.Invoke(this, photo); } } catch (Exception e) { Tracing.Trace("LowLagCapture: Exception while waiting for captured photo:\r\n{0}", e); } await this.StopAsync(); }); })); }
public static async Task <SoftwareBitmap> CaptureImage(MediaCapture MediaCaptureElement) { if (lowLagCapture == null) { lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); } try { CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; return(softwareBitmap); } catch { lowLagCapture = null; return(await CaptureImage(MediaCaptureElement)); } }
public async Task <SoftwareBitmap> GetFrameAsync() { if (_mediaCapture != null) { try { CapturedPhoto capturedPhoto = await _lowLagCapture.CaptureAsync(); SoftwareBitmap swBmp = capturedPhoto.Frame.SoftwareBitmap; return(swBmp); } catch (COMException e) { Debug.WriteLine(e); return(null); } } else { return(new SoftwareBitmap(BitmapPixelFormat.Bgra8, 400, 300, BitmapAlphaMode.Ignore)); } }
/// <summary> /// The <see cref="LowLagCapture.PhotoCaptured"/> event handler. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="capturedPhoto">Captured photo.</param> private void LowLagCaptureOnPhotoCaptured(object sender, CapturedPhoto capturedPhoto) { Tracing.Trace("PhotoCamera: Low lag photo captured."); this.NotifyPhotoCaptured(CapturedImageFrame.CreateFromCapturedFrame(capturedPhoto.Frame, this.Orientation, this.CameraType)); }
private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { try { SummarizedText.Blocks.Clear(); CapturedPhoto frame = await CapturePreviewFrame(); await mediaCapture.StopPreviewAsync(); EnableResultPane(); var bitmap = TLDR.FindMainParagraph.ExtractMainParagraph(frame.Frame.SoftwareBitmap); var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages(); var ocrResult = await ocrEngine.RecognizeAsync(bitmap); await ShowResultImage(bitmap); var client = new HttpClient(); var requestObject = new RequestObject() { content = ocrResult.Text }; var requestObjectString = JsonConvert.SerializeObject(requestObject); var dataWriter = new DataWriter() { UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8 }; dataWriter.WriteString(requestObjectString); var buffer = dataWriter.DetachBuffer(); var messageContent = new HttpBufferContent(buffer); messageContent.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/json"); var message = new HttpRequestMessage(HttpMethod.Post, ConfigConstants.EndPoint) { Content = messageContent }; var response = await client.SendRequestAsync(message); var responseBuffer = await response.Content.ReadAsBufferAsync(); var dataReader = DataReader.FromBuffer(responseBuffer); dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; var responseString = dataReader.ReadString(responseBuffer.Length); var responseObject = JsonConvert.DeserializeObject <ResponseObject>(responseString); var summaryString = responseObject.summary; var phrases = responseObject.key_phrases.Select(phrase => phrase.phrase).ToArray(); var stringParts = summaryString.Split(phrases, StringSplitOptions.RemoveEmptyEntries); keyPhrases = responseObject.key_phrases.ToDictionary(phrase => phrase.phrase); var keyPhraseIndices = phrases.Select(phrase => new Tuple <int, String>(summaryString.IndexOf(phrase), phrase)).OrderBy(pair => pair.Item1).ToArray(); var paragraph = new Paragraph(); var normalStringStartIndex = 0; foreach (var pair in keyPhraseIndices) { if (normalStringStartIndex < pair.Item1) { paragraph.Inlines.Add(new Run() { Text = summaryString.Substring(normalStringStartIndex, pair.Item1 - normalStringStartIndex) }); } var uiContainter = new InlineUIContainer(); var button = new Button() { Content = pair.Item2, Background = new SolidColorBrush(Colors.Yellow) }; button.Margin = new Thickness(0, 0, 0, -6); button.Padding = new Thickness(0, 0, 0, 0); button.Click += KeyPhraseClick; uiContainter.Child = button; paragraph.Inlines.Add(uiContainter); normalStringStartIndex = pair.Item1 + pair.Item2.Length; } if (normalStringStartIndex < summaryString.Length - 1) { paragraph.Inlines.Add(new Run() { Text = summaryString.Substring(normalStringStartIndex) }); } SummarizedText.Blocks.Add(paragraph); } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { ProgressBar.Visibility = Visibility.Collapsed; } }
private async void Camera_Click(object sender, RoutedEventArgs e) { if (isPreviewing) { CameraControl.Visibility = Visibility.Collapsed; LowLagPhotoCapture lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync( ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight) { softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } var source = new SoftwareBitmapSource(); await source.SetBitmapAsync(softwareBitmap); Job j = DataContext as Job; if (j != null) { j.Photo = softwareBitmap; } await lowLagCapture.FinishAsync(); await CleanupCameraAsync(); isPreviewing = false; } else { CameraControl.Visibility = Visibility.Visible; try { MediaCaptureInitializationSettings mediaInitSettings = new MediaCaptureInitializationSettings(); DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); foreach (var device in devices) { EnclosureLocation loc = device.EnclosureLocation; Debug.WriteLine($"Location: {loc.Panel.ToString()} ID: {device.Id}"); if (sender is Button && ((Button)sender).Tag.ToString() == loc.Panel.ToString()) { mediaInitSettings.VideoDeviceId = device.Id; mediaInitSettings.StreamingCaptureMode = StreamingCaptureMode.Video; mediaInitSettings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview; break; } } mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(mediaInitSettings); var resolutions = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties( MediaStreamType.Photo).Select(x => x as VideoEncodingProperties); var minRes = resolutions.OrderBy(x => x.Height * x.Width).FirstOrDefault(); await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync( MediaStreamType.VideoPreview, minRes); PreviewControl.Source = mediaCapture; await mediaCapture.StartPreviewAsync(); isPreviewing = true; displayRequest.RequestActive(); DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; } catch (UnauthorizedAccessException) { // This will be thrown if the user denied access to the camera in privacy settings Debug.WriteLine("The app was denied access to the camera"); } catch (Exception ex) { Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message); } } }
/// <summary> /// Captures a photo and sends it off for analysis /// </summary> private async void TakePhoto() { try { lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; await lowLagCapture.FinishAsync(); byte[] imageBytes = new byte[4 * softwareBitmap.PixelWidth * softwareBitmap.PixelHeight]; softwareBitmap.CopyToBuffer(imageBytes.AsBuffer()); bool isAlert = CheckForMotion(imageBytes); if (isAlert) { WriteableBitmap writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer); AlertDisplayImages.Add(new AlertDisplayImageModel() { AlertDisplayImage = writeableBitmap, AlertDisplayCaption = DateTime.Now.ToString() }); delayTimer.Interval = new TimeSpan(0, 0, ConfigurationSettings.AppConfig.AlertDelay); captureTimer.Stop(); delayTimer.Tick += OnDelayTimerTick; delayTimer.Start(); // It seems silly that we need to capture a second image but the first image that was captured isn't in a format that can // be easily emailed. This being the case, I decided it'd just be easier to grab another capture in the correct format and // email it off. The delta between the images is negligable var stream = new InMemoryRandomAccessStream(); await MediaCaptureElement.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); await Task.Delay(10); streamList.Add(stream); if (AutoSaveAlertImages) { } if (ConfigurationSettings.AppConfig.SendEmails && streamList.Count > ConfigurationSettings.AppConfig.AlertThreshold) { captureTimer.Stop(); await SendAlertEmail(streamList); await Task.Delay(new TimeSpan(0, 1, 0)); } } } catch (Exception error) { // Getting random COM errors. Just eat it and continue. There's nothing I can do about this. } }