Windows Phone 的 Windows Store 应用中如何进行照片拍摄

18

我的问题很简单:
如何使用摄像头在Windows Phone 8.1Windows Store应用程序中拍照?
MSDN上的示例使用Windows.Media.Capture.CameraCaptureUI,但这在Windows Phone上无法使用,或者是为Silverlight准备的。
我找不到任何关于使用Windows Runtime的Windows Phone应用程序的文档或示例。
如果有人知道,甚至有这方面的文档,我会很高兴。


2
你想要一个Windows Store应用程序还是Windows Phone应用程序? - Ivan Crojach Karačić
Windows Phone应用程序。但是使用Windows Runtime。(使用Windows Runtime的Windows Phone应用程序)。 - GlorfSf
3个回答

48
在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或者保存到流中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中添加前/后置摄像头
如果需要选择相机(前/后置),则需获取相机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                    
        });
}

@user3627550 我已经添加了选择相机设备(前置/后置)的功能。 - Romasz
我已经按照你的要求实现了所有功能,但是当我想拍照时,预览画面会翻转180度,但在横屏模式下拍摄时看起来正常。有什么办法可以解决这个问题吗? - Ivan Crojach Karačić
3
@IvanCrojachKaračić 曾经写过一篇博客文章,你可以看一下。预览默认是旋转的,在 StartPreviewBtn_Click 中,你会发现:captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees); - 这样应该就可以了。 - Romasz
同时添加 captureManager.SetPreviewMirroring(false); - user3290180
@user3290180 这取决于用户的需求。 - Romasz
显示剩余5条评论

3
在通用的Windows Phone 8.1(WinRT)应用程序中,不再可以直接跳转到内置相机应用并在拍照后接收回调。要实现这一点,您必须像上面所述一样实现Windows.Media.Capture.MediaCapture。以前有CameraCatureUI,但在Windows Phone 8.1的WinRT应用程序中不可用。
然而,有一个“解决方法”。您可以使用Windows.Storage.Pickers.FileOpenPicker,并将其配置为选择图像。现在,选择器将具有相机按钮。用户可以单击相机按钮,内置相机应用将打开。一旦用户拍摄了照片,您将在应用中收到回调。FileOpenPicker回调有点麻烦,但它可以工作。如果您可以接受可用性影响,这可能是一种有效的方法。
Microsoft在2014年的build-Conference上就此主题进行了演讲。您可以使用此链接在线观看该会话。

这只是针对手机的情况,那么手机商店应用程序呢? - Yawar

0

你可以在这个链接上采用这种方法。所有东西都解释得非常好。

只需使用PhotoCamera类,不要忘记在应用程序清单中启用相机使用。


好的,谢谢你,但问题是我需要完成一个Windows Store应用程序。Windows 8.1应用程序使用Windows Runtime,而这个API与Silverlight应用程序非常不同。当然,完整的API并没有为Windows Phone实现:一些对于Windows 8.1非常有用和简单的类在Windows Phone上无法使用,我找不到其他的解决方案。 - GlorfSf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接