public async Task TakePhotoAsync(Action <SoftwareBitmapSource> callback) { //kada uslika postaviti svoj stream Slika = new InMemoryRandomAccessStream(); try { //konvertovati uslikano u Software bitmap da se moze prikazati u image kontroli await MediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateBmp(), Slika); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(Slika); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SlikaBitmap = new SoftwareBitmapSource(); await SlikaBitmap.SetBitmapAsync(softwareBitmapBGR8); callback(SlikaBitmap); } catch (Exception ex) { Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString()); } }
//GENERAL PURPOSE FUNCTION ///<summary> /// Takes photo, saves photo file on temp folder, sets a place of grid as new photo and then returns user with attribute PhotoFace. ///</summary> private async Task <User> TakePhoto(String filename) { var photoFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateBmp(), photoFile); IRandomAccessStream photoStream = await photoFile.OpenReadAsync(); BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(photoStream); Image img = new Image() { Width = 100, Source = bitmap }; //Add photo to grid photoContainer.Children.Add(img); Grid.SetRow(img, photoContainer.RowDefinitions.Count - 1); Grid.SetColumn(img, photoContainer.ColumnDefinitions.Count - 1); User u = new User(); u.PhotoFace = img; await Print("Picture taken!"); return(u); }
async Task <IEnumerable <RawEmotion> > CaptureEmotionAsync() { _isProcessing = true; RawEmotion[] result; try { var photoFile = await _photoService.CreateAsync(); var imageProperties = ImageEncodingProperties.CreateBmp(); await _mediaManager.CapturePhotoToStorageFileAsync(imageProperties, photoFile); result = await _emotionClient.RecognizeAsync(await photoFile.OpenStreamForReadAsync()); } finally { await _photoService.CleanupAsync(); _isProcessing = false; } return(result.IsNullOrEmpty() ? await Task.FromResult(Enumerable.Empty <RawEmotion>()) : result); }
//private async void btnCapture_OnClick(object sender, RoutedEventArgs e) //{ // // Acknowledge that the user has triggered a button to capture a barcode. // this.lblMsg.Text = "-------"; // // Capture the photo from the camera to a storage-file. // ImageEncodingProperties fmtImage = ImageEncodingProperties.CreateJpeg(); // StorageLibrary libPhoto = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); // StorageFile storefile = // await libPhoto.SaveFolder.CreateFileAsync("BarcodePhoto.jpg", // CreationCollisionOption.ReplaceExisting); // await this.captureMgr.CapturePhotoToStorageFileAsync(fmtImage, storefile); // // Tell the user that we have taken a picture. // this.lblMsg.Text = "Picture taken"; //} /// <summary> /// uses memory stream /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnCapture_OnClick(object sender, RoutedEventArgs e) { // Acknowledge that the user has triggered a button to capture a barcode. this.lblMsg.Text = "-------"; // Capture the photo from the camera to a storage-file. ImageEncodingProperties fmtImage = ImageEncodingProperties.CreateBmp(); byte[] bytes; using (var captureStream = new InMemoryRandomAccessStream()) { await this.captureMgr.CapturePhotoToStreamAsync(fmtImage, captureStream); captureStream.Seek(0); bytes = new byte[captureStream.Size]; await captureStream.ReadAsync(bytes.AsBuffer(), (uint)captureStream.Size, InputStreamOptions.None); //var decoder = await BitmapDecoder.CreateAsync(captureStream); //var pixelData = await decoder.GetPixelDataAsync(); //bytes = pixelData.DetachPixelData(); } var response = await MakeOCRRequest(bytes); bool found = false; if (response["regions"] != null && ((JArray)response["regions"]).Count > 0) { var region = ((JArray)response["regions"])[0]; if (region["lines"] != null && ((JArray)region["lines"]).Count > 0) { var line = ((JArray)region["lines"])[0]; if (line["words"] != null && ((JArray)line["words"]).Count > 0) { var word = ((JArray)line["words"])[0]; found = true; var data = new { found, word }; this.device.SendDeviceToCloudMessagesAsync(JsonConvert.SerializeObject(data), found); // Tell the user that we have taken a picture. this.lblMsg.Text = Regex.Replace(JsonConvert.SerializeObject(word), @"\t|\n|\r|\s", ""); } } } if (!found) { var data = new { found = false, response }; this.device.SendDeviceToCloudMessagesAsync(JsonConvert.SerializeObject(data), false); this.lblMsg.Text = Regex.Replace(response.ToString(), @"\t|\n|\r|\s", ""); } // Tell the user that we have taken a picture. }
public async Task <SoftwareBitmap> CapturePhotoToSoftwareBitMap() { var imageEncodingProperties = ImageEncodingProperties.CreateBmp(); var memoryStream = new InMemoryRandomAccessStream(); await MediaCapture.CapturePhotoToStreamAsync(imageEncodingProperties, memoryStream); var bitMapDecoder = await BitmapDecoder.CreateAsync(memoryStream); return(await bitMapDecoder.GetSoftwareBitmapAsync()); }
public async Task TakePicture(string image_name) { // it creates the file to which image will be saved to specific path var file = await KnownFolders.PicturesLibrary.CreateFileAsync(image_name, CreationCollisionOption.ReplaceExisting); // it sets the type of saved picture as a bitmap ImageEncodingProperties Picture = ImageEncodingProperties.CreateBmp(); // update the file with image data await Capture.CapturePhotoToStorageFileAsync(Picture, file); }
private async void Camera_Button_Clicked(object sender, RoutedEventArgs e) { var stream = new InMemoryRandomAccessStream(); Debug.WriteLine("Taking photo..."); await cameraMediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateBmp(), stream); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); ImageBrush imageBrush = new ImageBrush { ImageSource = bitmapSource }; ImageCanvas.Background = imageBrush; detectionStatus.Text = ""; using (var ms = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms); encoder.SetSoftwareBitmap(softwareBitmap); await encoder.FlushAsync(); DetectedFace[] detectedFaces = await UploadAndDetectFacesStream(ms.AsStream()); if (detectedFaces != null) { ResultBox.Items.Clear(); DisplayParsedResults(detectedFaces); DisplayAllResults(detectedFaces); DrawFaceRectangleStream(detectedFaces, stream); detectionStatus.Text = "Detection Done"; } else { detectionStatus.Text = "Detection Failed"; } } }
public async Task CapturePhoto(Stream stream, uint width = 1920, uint height = 1080) { // Capture a semaphore so we don't use the card twice _logger.LogInformation($"Waiting on camera semaphore to become available"); await Semaphore.WaitAsync(); _logger.LogInformation($"Captured camera semaphore"); MediaCaptureInitializationSettings mcis = new MediaCaptureInitializationSettings(); if (!String.IsNullOrEmpty(DeviceId)) { mcis.VideoDeviceId = DeviceId; } MediaCapture mediaCapture = null; try { _logger.LogInformation($"Initializing media capture for bitmap capturing ({width}x{height})"); mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(mcis); ImageEncodingProperties iep = ImageEncodingProperties.CreateBmp(); iep.Width = width; iep.Height = height; using (InMemoryRandomAccessStream imras = new InMemoryRandomAccessStream()) { _logger.LogInformation($"Capturing photo to random access stream"); await mediaCapture.CapturePhotoToStreamAsync(iep, imras); _logger.LogInformation($"Copying RAS to IO Stream"); var imrasNative = imras.AsStream(); imrasNative.Position = 0; await imrasNative.CopyToAsync(stream); } } catch { throw; } finally { if (mediaCapture != null) { mediaCapture.Dispose(); } _logger.LogInformation($"Releasing camera semaphore"); Semaphore.Release(); } }
private async Task TakePhotoAsync() { var stream = new InMemoryRandomAccessStream(); try { await _mediaCapture.VideoDeviceController.FocusControl.FocusAsync(); Debug.WriteLine("Taking photo..."); await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateBmp(), stream); Debug.WriteLine("Photo taken!"); var decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap sBitmap = await decoder.GetSoftwareBitmapAsync(); WriteableBitmap bitmap = new WriteableBitmap(sBitmap.PixelWidth, sBitmap.PixelHeight); sBitmap.CopyToBuffer(bitmap.PixelBuffer); sBitmap.Dispose(); var buffer = bitmap.PixelBuffer; int center = ((bitmap.PixelWidth * (bitmap.PixelHeight / 2)) + bitmap.PixelWidth / 2) * 4; byte b = buffer.GetByte((uint)center); byte g = buffer.GetByte((uint)center + 1); byte r = buffer.GetByte((uint)center + 2); Debug.WriteLine("Red: " + r); Debug.WriteLine("Green: " + g); Debug.WriteLine("Blue: " + b); ///_displayRequest.RequestRelease(); ///await _mediaCapture.StopPreviewAsync(); ///_mediaCapture.Dispose(); Frame.Navigate(typeof(ItemsPage), new PixelColor() { Red = r, Green = g, Blue = b }); } catch (Exception ex) { Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString()); } }
/// <summary> /// Retrieves the raw byte data from the media capture. /// </summary> /// <returns>Raw byte data from the media capture.</returns> private async Task <byte[]> GetPixelDataFromCapture() { using (var stream = new InMemoryRandomAccessStream()) { await this.MediaCapture.CapturePhotoToStreamAsync( ImageEncodingProperties.CreateBmp(), stream); var decoder = await BitmapDecoder.CreateAsync(stream); var dataProvider = await decoder.GetPixelDataAsync(); return(dataProvider.DetachPixelData()); } }
/// <summary> /// Gets the Image encondings properties. /// </summary> /// <param name="format">The selected format.</param> /// <returns>The properties of the selected format.</returns> private ImageEncodingProperties GetImageEncodingProperties(PhotoCaptureFormat format) { switch (format) { case PhotoCaptureFormat.JPG: return(ImageEncodingProperties.CreateJpeg()); case PhotoCaptureFormat.BMP: return(ImageEncodingProperties.CreateBmp()); case PhotoCaptureFormat.PNG: default: return(ImageEncodingProperties.CreatePng()); } }
public async Task <SoftwareBitmap> CapturePhoto(MediaCapture mediaCapture) { using (var captureStream = new InMemoryRandomAccessStream()) { await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateBmp(), captureStream); var lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); var capturedPhoto = await lowLagCapture.CaptureAsync(); var softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; await lowLagCapture.FinishAsync(); return(softwareBitmap); } }
async void InitMediaCapture() { _imageProperties = ImageEncodingProperties.CreateBmp(); _imageProperties.Width = WIDTH; _imageProperties.Height = HEIGHT; Camera = new MediaCapture(); var settings = new MediaCaptureInitializationSettings { MediaCategory = MediaCategory.Media }; await Camera.InitializeAsync(settings); Camera.VideoDeviceController.DesiredOptimization = MediaCaptureOptimization.Quality; Camera.VideoDeviceController.PrimaryUse = CaptureUse.Photo; WhiteBalance = Camera.VideoDeviceController.WhiteBalance; WhiteBalSlider.Minimum = WhiteBalance.Capabilities.Min; WhiteBalSlider.Maximum = WhiteBalance.Capabilities.Max; Double value; WhiteBalance.TryGetValue(out value); WhiteBalSlider.Value = value; PreviewElement.Source = Camera; await Camera.StartPreviewAsync(); await TakePicture(); Task.Run(async() => { while (true) { await TakePicture(); Task.Delay(100).Wait(); } }); InitTimer(); }
private async void _frameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args) { if (_taskRunning == true) { return; } using (InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream()) { _taskRunning = true; var imgFormat = ImageEncodingProperties.CreateBmp(); var previewProperties = _webcam.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; await _webcam.CapturePhotoToStreamAsync(imgFormat, randomAccessStream); randomAccessStream.Seek(0L); await EvaluateFromImageAsync(randomAccessStream); _taskRunning = false; } }
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; }