c# - Windows(手机)8.1 相机使用

标签 c# camera windows-store-apps windows-phone-8.1 win-universal-app

我正在创建一个 Windows 通用应用程序。我希望用户能够上传图片,并且用户应该可以选择当场拍摄并发送。我使用 MediaCapture api 进行了这项工作。但是我似乎只能使用一个摄像头,例如,如果我的手机有一个前置摄像头和一个后置摄像头,则只使用前置摄像头。我如何才能切换正在使用的相机?

我在某处读到过一些关于使用这样的东西:

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

但是这对我来说总是返回 null,因为 deviceID 总是 null。

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

最佳答案

下面是我的做法:

首先是初始化部分

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

接着拍照

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

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

关于c# - Windows(手机)8.1 相机使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26137683/

相关文章:

C# GetHashCode 具有三个 Int16 值?

c# - 在 log4net 中使用 DI/IoC 或静态方法并获得正确的类类型和方法?

android - 关于 getParameters 失败(空参数)

ios - 使用 OpenCV 在卡片边缘绘制轮廓

c# - 资料库; POCOs/Linq-to-Sql 实体类之间的映射

c# - 如何解决 "Procedure or function has too many arguments specified"异常?

mysql - 保存安全摄像头流

c# - Windows 应用商店应用程序后台任务每分钟运行一次?

c# - 使用用于 UI 和 WriteableBitmapEx 的 Xamlcropcontrol 裁剪图像

c# - 将 IBuffer 转换为 IRandomAccessStream