Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#150 - Force reinit of camera2 view on surface change #183

Merged
merged 1 commit into from
May 15, 2024

Conversation

DaSpood
Copy link
Collaborator

@DaSpood DaSpood commented May 14, 2024

The problem was that sometimes onMeasure() was being called after the preview was initialized, so the startPreview() call in the surfaceChanged() 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" in activity_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.

The problem was that sometimes `onMeasure()` was being called after the
preview was initialized, so the `startPreview()` call in the
`onSurfaceChanged()` method would not do anything.

The only way to force a reset of the preview is to pause and resume it,
so the capture session is destroyed and can be properly recreated with
the new resolutions.

This added the issue of concurrent calls to `startPreview()`, some which
were actually needed to update the resolution, while others were 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.
@DaSpood DaSpood self-assigned this May 14, 2024
@DaSpood DaSpood requested a review from VincentKobz May 14, 2024 14:24
@DaSpood DaSpood merged commit acf90a9 into master May 15, 2024
2 checks passed
@DaSpood DaSpood deleted the 150-currentpreviewresolution-fixes branch May 15, 2024 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants