forked from mrousavy/react-native-vision-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix PreviewView being stretched (mrousavy#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
- Loading branch information
Showing
5 changed files
with
53 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
package/android/src/main/java/com/mrousavy/camera/extensions/SurfaceHolder+resize.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters