【问题标题】:CameraCaptureTask on WP7WP7 上的 CameraCaptureTask
【发布时间】:2011-04-01 09:01:38
【问题描述】:

我想在 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


【解决方案1】:

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

如果您使用WPConnect 工具进行连接,那么它应该可以工作。

【讨论】:

  • 是的,问题就在于此。我使用 Zune 连接到设备。
  • 这太酷了,不知道为什么微软删除了文档。
【解决方案2】:

你可以试试这个……

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

【讨论】:

    【解决方案3】:

    试试这个。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      相关资源
      最近更新 更多