简体   繁体   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?

I want the Windows Phone 8 app that I am building at this moment to access the camera for taking a photo when a concrete button on the screen is pressed and then save the image that has been taken into a determined folfer (a folder created into the Windows Phone project by me, not the Windows Phone default image gallery). 我希望此时正在构建的Windows Phone 8应用程序在按下屏幕上的具体按钮时访问相机拍照,然后将已拍摄的图像保存到确定的文件夹中(创建的文件夹)我的Windows Phone项目,而不是Windows Phone默认图库。

Could you help me accesing the camera, taking the picture and saving it into the folder created by me, please? 你能帮我看一下相机,把照片保存到我创建的文件夹中吗? I am using XAML and C#. 我正在使用XAML和C#。

Thank you so much!!! 非常感谢!!!

I would recommend PhotoCamera class if capture is to be processed on a button in the app 如果要在应用程序中的按钮上处理捕获,我建议使用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);

//Events //活动

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

        }

And there is one more alternative called CameraCaptureTask 还有一个名为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;
    }
}

Check this for PhotoCamera class 检查一下 PhotoCamera类

And this for CameraCaptureTask 适用于CameraCaptureTask

There's a simple code demonstration here showing your to put the camera API to use for Windows Phone8 apps. 这里有一个简单的代码演示,显示您将相机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);
}

Dont forget to add this line in WMAppManifent.xml file. 别忘了在WMAppManifent.xml文件中添加此行。

<Capability Name="ID_CAP_ISV_CAMERA"/>

you can read here , 你可以在这里阅读,

Using Cameras in Your Windows Phone Application 在Windows Phone应用程序中使用相机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在图片上绘制文本并将其保存在 Windows 手机 c# 应用程序中? - how to draw text on picture and save it on windows phone c# app? 从现有的相机应用程序Windows Phone 8.1运行时C中获取图片 - get Picture from exising camera app Windows Phone 8.1 runtime c# 从Windows 10手机上安装的应用程序中获取Xaml和C#代码 - Get Xaml and C# code from my app installed on my Windows 10 Phone 如何使用XAML和C#从Windows Phone 8中的文件夹创建图库? - How can I create an image gallery from a folder in Windows Phone 8 using XAML and C#? 来自c#的xaml颜色(windows phone) - color in xaml from c#(windows phone) Windows8 App by C#如何将Canvas保存为图片? - Windows8 App By C# How to save a Canvas as a picture? 在项目文件夹,Windows Phone 8.0,c#,xaml中存储图片 - Storing a Pic in The project folder, windows Phone 8.0, c#, xaml Windows 8 C#将相机的图片保存在本地存储中 - Windows 8 c# save a camera's picture on local storage 如何在“带有XAML的Windows Phone Direct3D”项目中从DirectX(C ++)到XAML(C#)进行通信 - How to communicate from DirectX (C++) to XAML (C#) in a “Windows Phone Direct3D with XAML” project 如何在挂起Windows Phone 8.1 rt xaml C时保存ui状态 - how to save ui state on suspending Windows phone 8.1 rt xaml c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM