C# 如何在windows phone 8.1中使用MediaCapture设置最大分辨率?

C# 如何在windows phone 8.1中使用MediaCapture设置最大分辨率?,c#,windows-runtime,windows-phone-8.1,C#,Windows Runtime,Windows Phone 8.1,我当时正在使用WindowsPhone8.1(RT),我使用MediaCapture拍摄了一张照片(请记住,我对预览要拍摄的照片不感兴趣)。 我使用了下面的代码,但是照片的质量很差,只有在拍摄的照片中可以看到照亮光线的部分 private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera) { // get available dev

我当时正在使用WindowsPhone8.1(RT),我使用MediaCapture拍摄了一张照片(请记住,我对预览要拍摄的照片不感兴趣)。 我使用了下面的代码,但是照片的质量很差,只有在拍摄的照片中可以看到照亮光线的部分

private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
    // get available devices for capturing pictures
    DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
        .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);

    if (deviceID != null) return deviceID;
    else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
}

public async Task takeAPicture()
{
    var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
    captureManager = new MediaCapture();

    await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
    {
        StreamingCaptureMode = StreamingCaptureMode.Video,
        PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
        AudioDeviceId = string.Empty,
        VideoDeviceId = cameraID.Id
    });

    var maxResolution = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);
    await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxResolution);

    StorageFile photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("myFirstPhoto.jpg", CreationCollisionOption.GenerateUniqueName);
    //take a photo with choosen Encoding

    await captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoFile);
    captureManager.Dispose();
}
专用静态异步任务GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
//获取用于捕获图片的可用设备
DeviceInformation deviceID=(等待DeviceInformation.FindalAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x=>x.EnclosureLocation!=null&&x.EnclosureLocation.Panel==desiredCamera);
如果(deviceID!=null)返回deviceID;
else抛出新异常(string.Format(“类型为{0}的摄影机不存在。”,desiredCamera));
}
公共异步任务takeapiture()
{
var cameraID=wait GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager=新媒体捕获();
等待captureManager.InitializeAsync(新的MediaCaptureInitializationSettings
{
StreamingCaptureMode=StreamingCaptureMode.Video,
PhotoCaptureSource=PhotoCaptureSource.VideoPreview,
AudioDeviceId=string.Empty,
VideoDeviceId=cameraID.Id
});
var maxResolution=captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).聚合((i1,i2)=>(i1作为视频编码属性).宽度>(i2作为视频编码属性).宽度?i1:i2);
等待captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo,maxResolution);
StorageFile photoFile=wait KnownFolders.PicturesLibrary.CreateFileAsync(“myFirstPhoto.jpg”,CreationCollisionOption.GenerateUniqueName);
//使用choosen编码拍摄照片
等待captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(),photoFile);
captureManager.Dispose();
}

感谢您的帮助。

看起来您正在配置MediaCapture,以提供“预览质量”的照片。改为这样做:

await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
    StreamingCaptureMode = StreamingCaptureMode.Video,
    PhotoCaptureSource = PhotoCaptureSource.Photo, // I believe your bug was here
    AudioDeviceId = string.Empty,
    VideoDeviceId = cameraID.Id
});
或者,实际上,只需尝试这些属性中的大多数属性的默认值,并仅设置VideoDeviceId以选择相机。这应该有助于缩小问题的范围

至于设置更高的照片捕获分辨率,类似这样的设置可能会起作用:

private async Task SetPhotoResolution()
{
    var resolutions = _captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Select(x => x as VideoEncodingProperties);

    var maxRes = resolutions.OrderByDescending(x => x.Height * x.Width).FirstOrDefault();

    await _captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxRes);
}
一定要确保照片分辨率的纵横比与预览分辨率的纵横比相匹配,否则在某些手机的边缘附近可能会出现一些条纹


另外,我测试过的手机只返回了
MediaStreamType.Photo
流上的
GetAvailableMediaStreamProperties
通话的NV12
VideoEncodingProperties
。您可能需要在不同的设备上处理也包含ImageEncodingProperties的可枚举文件。

我在手机上玩过一次,结果发现最大宽度并不意味着最大分辨率。为什么不枚举可用的宽度/高度,并选择将产生最大乘法结果的组合(宽度*高度)?尝试了该代码,但无法获取任何PhotoEncoding属性的参考或任何类似的内容。尝试了视频编码属性并进行了您建议的更改,但该照片只有一个带白色条带的黑色图像。看不到任何内容抱歉,在您检查答案之前,似乎没有及时获取我的编辑。它很快就会出现。你用什么手机运行这个?您是否有机会将示例图片上传到imgur.com?您是否在设备上尝试过不同的摄像头(正面和背面)?我使用的是lumia 730,当我添加PhotoCaptureSource=PhotoCaptureSource时,两个摄像头(正面和背面)都尝试过。照片我得到了一个带白带的黑屏,否则我使用视频预览时会得到一张暗分辨率的照片。我已经上传了代码。takePicBtn_Click方法中的注释行是您的建议答案。我很乐意提供帮助,但我无法访问您发布的代码。很抱歉,这是错误的。