照片拍摄在Windows商店应用的Windows Phone [英] Photo capture on Windows Store App for Windows Phone

查看:139
本文介绍了照片拍摄在Windows商店应用的Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的问题很简单:结果
我如何捕获一个图片的Windows Store应用的Windows Phone 8.1 ,使用相机?结果
MSDN上的示例使用 Windows.Media.Capture.CameraCaptureUI ,这是无法使用Windows Phone上,或者是的Silverlight 结果
我找不到专门为使用Windows运行时的Windows Phone应用程序的任何文档或样品。结果
如果有人知道,甚至有这样的医生,我会很高兴。

Well, my question is simple:
How do I capture pictures with a Windows Store App for Windows Phone 8.1, using the camera?
The samples on MSDN use Windows.Media.Capture.CameraCaptureUI, which is not usable on Windows Phone, or are for Silverlight.
I can't find any doc or sample specifically for Windows Phone app using Windows Runtime.
If someone knows, or even have the doc for this, I would be glad.

推荐答案

在WP8.1运行时(也Silverlight中),可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.media.capture.mediacapture.aspx\">MediaCapture.简而言之:

In WP8.1 Runtime (also in Silverlight) you can use MediaCapture. In short:

// First you will need to initialize MediaCapture
Windows.Media.Capture.MediaCapture  takePhotoManager = new Windows.Media.Capture.MediaCapture();
await takePhotoManager.InitializeAsync();

如果你需要一个preVIEW您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.ui.xaml.controls.captureelement\">CaptureElement:

If you need a preview you can use a CaptureElement:

// In XAML: 
<CaptureElement x:Name="PhotoPreview"/>

然后在code后面就可以启动/停止previewing是这样的:

Then in the code behind you can start/stop previewing like this:

// start previewing
PhotoPreview.Source = takePhotoManager;
await takePhotoManager.StartPreviewAsync();
// to stop it
await takePhotoManager.StopPreviewAsync();

最后,拍摄照片就可以,例如把它直接到文件<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700836.aspx\">CapturePhotoToStorageFileAsync或到Stream <一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700840.aspx\">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);

如果您想要捕获视频,然后这里更多的信息

If you want to capture video then here is more information.

另外,不要忘了添加网​​络摄像头功能您的manifest文件和前/后置摄像头要求

Also don't forget to add Webcam in Capabilities of your manifest file, and Front/Rear Camera in Requirements.

在情况下,你需要选择一个摄像头(fornt /后),您将需要获得摄像机ID,然后初始化 MediaCapture 与所需的设置:

In case you need to choose a Camera (fornt/back), you will need to get the Camera Id and then initialize MediaCapture with desired settings:

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商店应用的Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆