private async Task TakePicture(CameraMessage cameraMessage) { if (this.isCameraInitializing) { await this.SendResult(new ResultMessage(cameraMessage) { ResultId = -3, Result = "Initializing" }); return; } await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, async () => { lock (this.keysInProcess) { this.keysInProcess["CAMERA"] = true; } await this.InitializeCamera(); var imageName = "photo_" + DateTime.Now.Ticks + ".jpg"; foreach ( var destination in this.destinations.Where(destination => destination.CheckPrefix(cameraMessage.Url))) { imageName = destination.ParseAddressForFileName(cameraMessage.Url); break; } StorageFile stream = null; try { var timeout = DateTime.Now.AddSeconds(5); while (!this.camera.isPreviewing && DateTime.Now < timeout) { await Task.Delay(250); } stream = await this.camera.Capture(imageName); } catch (Exception e) { await this.SendResult(new ResultMessage(cameraMessage) { ResultId = -99, Result = e.Message }); lock (this.keysInProcess) { this.keysInProcess["CAMERA"] = false; } return; } // stores the image in Azure BLOB Storage var memStream = new MemoryStream(); var fileStream = await stream.OpenStreamForReadAsync(); await fileStream.CopyToAsync(memStream); if (!await this.SendToDestinations(cameraMessage, memStream, KnownFolders.PicturesLibrary)) { await this.SendResult(new ResultMessage(cameraMessage) { Result = imageName }); } lock (this.keysInProcess) { this.keysInProcess["CAMERA"] = false; } }); }
private async void TakePicture(CameraMessage cameraMessage) { if (isCameraInitializing) { await SendResult(new ResultMessage(cameraMessage) {ResultId = -3, Result = "Initializing"}); return; } await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { lock (keysInProcess) { keysInProcess["CAMERA"] = true; } if (!isCameraInitialized) { isCameraInitializing = true; await InitializeCamera(); isCameraInitializing = false; } var imageName = "photo_" + DateTime.Now.Ticks + ".jpg"; foreach ( var destination in destinations.Where(destination => destination.CheckPrefix(cameraMessage.Url))) { imageName = destination.ParseAddressForFileName(cameraMessage.Url); break; } var stream = await camera.Capture(imageName); //stores the image in Azure BLOB Storage var memStream = new MemoryStream(); var fileStream = await stream.OpenStreamForReadAsync(); await fileStream.CopyToAsync(memStream); if (!await SendToDestinations(cameraMessage, memStream, KnownFolders.PicturesLibrary)) { await SendResult(new ResultMessage(cameraMessage) {Result = imageName}); } lock (keysInProcess) { keysInProcess["CAMERA"] = false; } }); }