如何查询相机分辨率及其列表

时间:2012-07-25 05:50:19

标签: c# windows-phone-7 xaml camera

我只是想查询WP7设备的摄像头分辨率,并将这些分辨率添加到ListPicker中,以便用户可以选择他或她希望进行视频播放的分辨率。我的主页上显示了我的视频,分辨率ListPicker在我的SettingsPage上,所以我还需要将这个值从SettingsPage传递给MainPage,以便正确应用所选的分辨率。到目前为止,我所拥有的内容如下,虽然我不确定如何获取分辨率并将它们添加到ListPicker然后传递此值以在MainPage中使用?

MainPage.xaml中

<Border x:Name="videoRectangle" Grid.Row="0" Grid.ColumnSpan="2" >
            <Border.Background>
                <VideoBrush  x:Name="viewfinderBrush">
                    <VideoBrush.RelativeTransform>
                        <CompositeTransform x:Name="viewfinderBrushTransform" CenterX=".5" CenterY=".5" Rotation="90" />
                    </VideoBrush.RelativeTransform>
                </VideoBrush>
            </Border.Background>
 </Border>

MainPage.xaml.cs中

#region Fields
    //Declare a field of type PhotoCamera that will hold a reference to the camera
    private PhotoCamera camera;        

    #endregion

    #region Ctr

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    #endregion

    #region Navigation

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        //if camera = null {  .. } <-- is this necessary beforehand?
        // Check to see if the camera is available on the device.
        if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) ||
             (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true))
        {
            // Initialize the camera, when available.
            if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
            {
                // Use front-facing camera if available.
                camera = new PhotoCamera(CameraType.Primary);

                camera.Initialized += camera_Initialized;
                viewfinderBrush.SetSource(camera);
            }
            else
            {
                // The Primary camera is not supported on the device.
                this.Dispatcher.BeginInvoke(delegate()
                {
                    // Write message.
                    MessageBox.Show("Primary camera is not available on this device.", "Error!", MessageBoxButton.OK);
                });
            }
        }
        else
        {
            // No camera is supported on the device.
            //this.Dispatcher.BeginInvoke(delegate()
            //{
                // Write message.
                MessageBox.Show("Primary camera is available on this device.", "Error!", MessageBoxButton.OK);
            //});
        }

        base.OnNavigatedTo(e);
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        if (camera != null)
        {
            camera.Dispose();
            camera.Initialized -= camera_Initialized;
            camera = null;
        }

        base.OnNavigatedFrom(e);
    }

    #endregion

    void camera_Initialized(object sender, CameraOperationCompletedEventArgs e)
    {
        if (e.Succeeded)
        {
            //var res = from resolution in camera.AvailableResolutions
            //          //change to best resolution on camera
            //          //http://msdn.microsoft.com/en-us/library/hh202951(v=VS.92).aspx
            //          where resolution.Width == 640
            //          select resolution;

            //camera.Resolution = res.First();

            //***apply camera resolution here!?

            this.Dispatcher.BeginInvoke(delegate()
            {
                ShowRegularVideo();
            });
        }

        //throw new NotImplementedException();
    }

    private void ShowRegularVideo()
    {
        videoRectangle.Width = this.ActualWidth;
        videoRectangle.Height = this.ActualHeight;
    }

SettingsPage.xaml

<toolkit:ListPicker x:Name="ResolutionListPicker" Header="Resolutions" Grid.Row="3" Grid.ColumnSpan="2"                                            
                                        SelectedIndex="{Binding}"
                                        SelectionChanged="ResolutionListPicker_SelectionChanged"/>

SettingsPage.xaml.cs

#region Fields

    //Declare a field of type PhotoCamera that will hold a reference to the camera
    private PhotoCamera camera; 

    List<int> resolutionsList;

    #endregion

    #region Ctr

    public Settings()
    {
        InitializeComponent();
    }

    #endregion

    #region Navigation

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        GetResolutions();
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
    }

    #endregion

    private void GetResolutions()
    {
        resolutionsList = new List<int>();

        //var res = from resolution in camera.AvailableResolutions
        //          //change to best resolution on camera
        //          //http://msdn.microsoft.com/en-us/library/hh202951(v=VS.92).aspx
        //          where resolution.Width == 640
        //          select resolution;

        //camera.Resolution = res.First();

        for (int i = 0; i < camera.AvailableResolutions.Count(); i++)
        {
            //resolutionsList.Add(...)
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为您从如何:为Windows Phone创建基础相机应用程序开始 http://msdn.microsoft.com/en-us/library/hh202956(v=vs.92).aspx

如果您查看Res按钮,它会显示如何更改分辨率。如果没有看看如何:在Windows Phone应用程序中调整捕获的图片分辨率 http://msdn.microsoft.com/en-us/library/hh202951(v=vs.92).aspx

Camera.AvailableResolutions http://msdn.microsoft.com/en-us/library/microsoft.devices.camera.availableresolutions(v=vs.92).aspx

决议是大小结构的集合。每个Size指定高度和宽度属性。 Pixelcount =高度x宽度

然后通过适当地划分为1024

来划分它以获得Kilo像素或Mega像素