<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 4.1.0</li>
<li>Operating System / Platform =&gt; Android API Level 28 (Patch: June 1st, 2019)</li>
<li>Compiler =&gt; clang++ 7.0</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">Using <code class="notranslate">JavaCamera2View</code> will output a faulty camera image as shown below if the aspect ratio of the preview size, calculated by <code class="notranslate">calcPreviewSize</code>, does not match up with the actual screen aspect ratio.</p>
<details>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/22560425/60259434-88155200-98d7-11e9-8fef-90d6633544f9.png"><img src="https://user-images.githubusercontent.com/22560425/60259434-88155200-98d7-11e9-8fef-90d6633544f9.png" alt="device-2019-06-27-122401" style="max-width: 100%;"></a></p>
</details>
<p dir="auto">For reference, the device I tested this on is a Xiaomi Mi A1 with the latest patches installed. It has a device screen size of 1920x1080. The calculated preview size, which is also being used in the image above, is 1440x1080. This results in an aspect ratio of 4:3 as opposed to the screen aspect ratio of 16:9. The highest preview size with the same aspect ratio as provided by the device is 1280x720. Here's a small snippet of the device's reported available preview sizes:</p>
<ul dir="auto">
<li>1440x1080 (automatically picked as "best")</li>
<li>1280x960</li>
<li>1280x720 (should be picked as "best")</li>
<li>1280x640</li>
<li>864x480</li>
<li>...</li>
</ul>
<p dir="auto">This behavior is due to <code class="notranslate">calcPreviewSize</code> assuming that the first output size provided by <code class="notranslate">getOutputSizes</code> <a href="https://github.com/opencv/opencv/blob/b95e93c20ab3df34c110385539952e5abcff3715/modules/java/generator/android-21/java/org/opencv/android/JavaCamera2View.java#L264">can be assumed as the "best" preview size from the start</a>. Deleting the two lines before looping over all available preview sizes fixes this behavior. Just for reference, the image below is the same scene as above but with the two lines deleted.</p>
<details>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/22560425/60260502-0541c680-98da-11e9-9d00-1111b6177a7e.png"><img src="https://user-images.githubusercontent.com/22560425/60260502-0541c680-98da-11e9-9d00-1111b6177a7e.png" alt="device-2019-06-27-122256" style="max-width: 100%;"></a></p>
</details>
<p dir="auto">I'm willing to submit a PR since this fix is just deleting two lines of code. However I'd like to see if someone is able to confirm this behavior.</p>
<h5 dir="auto">Steps to reproduce</h5>
<p dir="auto">Haven't found a way to reproduce it yet because it seems to be an issue that heavily depends on the device (and I only have one device to test it on). However overriding the call to <code class="notranslate">calcPreviewSize</code> in <code class="notranslate">connectCamera</code> with dimensions smaller than the device's screen dimensions should evoke this behavior.</p>
<ul dir="auto">
<li>OpenCV version: 2.4</li>
<li>Host OS: Android 6.0</li>
</ul>
<p dir="auto"><a href="https://support.google.com/faqs/answer/7011127" rel="nofollow">https://support.google.com/faqs/answer/7011127</a></p>