Windows Phone App for Windows Phone上的照片捕获

Glo*_*fSf 18 c# windows xaml windows-phone-8.1 win-universal-app

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

Rom*_*asz 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();
Run Code Online (Sandbox Code Playgroud)

如果需要预览,可以使用CaptureElement:

// In XAML: 
<CaptureElement x:Name="PhotoPreview"/>
Run Code Online (Sandbox Code Playgroud)

然后在后面的代码中,您可以像这样开始/停止预览:

// start previewing
PhotoPreview.Source = takePhotoManager;
await takePhotoManager.StartPreviewAsync();
// to stop it
await takePhotoManager.StopPreviewAsync();
Run Code Online (Sandbox Code Playgroud)

最后拍摄照片,您可以将其直接带到文件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);
Run Code Online (Sandbox Code Playgroud)

如果您想捕获视频,那么这里有更多信息.

另外不要忘记添加WebcamCapabilities您的清单文件,并Front/Rear CameraRequirements.


如果您需要选择相机(前/后),您需要获取相机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                    
        });
}
Run Code Online (Sandbox Code Playgroud)

  • @IvanCrojachKaračić有一次,我写了一篇[博客文章](http://www.romasz.net/how-to-take-a-photo-in-windows-runtime/),看一看.旋转默认预览,在`StartPreviewBtn_Click`中你会发现:`captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);` - 这应该可以胜任. (3认同)