WP7中的CameraCaptureTask

6

我想在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。此外,在手机上相机没有显示。有什么想法吗?谢谢。

可能是Windows Phone 7 - CameraTask Not Working的重复问题。 - i_am_jorf
3个回答

14

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

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


是的,问题就在那里。我是使用 Zune 连接到设备的。 - user422688
这太酷了,不知道为什么微软删除了文档。 - Teoman shipahi

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;
        }
    }

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;
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接