camera: stamp raw front-camera stills rotate-then-flip (#932)
- Commit
6eec0a5509ad3dbd6cd09bbccf23a0aba1edde51- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-20 00:06 UTC (16 days ago)
- Parents
2f71d0b
Front-camera portrait photos sometimes saved upside down (horizontal shots were fine). Default Photo mode routes to the Motion Photo path, which saves raw sensor JPEGs and stamps EXIF orientation via stampStillBytes. That function reused the inverted-rotation mapping from writeCaptureExif, which is only correct for bitmaps that were already mirrored (FLIP after ROTATE equals ROTATE(-theta)); raw pixels need the tag itself to carry rotate-then-flip. Mirrored raw stills now map 0/90/180/270 to FLIP_HORIZONTAL (2), TRANSPOSE (5), FLIP_VERTICAL (4), TRANSVERSE (7); unmirrored keeps the plain rotations, and the already-mirrored bitmap path is untouched. Portrait selfies were tagged 90 where 270 was meant (and vice versa) - displayed 180 out. Regression tests cover the full 8-combination mapping and an end-to-end stamp-and-read-back of EXIF orientation. Verified: :camera:testDevUnitTest 517/517 and compile. Upright rendering on a real front camera remains device-only.
Diff
camera/src/main/java/com/vayunmathur/camera/platform/CameraCaptureProcessing.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/platform/CameraCaptureProcessing.kt b/camera/src/main/java/com/vayunmathur/camera/platform/CameraCaptureProcessing.ktindex bc151e843d..d3930adc17 100644--- a/camera/src/main/java/com/vayunmathur/camera/platform/CameraCaptureProcessing.kt+++ b/camera/src/main/java/com/vayunmathur/camera/platform/CameraCaptureProcessing.kt@@ -175,6 +175,30 @@ private fun orientationString(rotationDegrees: Int, mirrored: Boolean): String { }.toString() } +/**+ * EXIF orientation for a still whose pixels are still in sensor orientation (the raw+ * JPEG from an in-memory capture), where [mirrored] means the saved image must be+ * flipped horizontally *after* being rotated upright, to match the front-camera+ * preview. Unlike [orientationString] — whose pixels were already mirrored as a+ * bitmap, leaving only ROTATE(−θ) to tag — nothing has flipped these pixels yet, so+ * the tag itself must carry the flip: rotate-then-flip is one of the four combined+ * EXIF orientations. Tagging a pure inverted rotation instead (the old behaviour)+ * displays portrait selfies (θ=90/270) upside down, while landscape (θ=0/180) still+ * reads the right way up (issue #932).+ */+internal fun rawStillOrientationString(rotationDegrees: Int, mirrored: Boolean): String {+ if (!mirrored) return orientationString(rotationDegrees, mirrored = false)+ val normalized =+ ((rotationDegrees % EXIF_FULL_CIRCLE_DEGREES) + EXIF_FULL_CIRCLE_DEGREES) %+ EXIF_FULL_CIRCLE_DEGREES+ return when (normalized) {+ EXIF_DEGREES_90 -> ExifInterface.ORIENTATION_TRANSPOSE+ EXIF_DEGREES_180 -> ExifInterface.ORIENTATION_FLIP_VERTICAL+ EXIF_DEGREES_270 -> ExifInterface.ORIENTATION_TRANSVERSE+ else -> ExifInterface.ORIENTATION_FLIP_HORIZONTAL+ }.toString()+}+ /** * Stamps orientation + GPS into an in-memory still's JPEG [jpeg] and returns the updated * bytes. In-memory captures carry no ImageCapture.Metadata, so unlike the OutputFileOptions@@ -196,16 +220,15 @@ internal fun CameraViewModel.stampStillBytes( val loc = if (locationEnabledMutable.value) lastLocation else null // In-memory captures carry no ImageCapture.Metadata, so orientation must ALWAYS be // stamped — not just when GPS is on — or motion shots save sideways with location off.- // Mirrored front shots need the inverted rotation (FLIP∘ROTATE(θ)∘FLIP = ROTATE(−θ)),- // matching writeCaptureExif().- val effectiveDegrees =- if (mirrored) (EXIF_FULL_CIRCLE_DEGREES - rotationDegrees) % EXIF_FULL_CIRCLE_DEGREES- else rotationDegrees+ // These pixels are the raw sensor JPEG (never mirrored as a bitmap), so a mirrored+ // front shot needs a flip-carrying orientation, NOT the inverted rotation that+ // writeCaptureExif() uses for already-mirrored pixels (see rawStillOrientationString).+ val orientation = rawStillOrientationString(rotationDegrees, mirrored) var tmp: java.io.File? = null return try { tmp = java.io.File.createTempFile("stamp_", ".jpg", app.cacheDir) tmp.writeBytes(jpeg)- stampTempFile(tmp, effectiveDegrees, loc)+ stampTempFile(tmp, orientation, loc) tmp.readBytes() } catch (e: java.io.IOException) { Log.status("CameraViewModel", "Failed to stamp EXIF on still bytes", e)@@ -218,21 +241,10 @@ internal fun CameraViewModel.stampStillBytes( } } -/** Stamps orientation (+GPS when [loc] is non-null) into the temp JPEG file. */-private fun stampTempFile(tmp: java.io.File, effectiveDegrees: Int, loc: android.location.Location?) {+/** Stamps [orientation] (+GPS when [loc] is non-null) into the temp JPEG file. */+private fun stampTempFile(tmp: java.io.File, orientation: String, loc: android.location.Location?) { val exif = ExifInterface(tmp.absolutePath)- exif.setAttribute(- ExifInterface.TAG_ORIENTATION,- when (- (effectiveDegrees % EXIF_FULL_CIRCLE_DEGREES + EXIF_FULL_CIRCLE_DEGREES) %- EXIF_FULL_CIRCLE_DEGREES- ) {- EXIF_DEGREES_90 -> ExifInterface.ORIENTATION_ROTATE_90- EXIF_DEGREES_180 -> ExifInterface.ORIENTATION_ROTATE_180- EXIF_DEGREES_270 -> ExifInterface.ORIENTATION_ROTATE_270- else -> ExifInterface.ORIENTATION_NORMAL- }.toString()- )+ exif.setAttribute(ExifInterface.TAG_ORIENTATION, orientation) // GPS only when the user opted in; orientation is always stamped above. Skipping // setGpsInfo with no location keeps the bytes bit-for-bit (Ultra HDR gain map intact). if (loc != null) exif.setGpsInfo(loc)
camera/src/test/java/com/vayunmathur/camera/platform/CaptureProcessingMathTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/platform/CaptureProcessingMathTest.kt b/camera/src/test/java/com/vayunmathur/camera/platform/CaptureProcessingMathTest.ktindex e49acc1ce6..9b4e9a49c7 100644--- a/camera/src/test/java/com/vayunmathur/camera/platform/CaptureProcessingMathTest.kt+++ b/camera/src/test/java/com/vayunmathur/camera/platform/CaptureProcessingMathTest.kt@@ -92,4 +92,35 @@ class CaptureProcessingMathTest { assertEquals(androidx.exifinterface.media.ExifInterface.ORIENTATION_ROTATE_180.toString(), o(180, true)) assertNotNull(o(45, false)) // non-right-angle falls back to a defined orientation }++ @Test fun rawStillOrientationMapping() {+ // Raw sensor pixels, no mirror: plain rotations (unchanged behaviour).+ assertEquals("1", rawStillOrientationString(0, false))+ assertEquals("6", rawStillOrientationString(90, false))+ assertEquals("3", rawStillOrientationString(180, false))+ assertEquals("8", rawStillOrientationString(270, false))+ // Mirrored front shot: the tag must carry rotate-then-flip, not ROTATE(-θ) —+ // the pure inverted rotation displayed portrait selfies upside down (#932).+ assertEquals("2", rawStillOrientationString(0, true)) // FLIP_HORIZONTAL+ assertEquals("5", rawStillOrientationString(90, true)) // TRANSPOSE+ assertEquals("4", rawStillOrientationString(180, true)) // FLIP_VERTICAL+ assertEquals("7", rawStillOrientationString(270, true)) // TRANSVERSE+ }++ @Test fun stampStillBytesWritesRawOrientation() {+ val viewModel = vm()+ val src = bitmap(20, 10, Color.rgb(200, 30, 30))+ val baos = java.io.ByteArrayOutputStream()+ src.compress(Bitmap.CompressFormat.JPEG, 90, baos)+ fun stampedOrientation(deg: Int, mirrored: Boolean): String? {+ val stamped = with(viewModel) { stampStillBytes(baos.toByteArray(), deg, mirrored) }+ return androidx.exifinterface.media.ExifInterface(java.io.ByteArrayInputStream(stamped))+ .getAttribute(androidx.exifinterface.media.ExifInterface.TAG_ORIENTATION)+ }+ // Mirrored portrait selfies must not come out upside down (#932).+ assertEquals("5", stampedOrientation(90, true))+ assertEquals("7", stampedOrientation(270, true))+ assertEquals("2", stampedOrientation(0, true))+ assertEquals("6", stampedOrientation(90, false))+ } }