Esempio n. 1
0
        /// <summary>
        /// Converts the given pixel array into a writeable bitmap.
        /// </summary>
        /// <param name="pixelArray">The pixel array.</param>
        /// <param name="width">The width of the image in pixels.</param>
        /// <param name="height">The height of the image in pixels.</param>
        /// <returns></returns>
        public static async Task <WriteableBitmap> PixelArrayToWriteableBitmapAsync(byte[] pixelArray, int width, int height)
        {
            string videoRecordFormat = CameraUtils.ResolveVideoRecordFormat(VideoEngine.Instance.MediaCapture);

            WriteableBitmap bitmap =
                (videoRecordFormat.Equals(VideoEncodingPropertiesSubTypeYUV2) ||
                 videoRecordFormat.Equals(VideoEncodingPropertiesSubTypeMJPG))
                    ? await ImageProcessingUtils.Yuy2PixelArrayToWriteableBitmapAsync(pixelArray, width, height)
                    : await ImageProcessingUtils.Nv12PixelArrayToWriteableBitmapAsync(pixelArray, width, height);

            return(bitmap);
        }
Esempio n. 2
0
        public async Task <bool> InitializeAsync()
        {
            if (Initialized)
            {
                // Already intialized
                return(true);
            }

            _deviceInformationCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            // Find the back camera
            _deviceInformation =
                (from camera in _deviceInformationCollection
                 where camera.EnclosureLocation != null &&
                 camera.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
                 select camera).FirstOrDefault();

            if (_deviceInformation == null && _deviceInformationCollection.Count != 0)
            {
                // Fallback to whatever is available (e.g. webcam on laptop)
                _deviceInformation = _deviceInformationCollection[0];
            }

            if (_deviceInformation == null)
            {
                throw new Exception("No camera device found");
            }

            MediaCapture = new MediaCapture();
            MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();

            settings.StreamingCaptureMode = StreamingCaptureMode.Video;
            settings.VideoDeviceId        = _deviceInformation.Id;
            await MediaCapture.InitializeAsync(settings);

#if WINDOWS_PHONE_APP
            _lightSensor.ReportInterval  = _lightSensor.MinimumReportInterval;
            _lightSensor.ReadingChanged += OnLightSensorReadingChangedAsync;
#endif

            _videoDeviceController = MediaCapture.VideoDeviceController;

            IList <VideoEncodingProperties> listOfPropertiesWithHighestFrameRate =
                CameraUtils.ResolveAllVideoEncodingPropertiesWithHighestFrameRate(_videoDeviceController, RecordMediaStreamType);

            if (listOfPropertiesWithHighestFrameRate != null && listOfPropertiesWithHighestFrameRate.Count > 0)
            {
                VideoEncodingProperties selectedRecordingVideoEncodingProperties = listOfPropertiesWithHighestFrameRate.ElementAt(0);
                uint selectedRecordingVideoWidth = selectedRecordingVideoEncodingProperties.Width;

                for (int i = 1; i < listOfPropertiesWithHighestFrameRate.Count; ++i)
                {
                    VideoEncodingProperties currentProperties = listOfPropertiesWithHighestFrameRate.ElementAt(i);

                    if (selectedRecordingVideoWidth > MaximumVideoWidth ||
                        (currentProperties.Width <= MaximumVideoWidth && currentProperties.Width > selectedRecordingVideoWidth))
                    {
                        selectedRecordingVideoEncodingProperties = currentProperties;
                        selectedRecordingVideoWidth = selectedRecordingVideoEncodingProperties.Width;
                    }
                }

                _hfrVideoEncodingProperties = selectedRecordingVideoEncodingProperties;
                await _videoDeviceController.SetMediaStreamPropertiesAsync(RecordMediaStreamType, selectedRecordingVideoEncodingProperties);

                VideoEncodingProperties previewVideoEncodingProperties =
                    CameraUtils.FindVideoEncodingProperties(
                        _videoDeviceController, PreviewMediaStreamType,
                        PreviewFrameRate, selectedRecordingVideoWidth, selectedRecordingVideoEncodingProperties.Height);

                System.Diagnostics.Debug.WriteLine("Highest framerate for recording is "
                                                   + CameraUtils.ResolveFrameRate(selectedRecordingVideoEncodingProperties)
                                                   + " frames per second with selected resolution of "
                                                   + selectedRecordingVideoWidth + "x" + selectedRecordingVideoEncodingProperties.Height
                                                   + "\nThe preview properties for viewfinder are "
                                                   + CameraUtils.ResolveFrameRate(previewVideoEncodingProperties) + " FPS and "
                                                   + previewVideoEncodingProperties.Width + "x" + previewVideoEncodingProperties.Height);

                await _videoDeviceController.SetMediaStreamPropertiesAsync(PreviewMediaStreamType, previewVideoEncodingProperties);
            }

            if (_videoDeviceController.WhiteBalanceControl.Supported)
            {
                await _videoDeviceController.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Fluorescent);
            }

            Initialized = true;
            return(Initialized);
        }