Windows(手机)8.1相机使用 [英] Windows (Phone) 8.1 Camera Use

查看:192
本文介绍了Windows(手机)8.1相机使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Windows Universal应用程序。我想让用户能够上传图片,用户应该可以选择现场拍摄并发送。我有这个工作使用MediaCapture api。但是我只能使用一个相机,所以例如如果我的手机有一个前面和一个后面的相机只使用前面的相机。我将如何切换正在使用的相机?



我在某处阅读过使用这样的东西:

 私有静态异步任务< DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel)
{
DeviceInformation deviceID =(await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation!= null& x.EnclosureLocation.Panel == desired);

return deviceID;
}

但是这总是返回null,因为deviceID总是空。 / p>

或者,是否可以选择将应用程序的控制权转移到拍摄照片并将拍摄的照片返回给我的应用程序?我发现以下,但它不适用于Windows通用应用程序:
http://msdn.microsoft.com/en-us/library/windows/apps/hh394006(v = vs.105).aspx

解决方案

以下是我的操作方式:



首先是初始化部分

  //首先需要找到所有网络摄像机
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.All)

//然后我做查询来找到前面的摄像头
DeviceInformation frontWebcam =(从webcam中的webcam中的
其中webcam.EnclosureLocation!= null
& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();

//后台摄像头相同
DeviceInformation backWebcam =(来自webcamList中的webcam
其中webcam.EnclosureLocation!= null
& webcam.EnclosureLocation。 Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();

//然后你需要初始化你的MediaCapture
newCapture = new MediaCapture();
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
//选择您想要的网络摄像头
VideoDeviceId = backWebcam.Id,
AudioDeviceId =,
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview
});

//将CaptureElement的源设置为MediaCapture
//(在我的XAML中我称为CaptureElement * Capture *)
Capture.Source = newCapture;

//开始预览
await newCapture.StartPreviewAsync();

其次拍照

  //设置要拍摄的图片的路径
StorageFolder folder = ApplicationData.Current.LocalFolder;
var picPath =\\Pictures\\\\
ewPic.jpg;

StorageFile captureFile = await folder.CreateFileAsync(picPath,CreationCollisionOption.GenerateUniqueName);

ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();

//捕获你的图片到给定的存储文件
await newCapture.CapturePhotoToStorageFileAsync(imageProperties,captureFile);

这应该可以解决你的问题。


I am creating a Windows Universal application. I want to the user to be able to upload a picture and the user should have the option of taking one on the spot and sending that. I have this working using the MediaCapture api. However I can only seem to use one camera, so for example if my phone has a front and a back camera only the front camera is used. How would I be able to switch the camera that is in use?

I had read something somewhere about using something like this:

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);

    return deviceID;
}

However this always returns null for me, since the deviceID is always null.

Alternatively is there the option of giving control to an application that takes the picture and returns the taken picture to my application? I have found the following, but it doesn't work for Windows Universal apps: http://msdn.microsoft.com/en-us/library/windows/apps/hh394006(v=vs.105).aspx

解决方案

Here is how I would do it:

First the initialization part

// First need to find all webcams
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.All)

// Then I do a query to find the front webcam
DeviceInformation frontWebcam = (from webcam in webcamList
 where webcam.EnclosureLocation != null 
 && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
 select webcam).FirstOrDefault();

// Same for the back webcam
DeviceInformation backWebcam = (from webcam in webcamList
 where webcam.EnclosureLocation != null 
 && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
 select webcam).FirstOrDefault();

// Then you need to initialize your MediaCapture
newCapture  = new MediaCapture();
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings
        {
            // Choose the webcam you want
            VideoDeviceId = backWebcam.Id,
            AudioDeviceId = "",
            StreamingCaptureMode = StreamingCaptureMode.Video,
            PhotoCaptureSource = PhotoCaptureSource.VideoPreview
        });

// Set the source of the CaptureElement to your MediaCapture
// (In my XAML I called the CaptureElement *Capture*)
Capture.Source = newCapture;

// Start the preview
await newCapture.StartPreviewAsync();

Secondly take the picture

//Set the path of the picture you are going to take
StorageFolder folder = ApplicationData.Current.LocalFolder;
var picPath = "\\Pictures\\newPic.jpg";

StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName);

ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();

//Capture your picture into the given storage file
await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile);

That should solve your problem.

这篇关于Windows(手机)8.1相机使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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