我的问题很简单:
如何使用摄像头在Windows Phone 8.1
的Windows Store应用程序
中拍照?
MSDN上的示例使用Windows.Media.Capture.CameraCaptureUI
,但这在Windows Phone上无法使用,或者是为Silverlight
准备的。
我找不到任何关于使用Windows Runtime的Windows Phone应用程序的文档或示例。
如果有人知道,甚至有这方面的文档,我会很高兴。
我的问题很简单:
如何使用摄像头在Windows Phone 8.1
的Windows Store应用程序
中拍照?
MSDN上的示例使用Windows.Media.Capture.CameraCaptureUI
,但这在Windows Phone上无法使用,或者是为Silverlight
准备的。
我找不到任何关于使用Windows Runtime的Windows Phone应用程序的文档或示例。
如果有人知道,甚至有这方面的文档,我会很高兴。
// First you will need to initialize MediaCapture
Windows.Media.Capture.MediaCapture takePhotoManager = new Windows.Media.Capture.MediaCapture();
await takePhotoManager.InitializeAsync();
// In XAML:
<CaptureElement x:Name="PhotoPreview"/>
// start previewing
PhotoPreview.Source = takePhotoManager;
await takePhotoManager.StartPreviewAsync();
// to stop it
await takePhotoManager.StopPreviewAsync();
最后,你可以直接将照片保存到文件中CapturePhotoToStorageFileAsync或者保存到流中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
中添加Webcam
,以及在Requirements
中添加前/后置摄像头
。
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
});
}
StartPreviewBtn_Click
中,你会发现:captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
- 这样应该就可以了。 - Romasz你可以在这个链接上采用这种方法。所有东西都解释得非常好。
只需使用PhotoCamera
类,不要忘记在应用程序清单中启用相机使用。