From d7d31d63db5f0029a4a5e24d998601baee8bade0 Mon Sep 17 00:00:00 2001
From: Anatoly Stolbov <a.stolbov@netris.ru>
Date: Wed, 27 Jul 2016 11:01:51 +0300
Subject: [PATCH] Android camera: use closest viewfinder resolution

For some cameras difference between preview aspect rate and capture
aspect rate is more than 0.01. Therefore it is better to use preview size
with closest aspect rate.

Task-number: QTBUG-50813
Change-Id: I1284c8ec2be1aa160a656e396a52960fa06aaa56
Reviewed-by: Yoann Lopes <yoann.lopes@qt.io>
---
 .../src/mediacapture/qandroidcamerasession.cpp    | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp
index 7b065c8c4..df9f0367b 100644
--- a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp
+++ b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp
@@ -261,17 +261,28 @@ void QAndroidCameraSession::adjustViewfinderSize(const QSize &captureSize, bool
         // search for viewfinder resolution with the same aspect ratio
         const qreal aspectRatio = qreal(captureSize.width()) / qreal(captureSize.height());
         QList<QSize> previewSizes = m_camera->getSupportedPreviewSizes();
+        qreal minAspectDiff = 1;
+        QSize closestResolution;
         for (int i = previewSizes.count() - 1; i >= 0; --i) {
             const QSize &size = previewSizes.at(i);
-            if (qAbs(aspectRatio - (qreal(size.width()) / size.height())) < 0.01) {
+            const qreal sizeAspect = qreal(size.width()) / size.height();
+            if (qFuzzyCompare(aspectRatio, sizeAspect)) {
                 adjustedViewfinderResolution = size;
                 break;
+            } else if (minAspectDiff > qAbs(sizeAspect - aspectRatio)) {
+                closestResolution = size;
+                minAspectDiff = qAbs(sizeAspect - aspectRatio);
             }
         }
 
         if (!adjustedViewfinderResolution.isValid()) {
             qWarning("Cannot find a viewfinder resolution matching the capture aspect ratio.");
-            return;
+            if (closestResolution.isValid()) {
+                adjustedViewfinderResolution = closestResolution;
+                qWarning("Using closest viewfinder resolution.");
+            } else {
+                return;
+            }
         }
     }
 
-- 
GitLab