是否存在可以在WinRT API中拍照的系统任务

本文关键字:系统任务 API WinRT 存在 是否 | 更新日期: 2023-09-27 18:21:37

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

是否存在可以在WinRT API中拍照的系统任务

在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示例。

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

自己编码一些东西,这样这个任务就不会那么难了。

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

Windows(Phone)8.1相机使用

我在下面重写了我的答案,它包含了在设备上查找网络摄像头的代码,初始化你选择的摄像头,然后从中拍照并保存到你想要的地方。

该代码适用于windows phone、台式机或平板电脑应用程序。我唯一会更改的是网络摄像头的选择,因为用户可能正在使用外部网络摄像头,也许电脑没有内置网络摄像头。

这是代码:

首先初始化部分

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

(在下面的几行中,我得到了正面和背面的网络摄像头,但对于非手机应用程序你最好选择webcamList的索引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);

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