Support landscape orientation

Replace hardcoded portrait-only texture coordinate rotation with
SurfaceTexture.getTransformMatrix(), so the camera preview and capture
re-orient correctly when the device rotates. Also drive
Preview/ImageCapture targetRotation from the live display rotation, fix
the crop-to-fill aspect math to swap effective camera dimensions
between portrait and landscape, and make the slider control panel
scroll if it doesn't fit the shorter landscape height.

Bump to 1.1.6.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-05-07 16:31:43 +02:00
commit d321f07973
7 changed files with 127 additions and 55 deletions

View file

@ -2,8 +2,10 @@ package no.naiv.tiltshift.camera
import android.content.Context
import android.graphics.SurfaceTexture
import android.hardware.display.DisplayManager
import android.util.Log
import android.util.Size
import android.view.Display
import android.view.Surface
import androidx.camera.core.Camera
import androidx.camera.core.CameraSelector
@ -70,6 +72,14 @@ class CameraManager(private val context: Context) {
/** Weak reference to avoid preventing Activity GC across config changes. */
private var lifecycleOwnerRef: WeakReference<LifecycleOwner>? = null
/**
* Target rotation passed to CameraX use cases. Drives the SurfaceTexture
* transform matrix and the rotation metadata on captured images.
* Initialized to the display rotation when the camera binds; updated by
* [setTargetRotation] when the device orientation changes.
*/
private var targetRotation: Int = Surface.ROTATION_0
/**
* Starts the camera with the given lifecycle owner.
* The surfaceTextureProvider should return the SurfaceTexture from the GL renderer.
@ -80,6 +90,13 @@ class CameraManager(private val context: Context) {
) {
this.surfaceTextureProvider = surfaceTextureProvider
this.lifecycleOwnerRef = WeakReference(lifecycleOwner)
// Capture initial display rotation so the very first frame is oriented correctly,
// before the OrientationEventListener has had a chance to fire.
// Note: Context.getDisplay() throws on Application contexts; DisplayManager works
// for any context type and returns the default display.
targetRotation = (context.getSystemService(Context.DISPLAY_SERVICE) as? DisplayManager)
?.getDisplay(Display.DEFAULT_DISPLAY)?.rotation
?: Surface.ROTATION_0
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
cameraProviderFuture.addListener({
@ -110,11 +127,13 @@ class CameraManager(private val context: Context) {
preview = Preview.Builder()
.setResolutionSelector(resolutionSelector)
.setTargetRotation(targetRotation)
.build()
// Image capture use case
val captureBuilder = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.setTargetRotation(targetRotation)
imageCapture = captureBuilder.build()
@ -173,6 +192,19 @@ class CameraManager(private val context: Context) {
}
}
/**
* Updates the target rotation for Preview and ImageCapture use cases.
* Call when the device rotates: this rotates the SurfaceTexture transform matrix
* (so the GL preview stays upright) and tags captures with the right orientation.
* Safe to call on the main thread; CameraX permits live target-rotation updates.
*/
fun setTargetRotation(rotation: Int) {
if (targetRotation == rotation) return
targetRotation = rotation
preview?.targetRotation = rotation
imageCapture?.targetRotation = rotation
}
/**
* Sets the zoom ratio. Updates UI state immediately so that rapid pinch-to-zoom
* gestures accumulate correctly (each frame uses the latest ratio as its base).