/// <summary> /// Cleans up the camera resources (after stopping any video recording and/or preview if necessary) and unregisters from MediaCapture events /// </summary> /// <returns></returns> private async Task CleanupCameraAsync() { Debug.WriteLine("CleanupCameraAsync"); if (_isInitialized) { if (_isPreviewing) { // The call to stop the preview is included here for completeness, but can be // safely removed if a call to MediaCapture.Dispose() is being made later, // as the preview will be automatically stopped at that point await StopPreviewAsync(); } _isInitialized = false; } if (_mediaCapture != null) { _mediaCapture.Failed -= MediaCapture_Failed; _mediaCapture.Dispose(); _mediaCapture = null; } if (_rotationHelper != null) { _rotationHelper.OrientationChanged -= RotationHelper_OrientationChanged; _rotationHelper = null; } }
/// <summary> /// Gets the current orientation of the UI in relation to the device (when AutoRotationPreferences cannot be honored) and applies a corrective rotation to the preview /// </summary> private async Task SetPreviewRotationAsync() { // Only need to update the orientation if the camera is mounted on the device if (_externalCamera) { return; } // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames var rotation = _rotationHelper.GetCameraPreviewOrientation(); var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); props.Properties.Add(RotationKey, CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(rotation)); await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null); }
/// <summary> /// Takes a photo to a StorageFile and adds rotation metadata to it /// </summary> /// <returns></returns> private async Task TakePhotoAsync() { try { var stream = new InMemoryRandomAccessStream(); Debug.WriteLine("Taking photo..."); await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation()); var decoder = await BitmapDecoder.CreateAsync(stream); var encoder = await BitmapEncoder.CreateForInPlacePropertyEncodingAsync(decoder); var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } }; await encoder.BitmapProperties.SetPropertiesAsync(properties); await encoder.FlushAsync(); Debug.WriteLine("Photo saved in stream with orientation set!"); using (var dr = new DataReader(stream.GetInputStreamAt(0))) { var bytes = new byte[stream.Size]; await dr.LoadAsync((uint)stream.Size); dr.ReadBytes(bytes); SendEvent(deviceClient, Convert.ToBase64String(bytes)); } stream.Dispose(); } catch (Exception ex) { Debug.WriteLine("Exception when taking a photo: " + ex.ToString()); } }
/// <summary> /// Initializes the MediaCapture, registers events, gets camera device information for mirroring and rotating, starts preview and unlocks the UI /// </summary> /// <returns></returns> private async Task InitializeCameraAsync() { Debug.WriteLine("InitializeCameraAsync"); if (_mediaCapture == null) { // Attempt to get the back camera if one is available, but use any camera device if not var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back); if (cameraDevice == null) { Debug.WriteLine("No camera device found!"); return; } // Create MediaCapture and its settings _mediaCapture = new MediaCapture(); _mediaCapture.Failed += MediaCapture_Failed; var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; // Initialize MediaCapture try { await _mediaCapture.InitializeAsync(settings); _isInitialized = true; } catch (UnauthorizedAccessException) { Debug.WriteLine("The app was denied access to the camera"); } // If initialization succeeded, start the preview if (_isInitialized) { // Figure out where the camera is located if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown) { // No information on the location of the camera, assume it's an external camera, not integrated on the device _externalCamera = true; } else { // Camera is fixed on the device _externalCamera = false; // Only mirror the preview if the camera is on the front panel _mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front); } // Initialize rotationHelper _rotationHelper = new CameraRotationHelper(cameraDevice.EnclosureLocation); _rotationHelper.OrientationChanged += RotationHelper_OrientationChanged; await StartPreviewAsync(); } } }