Esempio n. 1
0
        public async static Task <String> ProcessTakePictureCommand(TakePictureCommandEventArgs e)
        {
            string photoFileName = "";

            try
            {
                DeviceInformation camera = Cameras[e.Camera];

                MediaCapture mediaCapture = new MediaCapture();

                MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings {
                    VideoDeviceId = camera.Id, StreamingCaptureMode = StreamingCaptureMode.Video
                };

                await mediaCapture.InitializeAsync(settings);

                // Photo StorageFile.
                StorageFile photoFile;

                // Photo file name.
                //string photoFileName = buildDateTimeStamp() + ".jpg";
                photoFileName = BuildFileName(e);

                // Create the PhotoFile in the PicturesLibrary System folder.
                photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync(photoFileName, CreationCollisionOption.ReplaceExisting);

                // Create an ImageEncodingProperties object for the photo file.
                ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();

                // Using the MediaCapture, capture the photo and save it to the PicturesLibrary System folder.
                await mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoFile);

                // Create an Azure CloudStorageAccount object.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=" + azureStorageAccount + ";AccountKey=" + azureStoragePrimaryKey);

                // Create a CloudBlobClient object using the CloudStorageAccount.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                // Create a CloudBlobContain object using the CloudBlobClient.
                CloudBlobContainer container = blobClient.GetContainerReference(azureStorageContainer);

                //  Create a CloudBlockBlob using the CloudBlobContainer.
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(photoFileName);

                //  Using the CloudBlockBlob, Upload the file to Azure.
                await blockBlob.UploadFromFileAsync(photoFile);

                // Clean up the media capture
                mediaCapture.Dispose();

                mediaCapture = null;
            }
            catch (Exception ex)
            {
                throw(ex);
            }

            return(photoFileName);

            //  catch (UnauthorizedAccessException)
            //  {
            //    // This will be thrown if the user denied access to the camera in privacy settings
            //    System.Diagnostics.Debug.WriteLine("The app was denied access to the camera");
            //  }
            //  catch (Exception ex)
            //  {
            //    System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
            //  }
            //}
        }
Esempio n. 2
0
        private async void IoTHubDeviceService_TakePictureCommandReceived(object sender, TakePictureCommandEventArgs e)
        {
            //Using a zero based index... Hmm...
            if (e.Camera >= 0 && e.Camera < CameraService.Cameras.Count)
            {
                await SoundService.PlayAudioFileAsync("pictake01.wav");

                //Debug.WriteLine(String.Format("Taking picture for Team {0} with camera: {1}", e.CommandMessage.Team, e.Camera));
                try
                {
                    //string fileName = await CameraService.ProcessTakePictureCommand(e);
                    string fileName = await CameraService.ProcessTakePictureCommandFixed(e);

                    string msg = String.Format("Team {0}, the photo from camera {1} has been uploaded to '{0}-<correlation-id>.jpg'", e.CommandMessage.Team, e.Camera);
                    SendDeviceMessage(e.CommandMessage.Team, e.Message.MessageId, msg);
                }
                catch (Exception ex)
                {
                    string msg = String.Format("Team {0}, there was a problem uploading the photo from camera {1}.", e.CommandMessage.Team, e.Camera);
                    SendDeviceMessage(e.CommandMessage.Team, e.Message.MessageId, msg);
                }
            }
            else
            {
                await SoundService.PlayAudioFileAsync("soundeffect02.wav");

                string msg = String.Format("Team {0}, the following camera is invalid: {1}", e.CommandMessage.Team, e.Camera);
                SendDeviceMessage(e.CommandMessage.Team, e.Message.MessageId, msg);
            }
        }
Esempio n. 3
0
 private static string BuildFileName(TakePictureCommandEventArgs e)
 {
     return(String.Format("{0}-{1}.jpg", e.CommandMessage.Team, e.Message.MessageId));
 }