fix: prevent image leaks and improve capture timestamp matching

This commit is contained in:
Hinnka committed 2026-05-02 08:21:03 +08:00
1 parent d2ac24d43a
commit d994fa4b20
3 files changed
+39 -7

No files matched your search

@@ -179,6 +179,7 @@ class Camera2Controller(private val context: Context) {
// 缓存 CaptureResult 和 Image 用于配对 (timestamp -> Data)
private val pendingResults = ConcurrentHashMap<Long, TotalCaptureResult>()
private val pendingImages = ConcurrentHashMap<Long, SafeImage>()
private val pendingCaptureStartedTimestamps = ConcurrentHashMap<Long, Long>()
private val pendingCloseReaders = mutableListOf<ImageReader>()
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) {
@@ -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
@@ -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<Application>()
@@ -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()
}
}
}