diff --git a/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt b/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt index 910ad3fa..d34a407f 100644 --- a/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt +++ b/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt @@ -179,6 +179,7 @@ class Camera2Controller(private val context: Context) { // 缓存 CaptureResult 和 Image 用于配对 (timestamp -> Data) private val pendingResults = ConcurrentHashMap() private val pendingImages = ConcurrentHashMap() + private val pendingCaptureStartedTimestamps = ConcurrentHashMap() private val pendingCloseReaders = mutableListOf() private val openImagesCount = AtomicInteger(0) @@ -198,6 +199,12 @@ class Camera2Controller(private val context: Context) { return image?.let { SafeImage(it, this) } } + private fun getCaptureTimestamp(result: TotalCaptureResult): Long? { + val frameNumber = result.frameNumber + val startedTimestamp = pendingCaptureStartedTimestamps.remove(frameNumber) + return result.get(CaptureResult.SENSOR_TIMESTAMP) ?: startedTimestamp + } + // 快门音效播放回调 var onPlayShutterSound: (() -> Unit)? = null @@ -2669,6 +2676,9 @@ class Camera2Controller(private val context: Context) { timestamp: Long, frameNumber: Long ) { + if (state.value.useRaw && isRawSupported) { + pendingCaptureStartedTimestamps[frameNumber] = timestamp + } PLog.d(TAG, "Burst capture started at frame $frameNumber") } @@ -2677,7 +2687,7 @@ class Camera2Controller(private val context: Context) { request: CaptureRequest, result: TotalCaptureResult ) { - val timestamp = result.get(CaptureResult.SENSOR_TIMESTAMP) + val timestamp = getCaptureTimestamp(result) if (timestamp != null && state.value.useRaw && isRawSupported) { val pendingImage = pendingImages.remove(timestamp) if (pendingImage != null) { @@ -2723,6 +2733,9 @@ class Camera2Controller(private val context: Context) { timestamp: Long, frameNumber: Long ) { + if (state.value.useRaw && isRawSupported) { + pendingCaptureStartedTimestamps[frameNumber] = timestamp + } PLog.d(TAG, "Capture started at frame $frameNumber") } @@ -2731,7 +2744,7 @@ class Camera2Controller(private val context: Context) { request: CaptureRequest, result: TotalCaptureResult ) { - val timestamp = result.get(CaptureResult.SENSOR_TIMESTAMP) + val timestamp = getCaptureTimestamp(result) if (timestamp != null && state.value.useRaw && isRawSupported) { val pendingImage = pendingImages.remove(timestamp) if (pendingImage != null) { diff --git a/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt b/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt index faa235bc..15f5f2eb 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt @@ -1126,9 +1126,19 @@ object GalleryManager { val tempDngFile = File(photoDir, "temp.dng") val tempFile = File(photoDir, "temp.jpg") - val metadata = loadMetadata(context, photoId) ?: return@withContext + val metadata = loadMetadata(context, photoId) + if (metadata == null) { + PLog.e(TAG, "saveRawPhoto aborted: metadata unavailable for $photoId") + image.close() + return@withContext + } - captureResult ?: return@withContext + val resolvedCaptureResult = captureResult + if (resolvedCaptureResult == null) { + PLog.e(TAG, "saveRawPhoto aborted: captureResult unavailable for $photoId") + image.close() + return@withContext + } var dngSaveAttempted = false FileOutputStream(tempDngFile).use { outputStream -> @@ -1137,7 +1147,7 @@ object GalleryManager { RawProcessor.saveToDng( image, characteristics, - captureResult, + resolvedCaptureResult, outputStream, rotation, thumbnail diff --git a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt index f911d045..1a765484 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt @@ -2473,6 +2473,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) characteristics: CameraCharacteristics?, captureResult: CaptureResult? ) { + var ownsImage = true try { PLog.d(TAG, "saveImage started - dimensions: ${image.width}x${image.height}, format: ${image.format}") val context = getApplication() @@ -2582,7 +2583,10 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) deferred } else null - characteristics ?: return + val resolvedCharacteristics = characteristics ?: run { + PLog.e(TAG, "Failed to save image: camera characteristics unavailable") + return + } val photoId = GalleryManager.preparePhoto( context, @@ -2597,6 +2601,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) PLog.e(TAG, "Failed to save image") return } + ownsImage = false viewModelScope.launch(Dispatchers.IO) { GalleryManager.saveVideo(context, photoId, livePhotoVideoDeferred) @@ -2607,7 +2612,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) previewThumbnail, rotation, aspectRatio, - characteristics, + resolvedCharacteristics, captureResult, shouldAutoSave, contentRepository.photoProcessor, @@ -2624,6 +2629,10 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) _imageSavedEvent.emit(Unit) } catch (e: Exception) { PLog.e(TAG, "Failed to save image", e) + } finally { + if (ownsImage) { + image.close() + } } }