#150 - Force reinit of camera2 view on surface change #183
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem was that sometimes
onMeasure()
was being called after the preview was initialized, so thestartPreview()
call in thesurfaceChanged()
method would not do anything.The only way to force an update of the preview resolution is to pause and resume it, so the capture session is destroyed and can be properly recreated with the new resolutions.
This adds the issue of concurrent calls to
startPreview()
, some which are actually needed to create the base preview or update the resolution, while others are not. As we cannot create multiple captureSessions, we have to wait for the ongoing init to finish before destroying the session and calling the method again with more up-to-date parameters (via pause-resume).=====
This fix is a bit of a hack with regards to its handling of concurrent initialization so it needs to be properly tested on multiple devices, with
app:previewRatioMode="fitToPicture"
inactivity_main_alt.xml
:Open the camera, pause/unpause it, rotate the device multiple times, make sure there is NO distortion, NO crashes and NO aspect ratio changes when unpausing (or at least none that distorts the preview, I know one sometimes happens in portrait mode but it is negligible and could be due to camera resolution changing dynamically).
May be worth testing with Camera 1 to see if a backport of this fix is needed, though so far there has been no reports of distortion problems with
app:previewRatioMode="fitToPicture"
in Camera 1.