[英]Photo capture on Windows Store App for Windows Phone
好吧,我的问题很简单:
如何使用相机使用适用于Windows Phone 8.1
的Windows Store App
捕获图片?
MSDN上的示例使用Windows.Media.Capture.CameraCaptureUI
,它在Windows Phone上不可用,或者用于Silverlight
。
我无法使用Windows运行时专门为Windows Phone应用找到任何文档或示例。
如果有人知道,或者甚至有这方面的文件,我会很高兴。
在WP8.1 Runtime(也在Silverlight中)中,您可以使用MediaCapture 。 简而言之:
// First you will need to initialize MediaCapture
Windows.Media.Capture.MediaCapture takePhotoManager = new Windows.Media.Capture.MediaCapture();
await takePhotoManager.InitializeAsync();
如果需要预览,可以使用CaptureElement :
// In XAML:
<CaptureElement x:Name="PhotoPreview"/>
然后在后面的代码中,您可以像这样开始/停止预览:
// start previewing
PhotoPreview.Source = takePhotoManager;
await takePhotoManager.StartPreviewAsync();
// to stop it
await takePhotoManager.StopPreviewAsync();
最后拍摄照片,您可以将其直接带到文件CapturePhotoToStorageFileAsync或Stream CapturePhotoToStreamAsync :
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
// a file to save a photo
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"Photo.jpg", CreationCollisionOption.ReplaceExisting);
await takePhotoManager.CapturePhotoToStorageFileAsync(imgFormat, file);
如果您想捕获视频,那么这里有更多信息 。
另外,不要忘记在清单文件的Capabilities
和Requirements
Front/Rear Camera
Webcam
中添加Webcam
。
如果您需要选择相机(前/后),您需要获取相机ID,然后使用所需的设置初始化MediaCapture
:
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desired));
}
async private void InitCamera_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
}
在通用Windows Phone 8.1(WinRT)应用程序中,不再可能直接跳入内置相机应用程序并在拍摄照片时接收回调。
为此,您必须如上所述实现Windows.Media.Capture.MediaCapture
。 曾经有CameraCatureUI
但它在Windows Phone 8.1的WinRT应用程序中不可用。
但是有一个“解决方法”。 您可以使用Windows.Storage.Pickers.FileOpenPicker
并将其配置为选择图像。 现在选择器将有一个相机按钮。 用户可以单击相机按钮,内置的相机应用程序将打开。 用户拍完照片后,您会在应用中收到回电信息。 FileOpenPicker
回调实现起来有点烦人,但它确实有效。 如果您可以接受可用性含义,那么这可能是一种有效的方法。
在2014年的微软建设大会期间,有关于此主题的会议。 您可以通过此链接在线观看会话。
您可以在此链接上采用该方法。 一切都很好地解释了。
只需使用PhotoCamera
类,不要忘记在应用清单中启用相机使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.