fix: Fix PreviewView being stretched (#2519)

* fix: Fix Preview stretching

* feat: Keep screen on on Android

* Add test code for race condition

* fix: Fix preview stretching by awaiting SurfaceHolder resizing (`setFixedSize`)  before configuring Camera

* Format

* Update SurfaceHolder+resize.kt

* Update CameraPage.tsx
This commit is contained in:
Marc Rousavy
2024-02-07 11:50:33 +01:00
committed by GitHub
parent b20d0fc5f7
commit 3192f5e939
5 changed files with 53 additions and 14 deletions

View File

@@ -9,7 +9,7 @@ fun getMaximumPreviewSize(): Size {
// See https://developer.android.com/reference/android/hardware/camera2/params/StreamConfigurationMap
// According to the Android Developer documentation, PREVIEW streams can have a resolution
// of up to the phone's display's resolution, with a maximum of 1920x1080.
val display1080p = Size(1080, 1920)
val display1080p = Size(1920, 1080)
val displaySize = Size(
Resources.getSystem().displayMetrics.widthPixels,
Resources.getSystem().displayMetrics.heightPixels

View File

@@ -0,0 +1,32 @@
package com.mrousavy.camera.extensions
import android.view.SurfaceHolder
import androidx.annotation.UiThread
import kotlin.coroutines.resume
import kotlinx.coroutines.suspendCancellableCoroutine
@UiThread
suspend fun SurfaceHolder.resize(width: Int, height: Int) {
return suspendCancellableCoroutine { continuation ->
val currentSize = this.surfaceFrame
if (currentSize.width() == width && currentSize.height() == height) {
// Already in target size
continuation.resume(Unit)
return@suspendCancellableCoroutine
}
val callback = object : SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) = Unit
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
holder.removeCallback(this)
continuation.resume(Unit)
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
holder.removeCallback(this)
continuation.cancel(Error("Tried to resize SurfaceView, but Surface has been destroyed!"))
}
}
this.addCallback(callback)
this.setFixedSize(width, height)
}
}