【问题标题】:Photo capture on Windows Store App for Windows Phone适用于 Windows Phone 的 Windows Store App 上的照片捕获
【发布时间】:2014-05-12 09:55:25
【问题描述】:

好吧,我的问题很简单:
如何使用相机拍摄带有Windows Store AppWindows Phone 8.1 的图片?
MSDN 上的示例使用Windows.Media.Capture.CameraCaptureUI,在 Windows Phone 上不可用,或者用于Silverlight
我找不到任何专门针对使用 Windows 运行时的 Windows Phone 应用的文档或示例。
如果有人知道,甚至有这方面的文档,我会很高兴。

【问题讨论】:

  • 您想要 Windows 应用商店应用还是 Windows Phone 应用?
  • Windows 应用商店应用。但对于 Windows Phone。 (使用 Windows 运行时的 Windows 手机应用程序)。

标签: c# windows xaml windows-phone-8.1 win-universal-app


【解决方案1】:

在 WP8.1 运行时(也在 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);

如果你想捕捉视频,那么here is more information

别忘了在清单文件的Capabilities 中添加Webcam,在Requirements 中添加Front/Rear Camera


如果您需要选择相机(前/后),您需要获取相机 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 我添加了选择摄像头设备(前/后)。
  • 我已经实现了你所拥有的一切,但是当我想拍照时,预览会翻转 180 度,但当我将手机置于横向模式时看起来还不错。任何想法如何解决它?
  • @IvanCrojachKaračić 曾经写过blog post,看看吧。默认预览是旋转的,在StartPreviewBtn_Click 你会发现:captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees); - 这应该可以完成这项工作。
  • 同时添加 captureManager.SetPreviewMirroring(false);
【解决方案2】:

在通用 Windows Phone 8.1 (WinRT) 应用中,不再可能直接跳转到内置相机应用并在拍照时接收回调。

为此,您必须如上所述实现Windows.Media.Capture.MediaCapture。曾经有 CameraCatureUI,但它在 Windows Phone 8.1 的 WinRT 应用程序中不可用。

但是有一个“解决方法”。您可以使用Windows.Storage.Pickers.FileOpenPicker 并将其配置为选择图像。现在选择器将有一个相机按钮。用户可以单击相机按钮,内置相机应用程序将打开。用户拍照后,您将在您的应用中收到回调。 FileOpenPicker 回调实现起来有点烦人,但它确实有效。如果您可以忍受可用性影响,这可能是一种有效的方法。

在 2014 年微软的 build-Conference 期间有一个关于这个主题的会议。You can watch the session online with this link.

【讨论】:

  • 这只是手机的情况,手机商店应用呢?
【解决方案3】:

您可以在this 链接上采取该方法。一切都解释得很好。

只需使用 PhotoCamera 类,不要忘记在您的应用清单中启用相机使用

【讨论】:

  • 嗯,谢谢,但重点是我需要完成一个 Windows 商店应用程序。 Windows 8.1 应用程序使用 Windows 运行时,此 API 与 silverlight 应用程序非常不同。而且,当然,完整的 API 没有为 windows phone 实现:一些非常有用和简单的类,你可以在 windows 8.1 上使用,但在 windows phone 上不可用,我找不到其他解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多