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

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

你能帮我看一下相机,把照片保存到我创建的文件夹中吗? 我正在使用XAML和C#。

非常感谢!!!

如果要在应用程序中的按钮上处理捕获,我建议使用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(camera_CaptureCompleted); myCamera.CaptureImageAvailable += new EventHandler(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(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; } } 

检查这个 PhotoCamera类

这适用于CameraCaptureTask

这里有一个简单的代码演示,显示您将相机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(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文件中添加此行。

  

你可以在这里阅读,

在Windows Phone应用程序中使用相机