Skip to content

Commit ae5f315

Browse files
authored
fix: Fix Orientation being wrong when flipping devices (#2958)
1 parent 5f88b21 commit ae5f315

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class CameraSession(internal val context: Context, internal val callback: Callba
133133
if (diff.outputsChanged) {
134134
// 1. outputs changed, re-create them
135135
configureOutputs(config)
136+
// 1.1. whenever the outputs changed, we need to update their orientation as well
137+
configureOrientation()
136138
}
137139
if (diff.deviceChanged || diff.outputsChanged) {
138140
// 2. input or outputs changed, or the session was destroyed from outside, rebind the session
@@ -148,7 +150,7 @@ class CameraSession(internal val context: Context, internal val callback: Callba
148150
}
149151
if (diff.orientationChanged) {
150152
// 5. update the target orientation mode
151-
configureOrientation(config)
153+
orientationManager.setTargetOutputOrientation(config.outputOrientation)
152154
}
153155
if (diff.locationChanged) {
154156
// 6. start or stop location update streaming
@@ -175,27 +177,32 @@ class CameraSession(internal val context: Context, internal val callback: Callba
175177
if (status != PackageManager.PERMISSION_GRANTED) throw MicrophonePermissionError()
176178
}
177179

178-
private fun configureOrientation(config: CameraConfiguration) {
179-
orientationManager.setTargetOutputOrientation(config.outputOrientation)
180-
}
181-
182180
override fun onOutputOrientationChanged(outputOrientation: Orientation) {
183181
Log.i(TAG, "Output orientation changed! $outputOrientation")
184-
photoOutput?.targetRotation = outputOrientation.toSurfaceRotation()
185-
videoOutput?.targetRotation = outputOrientation.toSurfaceRotation()
186-
frameProcessorOutput?.targetRotation = outputOrientation.toSurfaceRotation()
187-
codeScannerOutput?.targetRotation = outputOrientation.toSurfaceRotation()
188-
182+
configureOrientation()
189183
callback.onOutputOrientationChanged(outputOrientation)
190184
}
191185

192186
override fun onPreviewOrientationChanged(previewOrientation: Orientation) {
193187
Log.i(TAG, "Preview orientation changed! $previewOrientation")
194-
previewOutput?.targetRotation = previewOrientation.toSurfaceRotation()
195-
188+
configureOrientation()
196189
callback.onPreviewOrientationChanged(previewOrientation)
197190
}
198191

192+
private fun configureOrientation() {
193+
// Preview Orientation
194+
orientationManager.previewOrientation.toSurfaceRotation().let { previewRotation ->
195+
previewOutput?.targetRotation = previewRotation
196+
}
197+
// Outputs Orientation
198+
orientationManager.outputOrientation.toSurfaceRotation().let { outputRotation ->
199+
photoOutput?.targetRotation = outputRotation
200+
videoOutput?.targetRotation = outputRotation
201+
frameProcessorOutput?.targetRotation = outputRotation
202+
codeScannerOutput?.targetRotation = outputRotation
203+
}
204+
}
205+
199206
interface Callback {
200207
fun onError(error: Throwable)
201208
fun onFrame(frame: Frame)

0 commit comments

Comments
 (0)