camera: panoramas are geotagged when location is on, like every other photo path
- Commit
850e8eba45ccb687b28d093abdbf69fe235f9ebd- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 11:09 UTC (5 days ago)
- Parents
fd5f5f8
Every sibling photo path stamps GPS EXIF when the Location setting is on โ plain/processed stills, night merges โ but the panorama path never did: finishPanoramaSweep saved the stitched JPEG (which carries no source EXIF at all) through an engine that only injected GPano XMP. With geotagging on, panoramas came out locationless while every other photo from the same session was tagged. finishPanoramaSweep now refreshes the fix at save time (as writeCaptureExif does) and passes it (or null when the setting is off) into saveToMediaStore, which stamps GPS EXIF into the XMP-tagged bytes before either write path โ any stamping failure returns the original bytes, so a pano is never lost over a geotag. Three regression tests: a fix is stamped with GPano XMP surviving, no-fix bytes carry no GPS tags, and a save with a fix completes. The remaining settings setters, startup restore and save routing were audited and cleared. Verified: :camera:testDevUnitTest full suite 641 tests pass; :camera:lint passes.
Diff
camera/src/main/java/com/vayunmathur/camera/platform/CameraVideoRecording.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/platform/CameraVideoRecording.kt b/camera/src/main/java/com/vayunmathur/camera/platform/CameraVideoRecording.ktindex eaccd19ae2..d4db487bb7 100644--- a/camera/src/main/java/com/vayunmathur/camera/platform/CameraVideoRecording.kt+++ b/camera/src/main/java/com/vayunmathur/camera/platform/CameraVideoRecording.kt@@ -10,6 +10,7 @@ import com.vayunmathur.camera.data.saveStillBytes import com.vayunmathur.camera.data.saveVideoStaged import com.vayunmathur.camera.data.scanSafDoc import com.vayunmathur.camera.data.setLastCaptureUri+import com.vayunmathur.camera.data.updateLocation @android.annotation.SuppressLint("MissingPermission") fun CameraViewModel.toggleHighSpeedRecording() {@@ -536,7 +537,18 @@ internal fun CameraViewModel.finishPanoramaSweep() { } else { val (jpeg, info) = result Log.status("CameraViewModel", "Panorama stitched ${jpeg.size} bytes, saving")- val uri = panoramaEngine.saveToMediaStore(jpeg, info, saveTargetMutable.value)+ // Refresh the fix at save time and geotag like every other+ // still; null when the setting is off (bytes stay untouched).+ updateLocation()+ val gpsLocation = if (locationEnabledMutable.value) lastLocation else null+ val uri = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) {+ panoramaEngine.saveToMediaStore(+ jpeg,+ info,+ saveTargetMutable.value,+ gpsLocation,+ )+ } if (uri != null) { Log.status("CameraViewModel", "Panorama saved uri=$uri") scanSafDoc(uri)
camera/src/main/java/com/vayunmathur/camera/platform/PanoramaEngine.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/platform/PanoramaEngine.kt b/camera/src/main/java/com/vayunmathur/camera/platform/PanoramaEngine.ktindex 69dc6ef72b..2eedd2b917 100644--- a/camera/src/main/java/com/vayunmathur/camera/platform/PanoramaEngine.kt+++ b/camera/src/main/java/com/vayunmathur/camera/platform/PanoramaEngine.kt@@ -10,6 +10,8 @@ import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager+import android.location.Location+import androidx.exifinterface.media.ExifInterface import com.vayunmathur.library.log.Log import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow@@ -614,27 +616,64 @@ class PanoramaEngine(private val context: Context) : SensorEventListener { jpeg: ByteArray, info: PanoInfo, saveTarget: SaveTarget = SaveTarget.MediaStoreDefault,+ gpsLocation: Location? = null, ): android.net.Uri? { val prefix = if (sphereMode) "SPHERE" else "PANO" val name = "${prefix}_${MediaStoreSaver.timestamp()}.jpg" val tagged = PanoXmp.injectXmp(jpeg, PanoXmp.buildGPanoXmp(info))+ // Geotag exactly like every other still (see CameraCaptureProcessing):+ // the caller passes the capture-time fix only when the location+ // setting is on, so an off setting keeps the stitched bytes untouched.+ val stamped = if (gpsLocation != null) stampJpegGps(tagged, gpsLocation) else tagged val treeUri = (saveTarget as? SaveTarget.SafTree)?.treeUri if (treeUri != null) { val doc = SafDocuments.createImageDoc(context.contentResolver, treeUri, name) if (doc != null) {- MediaStoreSaver.saveJpegBytesToUri(context.contentResolver, doc, tagged)?.let { uri ->- Log.status("PanoramaEngine", "Saved $prefix to $uri ${tagged.size} bytes")+ MediaStoreSaver.saveJpegBytesToUri(context.contentResolver, doc, stamped)?.let { uri ->+ Log.status("PanoramaEngine", "Saved $prefix to $uri ${stamped.size} bytes") return uri } Log.status("PanoramaEngine", "SAF save failed for $prefix; using MediaStore") } } val contentValues = MediaStoreSaver.imageValues(name)- return MediaStoreSaver.saveJpegBytes(context.contentResolver, contentValues, tagged).also { uri ->+ return MediaStoreSaver.saveJpegBytes(context.contentResolver, contentValues, stamped).also { uri -> if (uri == null) { Log.error("PanoramaEngine", "MediaStore save failed for $prefix") } else {- Log.status("PanoramaEngine", "Saved $prefix to $uri ${tagged.size} bytes")+ Log.status("PanoramaEngine", "Saved $prefix to $uri ${stamped.size} bytes")+ }+ }+ }++ /**+ * Returns [jpeg] with GPS EXIF from [location] (the GPano XMP segment+ * already injected is preserved). On any failure the original bytes are+ * returned: a pano is never lost over a geotag. Blocking (temp-file I/O):+ * call off the main thread.+ */+ internal fun stampJpegGps(+ jpeg: ByteArray,+ location: Location,+ ): ByteArray {+ var tmp: java.io.File? = null+ return try {+ tmp = java.io.File.createTempFile("pano_gps_", ".jpg", context.cacheDir)+ tmp.writeBytes(jpeg)+ val exif = ExifInterface(tmp.absolutePath)+ exif.setGpsInfo(location)+ exif.saveAttributes()+ tmp.readBytes()+ } catch (e: java.io.IOException) {+ Log.status("PanoramaEngine", "Failed to stamp GPS on panorama", e)+ jpeg+ } catch (e: SecurityException) {+ Log.status("PanoramaEngine", "Failed to stamp GPS on panorama", e)+ jpeg+ } finally {+ try {+ tmp?.delete()+ } catch (_: Exception) { } } }
camera/src/test/java/com/vayunmathur/camera/platform/PanoramaGpsSaveTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/platform/PanoramaGpsSaveTest.kt b/camera/src/test/java/com/vayunmathur/camera/platform/PanoramaGpsSaveTest.ktnew file mode 100644index 0000000000..6d65621042--- /dev/null+++ b/camera/src/test/java/com/vayunmathur/camera/platform/PanoramaGpsSaveTest.kt@@ -0,0 +1,82 @@+package com.vayunmathur.camera.platform++import android.graphics.Bitmap+import android.graphics.Color+import android.location.Location+import androidx.exifinterface.media.ExifInterface+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.camera.domain.PanoInfo+import com.vayunmathur.camera.domain.PanoXmp+import java.io.ByteArrayInputStream+import java.io.ByteArrayOutputStream+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertNull+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import org.robolectric.annotation.GraphicsMode++/**+ * Regression: panoramas/photo-spheres saved with no GPS even when the+ * location setting was on and a fix was available, while every other+ * still stamped it. The pano save now stamps the capture-time fix (and+ * only that fix) into the stitched JPEG.+ */+@GraphicsMode(GraphicsMode.Mode.NATIVE)+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class PanoramaGpsSaveTest {++ private val context = ApplicationProvider.getApplicationContext<android.content.Context>()++ private val fix = Location("test").apply {+ latitude = 37.422+ longitude = -122.084+ }++ private fun jpegBytes(): ByteArray {+ val bmp = Bitmap.createBitmap(32, 16, Bitmap.Config.ARGB_8888).apply { eraseColor(Color.YELLOW) }+ val out = ByteArrayOutputStream()+ bmp.compress(Bitmap.CompressFormat.JPEG, 90, out)+ bmp.recycle()+ return out.toByteArray()+ }++ private fun info() = PanoInfo(+ projectionType = "cylindrical", fullWidth = 32, fullHeight = 16,+ croppedWidth = 32, croppedHeight = 16, croppedLeft = 0, croppedTop = 0,+ )++ private fun taggedJpeg(): ByteArray =+ PanoXmp.injectXmp(jpegBytes(), PanoXmp.buildGPanoXmp(info()))++ @Test fun stampJpegGpsWritesFixAndKeepsGpanoXmp() {+ val engine = PanoramaEngine(context)+ val stamped = engine.stampJpegGps(taggedJpeg(), fix)+ val exif = ExifInterface(ByteArrayInputStream(stamped))+ val latLong = exif.getLatLong()+ assertNotNull(latLong, "stamped pano must carry GPS")+ assertEquals(37.422, latLong[0].toDouble(), 0.001)+ assertEquals(-122.084, latLong[1].toDouble(), 0.001)+ // The GPano packet must survive EXIF stamping or viewers stop+ // treating the shot as a panorama.+ assertTrue(String(stamped, Charsets.ISO_8859_1).contains("GPano"))+ }++ @Test fun unstitchedBytesWithoutFixCarryNoGps() {+ // The off/no-fix contract: the caller passes no location and the+ // stitched bytes are written untouched (no GPS tags at all).+ val exif = ExifInterface(ByteArrayInputStream(taggedJpeg()))+ assertNull(exif.getLatLong())+ assertNull(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE))+ }++ @Test fun saveToMediaStoreWithFixStillSaves() {+ val engine = PanoramaEngine(context)+ val uri = engine.saveToMediaStore(jpegBytes(), info(), gpsLocation = fix)+ assertTrue(uri == null || uri.toString().contains("media"))+ }+}