【问题标题】:Is there a system Task that can take a photo in the WinRT API?WinRT API中是否有可以拍照的系统任务?
【发布时间】:2014-09-11 17:55:22
【问题描述】:

我以前在我的 C# Window Store 应用程序 (WinRT) 中使用过照片选择器任务。在 Windows Phone 上,您可以启动一个补充任务,让用户拍照,并返回对图像数据的引用以供您使用。我似乎在 Windows Store 应用 API 中找不到相同的东西。我知道 WinRT 中的捕获媒体 API 和相机捕获 API,但是如果我可以启动一个完整的任务来处理整个拍照操作,而不是自己编写一些东西,那显然会更容易。 Windows Store Apps API 中有这样的工具吗?

【问题讨论】:

    标签: c# windows-runtime windows-store-apps webcam image-capture


    【解决方案1】:

    在 Windows 8.1(不是 Phone)上,Windows.Media.Capture.CameraCaptureUI 类非常易于使用——只需几行代码,并且它具有相机选择、像素密度和裁剪等内置功能:

    using Windows.Media.Capture; 
    using Windows.Storage; 
    
    CameraCaptureUI dialog = new CameraCaptureUI(); 
    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo); 
    

    文件变量将包含捕获的图像。有关详细信息和用法,请参阅Camera Capture UI sample

    请注意,如果您的目标是通用 Windows 8.1 应用,则该 API 在该版本的 Windows Phone 上不可用,您需要编写自己的捕获例程,如 Aurelien 演示的那样。

    【讨论】:

      【解决方案2】:

      自己编写一些代码,因此这项任务并不难。

      我已经回答了一个类似的问题,您可以使用答案:

      Windows (Phone) 8.1 Camera Use

      我在那里重写了我的答案,它包含在设备上查找网络摄像头、初始化您选择的网络摄像头、从中拍照并将其保存在您想要的位置的代码。

      该代码适用于 Windows 手机、桌面或平板电脑应用程序,我唯一要更改的是网络摄像头选择,因为用户可能使用外部网络摄像头,也许电脑没有内置网络摄像头。

      代码如下:

      首先是初始化部分

      // First need to find all webcams
      DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.All)
      

      (在以下几行中,我得到了前后网络摄像头,但对于非手机应用程序 例如,您最好选择网络摄像头列表的索引 0)

      // Then I do a query to find the front webcam
      DeviceInformation frontWebcam = (from webcam in webcamList
       where webcam.EnclosureLocation != null 
       && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
       select webcam).FirstOrDefault();
      
      // Same for the back webcam
      DeviceInformation backWebcam = (from webcam in webcamList
       where webcam.EnclosureLocation != null 
       && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
       select webcam).FirstOrDefault();
      
      // Then you need to initialize your MediaCapture
      newCapture  = new MediaCapture();
      await newCapture.InitializeAsync(new MediaCaptureInitializationSettings
          {
              // Choose the webcam you want
              VideoDeviceId = backWebcam.Id,
              AudioDeviceId = "",
              StreamingCaptureMode = StreamingCaptureMode.Video,
              PhotoCaptureSource = PhotoCaptureSource.VideoPreview
          });
      
      // Set the source of the CaptureElement to your MediaCapture
      // (In my XAML I called the CaptureElement *Capture*)
      Capture.Source = newCapture;
      
      // Start the preview
      await newCapture.StartPreviewAsync();
      

      第二张照片

      //Set the path of the picture you are going to take
      StorageFolder folder = ApplicationData.Current.LocalFolder;
      var picPath = "\\Pictures\\newPic.jpg";
      
      StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName);
      
      ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
      
      //Capture your picture into the given storage file
      await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile);
      

      完成!图片保存在您的应用程序存储文件夹中的给定路径。

      【讨论】:

        猜你喜欢
        • 2016-09-27
        • 2012-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-19
        • 1970-01-01
        相关资源
        最近更新 更多