WP7上的CameraCaptureTask

时间:2011-04-01 09:01:38

标签: windows-phone-7

我想在WP7上使用CameraCaptureTask,以便从手机获取图像并对其进行操作。 我的代码是:

    CameraCaptureTask cameraCaptureTask;
    public MainPage()
    {
        InitializeComponent();

        try
        {
            cameraCaptureTask = new CameraCaptureTask();
            cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);

        }
        catch (System.InvalidOperationException ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {

        try
        {
            cameraCaptureTask.Show();

        }
        catch (System.InvalidOperationException ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        MessageBox.Show("event: " + e.TaskResult.ToString());
        if (e.TaskResult == TaskResult.OK)
        {                
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            image1.Source = bmp;
        }
    }

}

问题在于,每次单击button1时,都会引发事件,但值为TaskResult.Cancel,即OK。此外,在手机中没有显示相机。

有什么想法吗?感谢

3 个答案:

答案 0 :(得分:14)

您是否正在运行调试器?如果是这样,当您使用Zune软件连接到设备时,相机将无法工作。

如果使用WPConnect工具进行连接,则应该可以正常工作。

答案 1 :(得分:1)

你可以试试这个......

private void button1_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            cameraCaptureTask = new CameraCaptureTask();
            cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
            cameraCaptureTask.Show();
        }
        catch (System.InvalidOperationException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
   void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        MessageBox.Show("event: " + e.TaskResult.ToString());
        if (e.TaskResult == TaskResult.OK)
        {                
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            image1.Source = bmp;
        }
    }

答案 2 :(得分:0)

试试这个。

void ctask_Completed(object sender, PhotoResult e)
{

    if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
    {

        //Take JPEG stream and decode into a WriteableBitmap object
        App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);


        //Collapse visibility on the progress bar once writeable bitmap is visible.
        progressBar1.Visibility = Visibility.Collapsed;


        //Populate image control with WriteableBitmap object.
        ImageMain.Source = App.CapturedImage;
    }

}