Exemplo n.º 1
0
        async void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Size   targetMediaElementSize = new Size(640, 480);
            double aspectRatio            = 4.0 / 3.0;

            // 1. Open camera
            if (m_camera == null)
            {
                var  captureRes         = PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back);
                Size selectedCaptureRes = captureRes.Where(res => Math.Abs(aspectRatio - res.Width / res.Height) <= 0.1)
                                          .OrderBy(res => res.Width)
                                          .Last();
                m_camera = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, selectedCaptureRes);

                m_camera.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, m_camera.SensorLocation == CameraSensorLocation.Back ? m_camera.SensorRotationInDegrees : -m_camera.SensorRotationInDegrees);

                var  previewRes         = PhotoCaptureDevice.GetAvailablePreviewResolutions(CameraSensorLocation.Back);
                Size selectedPreviewRes = previewRes.Where(res => Math.Abs(aspectRatio - res.Width / res.Height) <= 0.1)
                                          .Where(res => (res.Height >= targetMediaElementSize.Height) && (res.Width >= targetMediaElementSize.Width))
                                          .OrderBy(res => res.Width)
                                          .First();
                await m_camera.SetPreviewResolutionAsync(selectedPreviewRes);

                cameraEffect.CaptureDevice = m_camera;
            }

            // Always create a new source, otherwise the MediaElement will not start.
            source = new CameraStreamSource(cameraEffect, targetMediaElementSize);
            MyCameraMediaElement.SetSource(source);

            m_timer          = new DispatcherTimer();
            m_timer.Interval = new TimeSpan(0, 0, 0, 1, 0); // Tick every 1s.
            m_timer.Tick    += m_timer_Tick;
            m_timer.Start();
        }
        async void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Size targetMediaElementSize = new Size(640, 480);
            double aspectRatio = 4.0/3.0;

            // 1. Open camera 
            if (m_camera == null)
            {
                var captureRes = PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back);
                Size selectedCaptureRes = captureRes.Where(res => Math.Abs(aspectRatio - res.Width/res.Height ) <= 0.1)
                                                    .OrderBy(res => res.Width)
                                                    .Last();
                m_camera = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, selectedCaptureRes);
                m_camera.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, m_camera.SensorLocation == CameraSensorLocation.Back ? m_camera.SensorRotationInDegrees : -m_camera.SensorRotationInDegrees);
               
                var previewRes = PhotoCaptureDevice.GetAvailablePreviewResolutions(CameraSensorLocation.Back);
                Size selectedPreviewRes = previewRes.Where(res => Math.Abs(aspectRatio - res.Width/res.Height ) <= 0.1) 
                                                    .Where(res => (res.Height >= targetMediaElementSize.Height) && (res.Width >= targetMediaElementSize.Width))
                                                    .OrderBy(res => res.Width)
                                                    .First();
                await m_camera.SetPreviewResolutionAsync(selectedPreviewRes);
                cameraEffect.CaptureDevice = m_camera;
            }

            // Always create a new source, otherwise the MediaElement will not start.
            source = new CameraStreamSource(cameraEffect, targetMediaElementSize);
            MyCameraMediaElement.SetSource(source);

            m_timer = new DispatcherTimer();
            m_timer.Interval = new TimeSpan(0, 0, 0, 1, 0); // Tick every 1s.
            m_timer.Tick += m_timer_Tick;
            m_timer.Start();
        }