簡體   English   中英

如何從我的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?

我希望此時正在構建的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<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;
    }
}

檢查一下 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<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"/>

你可以在這里閱讀,

在Windows Phone應用程序中使用相機

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM