From 80411380fbf614d833cd42dee80d01510326ccd3 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Tue, 19 Mar 2019 14:12:07 +0100 Subject: winrt: Use highest supported resolution for camera preview/image capture Using the lowest supported resolution yields ugly results and has weird side effects (green bars). If no resolution is explicitly given, we should use the maximum supported solution for preview as well as capture. Task-number: QTBUG-72874 Change-Id: Ie0fae65180e66156c6de468f2cabb9122fe665ba Reviewed-by: VaL Doroshchuk --- src/plugins/winrt/qwinrtcameracontrol.cpp | 16 ++++++++++------ src/plugins/winrt/qwinrtimageencodercontrol.cpp | 9 ++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/plugins/winrt/qwinrtcameracontrol.cpp b/src/plugins/winrt/qwinrtcameracontrol.cpp index c00f65624..ede3f6b04 100644 --- a/src/plugins/winrt/qwinrtcameracontrol.cpp +++ b/src/plugins/winrt/qwinrtcameracontrol.cpp @@ -1398,6 +1398,10 @@ HRESULT QWinRTCameraControl::onInitializationCompleted(IAsyncAction *, AsyncStat &captureResolutions); RETURN_HR_IF_FAILED("Failed to find a suitable video format"); + std::sort(captureResolutions.begin(), captureResolutions.end(), [](QSize size1, QSize size2) { + return size1.width() * size1.height() < size2.width() * size2.height(); + }); + // Set capture resolutions. d->imageEncoderControl->setSupportedResolutionsList(captureResolutions.toList()); const QSize captureResolution = d->imageEncoderControl->imageSettings().resolution(); @@ -1412,17 +1416,17 @@ HRESULT QWinRTCameraControl::onInitializationCompleted(IAsyncAction *, AsyncStat Q_ASSERT_SUCCEEDED(hr); // Set preview resolution. - QVector filtered; + QSize maxSize; const float captureAspectRatio = float(captureResolution.width()) / captureResolution.height(); for (const QSize &resolution : qAsConst(previewResolutions)) { const float aspectRatio = float(resolution.width()) / resolution.height(); - if (qAbs(aspectRatio - captureAspectRatio) <= ASPECTRATIO_EPSILON) - filtered.append(resolution); + if ((qAbs(aspectRatio - captureAspectRatio) <= ASPECTRATIO_EPSILON) + && (maxSize.width() * maxSize.height() < resolution.width() * resolution.height())) { + maxSize = resolution; + } } - std::sort(filtered.begin(), filtered.end(), - [](QSize size1, QSize size2) { return size1.width() * size1.height() < size2.width() * size2.height(); }); - const QSize &viewfinderResolution = filtered.first(); + const QSize &viewfinderResolution = maxSize; const quint32 viewfinderResolutionIndex = quint32(previewResolutions.indexOf(viewfinderResolution)); hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Media_MediaProperties_MediaEncodingProfile).Get(), &d->encodingProfile); diff --git a/src/plugins/winrt/qwinrtimageencodercontrol.cpp b/src/plugins/winrt/qwinrtimageencodercontrol.cpp index 7ea851b77..2aed5f8a6 100644 --- a/src/plugins/winrt/qwinrtimageencodercontrol.cpp +++ b/src/plugins/winrt/qwinrtimageencodercontrol.cpp @@ -105,8 +105,15 @@ void QWinRTImageEncoderControl::applySettings() if (d->imageEncoderSetting.codec().isEmpty()) d->imageEncoderSetting.setCodec(QStringLiteral("jpeg")); + if (d->supportedResolutions.isEmpty()) + return; + QSize requestResolution = d->imageEncoderSetting.resolution(); - if (d->supportedResolutions.isEmpty() || d->supportedResolutions.contains(requestResolution)) + if (!requestResolution.isValid()) { + d->imageEncoderSetting.setResolution(d->supportedResolutions.last()); + return; + } + if (d->supportedResolutions.contains(requestResolution)) return; // Find closest resolution from the list -- cgit v1.2.1