【问题标题】:How to query camera resolutions and them to a list如何查询相机分辨率并将它们添加到列表中
【发布时间】:2012-07-25 05:50:19
【问题描述】:

我只是想查询 WP7 设备的相机分辨率,并将这些分辨率添加到 ListPicker,以便用户可以选择他或她想要的视频画笔分辨率。我的 videobrush 显示在我的 MainPage 上,分辨率 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(...)
        }
    }

【问题讨论】:

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


    【解决方案1】:

    我认为您从如何:为 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 结构的集合。每个 Size 指定一个 Height 和 Width 属性。 像素数 = 高 x 宽

    然后用 1024 适当除以得到千像素或百万像素

    【讨论】:

    • 我已经看过这些资源,但我的问题实际上是如何将它们添加到列表选择器并在 MainPage 中相应地更改此值?
    • 创建一个列表,迭代分辨率并存储它。然后将列表绑定到listpicker
    • 能否给我一个如何迭代相机分辨率的示例,这就是我遇到问题的地方。
    • 认真的吗? foreach(Size res in Camera.AvailableResolutions) { ... } 认为你应该认真考虑在继续之前做一些 c# 学习
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2015-10-25
    • 2017-03-26
    相关资源
    最近更新 更多