如何从我的 Windows Phone 8 应用程序(XAML 和 C#)访问相机并将拍摄的照片保存在确定的文件夹中?

Posted

技术标签:

【中文标题】如何从我的 Windows Phone 8 应用程序(XAML 和 C#)访问相机并将拍摄的照片保存在确定的文件夹中?【英文标题】:How to access the camera from my Windows Phone 8 app (XAML and C#) and save the taken picture in a determined folder? 【发布时间】:2014-03-19 12:24:56 【问题描述】:

我希望我现在正在构建的 Windows Phone 8 应用程序能够在按下屏幕上的具体按钮时访问相机以拍照,然后将已拍摄的图像保存到确定的文件夹(文件夹由我创建到 Windows Phone 项目中,而不是 Windows Phone 默认图片库)。

您能帮我打开相机,拍照并将其保存到我创建的文件夹中吗?我正在使用 XAML 和 C#。

非常感谢!!!

【问题讨论】:

【参考方案1】:

如果要在应用程序中的按钮上处理捕获,我会推荐 PhotoCamera 类

 PhotoCamera myCamera = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
 //viewfinderBrush is a videobrush object declared in xaml
 viewfinderBrush.SetSource(myCamera);
 myCamera.Initialized += myCamera_Initialized;
 myCamera.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(camera_CaptureCompleted);
 myCamera.CaptureImageAvailable += new EventHandler<Microsoft.Devices.ContentReadyEventArgs>(camera_CaptureImageAvailable);

//事件

   void myCamera_Initialized(object sender, CameraOperationCompletedEventArgs e)
    
        try
        
            if (e.Succeeded)
            

            
        
        catch
        
            MessageBox.Show("Problem occured in camera initialization.");
        
    

 void camera_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
        
            try
            

            
            catch
            
                MessageBox.Show("Captured image is not available, please try again.");
            
        

void camera_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
        
            try
            

            
            catch (Exception ex)
            
                MessageBox.Show("Captured image is not available, please try again.   " + ex.Message);
            

        

还有另一种选择,称为 CameraCaptureTask

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

void cameraCaptureTask_Completed(object sender, PhotoResult e)

    if (e.TaskResult == TaskResult.OK)
    
        MessageBox.Show(e.ChosenPhoto.Length.ToString());

        //Code to display the photo on the page in an image control named myImage.
        //System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
        //bmp.SetSource(e.ChosenPhoto);
        //myImage.Source = bmp;
    

检查 this 的 PhotoCamera 类

还有 this 用于 CameraCaptureTask

【讨论】:

【参考方案2】:

这里有一个简单的代码演示,显示您将相机 API 用于 Windows Phone8 应用程序。

 private void MainPage_Loaded(object sender, RoutedEventArgs e)
 

 if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) ||
     (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true))  
     // Initialize the default camera.
     _photoCamera = new Microsoft.Devices.PhotoCamera();

     //Event is fired when the PhotoCamera object has been initialized
     _photoCamera.Initialized += new EventHandler<Microsoft.Devices.CameraOperationCompletedEventArgs>(OnPhotoCameraInitialized);

      //Set the VideoBrush source to the camera
      viewfinderBrush.SetSource(_photoCamera);
  
  else 
      // The camera is not supported on the device.
      this.Dispatcher.BeginInvoke(delegate()  
      // Write message.
          txtDebug.Text = "A Camera is not available on this device.";
      );

  



private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e) 
    int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
    int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);

不要忘记在 WMAppManifent.xml 文件中添加这一行。

<Capability Name="ID_CAP_ISV_CAMERA"/>

你可以在这里阅读,

Using Cameras in Your Windows Phone Application

【讨论】:

以上是关于如何从我的 Windows Phone 8 应用程序(XAML 和 C#)访问相机并将拍摄的照片保存在确定的文件夹中?的主要内容,如果未能解决你的问题,请参考以下文章

带有 AngularJS 的 Windows Phone 8.1 上的 Phonegap 无法从我的 API 中检索 JSONP

我无法将套接字 io 与 windows phone 8.1 C# 连接

如何调试在 Windows Phone 8.1 上挂起的应用程序

如何将 Windows Phone 8.1 应用程序部署到 Windows 10 移动设备?

如何在 Windows Phone 8 中使用 mp3 文件?

如何在 Windows Phone 8 应用程序中检查社交网络身份验证?