简体   繁体   中英

How to set maximum resolution with MediaCapture in windows phone 8.1?

I was working on windows phone 8.1(RT) I used the MediaCapture to take a photo(Do keep in mind not interested in previewing the pic to be taken). I used the following code but the quality of the picture is terrible only the part which illuminates light is seen in the picture taken.

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

Thanks for the help.

It looks like you configuring your MediaCapture to give you "preview-quality" photos. Do this instead:

await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
    StreamingCaptureMode = StreamingCaptureMode.Video,
    PhotoCaptureSource = PhotoCaptureSource.Photo, // I believe your bug was here
    AudioDeviceId = string.Empty,
    VideoDeviceId = cameraID.Id
});

Or really, just try the defaults for most of those properties, and only set the VideoDeviceId to choose a camera. It should help in narrowing down the issue.

As for setting a higher photo capture resolution, something like this might work:

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

Do make sure that the aspect ratio of the photo resolution matches the aspect ratio of the preview resolution, or you might get some banding near the edges on some phones.

Also, the phone I tested this on only returned NV12 VideoEncodingProperties for the GetAvailableMediaStreamProperties call on the MediaStreamType.Photo stream. You may need to handle an enumerable that also contains ImageEncodingProperties as well, on a different device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM