feat: photo capture progress

This commit is contained in:
Hinnka committed 2026-09-19 09:59:00 +08:00
1 parent d42cfedd24
commit 55448c6083
4 files changed
+107 -21

No files matched your search

@@ -531,6 +531,7 @@ class Camera2Controller(private val context: Context) {
private fun failPhotoCapture(shot: PhotoCaptureContext, reason: String, drainQueue: Boolean = true) {
if (shot.failed || shot.delivered) return
shot.failed = true
clearPhotoCaptureProgress(shot)
shot.timeout?.let { cameraHandler?.removeCallbacks(it) }
shot.timeout = null
shot.timestamps.forEach { timestamp ->
@@ -581,6 +582,7 @@ class Camera2Controller(private val context: Context) {
private fun finishPhotoCaptureSequence(shot: PhotoCaptureContext, successful: Boolean) {
shot.sequenceCompleted = true
clearPhotoCaptureProgress(shot)
if (!successful) failPhotoCapture(shot, "capture sequence aborted")
if (shot.failed) settleFailedPhotoCapture(shot)
if (activePhotoCapture === shot) {
@@ -677,16 +679,45 @@ class Camera2Controller(private val context: Context) {
deliverNextPhotoCaptureIfReady()
}
private fun clearPhotoCaptureProgress(shot: PhotoCaptureContext) {
_state.update {
if (it.photoCaptureProgress?.captureId == shot.id) {
it.copy(photoCaptureProgress = null)
} else {
it
}
}
}
private fun submitPhotoCapture(
shot: PhotoCaptureContext,
session: CameraCaptureSession,
requests: List<CaptureRequest>,
baseExposureResult: CaptureResult? = lastCaptureResult,
) {
check(requests.size == shot.expectedFrames) {
"Capture ${shot.id} reserved ${shot.expectedFrames} frames but built ${requests.size} requests"
}
shot.livePhotoStartTimestampUs = livePhotoVideoStartTimestampUs
val captureSurface = shot.reader.surface
// Sum the actual request plan so HDR frames with different exposures contribute
// their own duration. Keep one timeline for the entire photo, including frame gaps.
val estimatedDurationNs = requests.sumOf { request ->
val manualExposure = request.get(CaptureRequest.CONTROL_AE_MODE) ==
CaptureRequest.CONTROL_AE_MODE_OFF
val exposureNs = if (manualExposure) {
request.get(CaptureRequest.SENSOR_EXPOSURE_TIME) ?: shot.state.shutterSpeed
} else {
baseExposureResult?.get(CaptureResult.SENSOR_EXPOSURE_TIME) ?: shot.state.shutterSpeed
}
val frameDurationNs = if (manualExposure) {
request.get(CaptureRequest.SENSOR_FRAME_DURATION)
} else {
baseExposureResult?.get(CaptureResult.SENSOR_FRAME_DURATION)
}
maxOf(exposureNs, frameDurationNs ?: 0L, 1L)
}
var progressStarted = false
val callback = object : CameraCaptureSession.CaptureCallback() {
override fun onCaptureStarted(
session: CameraCaptureSession,
@@ -702,6 +733,16 @@ class Camera2Controller(private val context: Context) {
shot.timestampByFrameNumber[frameNumber] = timestamp
pendingCaptureStartedTimestamps[frameNumber] = timestamp
registerPhotoTimestamp(shot, timestamp)
if (!progressStarted && activePhotoCapture === shot && !shot.continuousBurst && !shot.failed) {
progressStarted = true
val progress = PhotoCaptureProgress(
captureId = shot.id,
// Sensor timestamps may use a different clock from elapsedRealtime.
startedElapsedNs = SystemClock.elapsedRealtimeNanos(),
estimatedDurationNs = estimatedDurationNs,
)
_state.update { it.copy(photoCaptureProgress = progress) }
}
}
override fun onCaptureCompleted(
@@ -7411,7 +7452,7 @@ class Camera2Controller(private val context: Context) {
if (captureSession == null) return
val shot = reservePhotoCapture(_state.value, reader) ?: return
activePhotoCapture = shot
_state.update { it.copy(isCapturing = true) }
_state.update { it.copy(isCapturing = true, photoCaptureProgress = null) }
try {
onAccepted?.invoke(shot.id)
} catch (e: Exception) {
@@ -7520,7 +7561,7 @@ class Camera2Controller(private val context: Context) {
val shot = activePhotoCapture ?: error("Missing HDR capture reservation")
shot.state = shot.state.copy(hdrBracketCapturing = true, hdrBracketFrameCount = hdrFrameCount)
submitPhotoCapture(shot, session, requests)
submitPhotoCapture(shot, session, requests, baseExposureResult)
} catch (e: Exception) {
PLog.e(TAG, "Failed to capture HDR bracket", e)
_state.value = _state.value.copy(
@@ -8188,10 +8229,10 @@ class Camera2Controller(private val context: Context) {
}
val shot = activePhotoCapture ?: error("Missing multi-frame capture reservation")
submitPhotoCapture(shot, session, requests)
submitPhotoCapture(shot, session, requests, baseExposureResult)
} else {
val shot = activePhotoCapture ?: error("Missing single capture reservation")
submitPhotoCapture(shot, session, listOf(captureBuilder.build()))
submitPhotoCapture(shot, session, listOf(captureBuilder.build()), baseExposureResult)
}
} catch (e: Exception) {
PLog.e(TAG, "Failed to perform capture", e)
@@ -341,6 +341,7 @@ data class CameraState(
// 是否正在拍照
val isCapturing: Boolean = false,
val photoCaptureProgress: PhotoCaptureProgress? = null,
// LUT 设置
val currentLutName: String? = null,
@@ -0,0 +1,14 @@
package com.hinnka.mycamera.camera
/** Sensor capture only; image delivery, merging and saving are not part of this progress. */
data class PhotoCaptureProgress(
val captureId: Long,
val startedElapsedNs: Long,
val estimatedDurationNs: Long,
) {
fun fractionAt(elapsedRealtimeNs: Long): Float {
val elapsedNs = (elapsedRealtimeNs - startedElapsedNs).coerceAtLeast(0L)
return (elapsedNs.toDouble() / estimatedDurationNs.coerceAtLeast(1L))
.coerceIn(0.0, 1.0).toFloat()
}
}
@@ -7,6 +7,7 @@ import android.content.Intent
import android.graphics.Bitmap
import android.graphics.SurfaceTexture
import android.os.Environment
import android.os.SystemClock
import android.provider.Settings
import android.view.WindowManager
import androidx.activity.compose.rememberLauncherForActivityResult
@@ -34,6 +35,7 @@ import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathOperation
@@ -67,6 +69,7 @@ import com.hinnka.mycamera.R
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.CameraState
import com.hinnka.mycamera.camera.FocusPointSource
import com.hinnka.mycamera.camera.PhotoCaptureProgress
import com.hinnka.mycamera.data.CaptureButtonStyle
import com.hinnka.mycamera.data.DevelopAnimationStyle
import com.hinnka.mycamera.model.CameraPreset
@@ -2015,6 +2018,8 @@ private fun Controls(
captureMode = state.captureMode,
isProfessionalMode = state.useRaw && state.isRawSupported,
isCapturing = state.isCapturing,
photoCaptureProgress = state.photoCaptureProgress,
isBurstCapturing = state.burstCapturing,
isVideoRecording = state.videoRecordingState.isRecording,
isVideoProcessing = state.videoRecordingState.isProcessing,
isPaused = state.videoRecordingState.isPaused,
@@ -2205,6 +2210,8 @@ fun CaptureButton(
captureMode: CaptureMode,
isProfessionalMode: Boolean,
isCapturing: Boolean,
photoCaptureProgress: PhotoCaptureProgress?,
isBurstCapturing: Boolean,
isVideoRecording: Boolean,
isVideoProcessing: Boolean,
isPaused: Boolean,
@@ -2247,7 +2254,11 @@ fun CaptureButton(
initialValue = 0f,
targetValue = 360f,
animationSpec = infiniteRepeatable(tween(1500)),
label = "livePhotoRotation"
label = "burstCaptureRotation"
)
val captureProgress = rememberPhotoCaptureProgress(
photoCaptureProgress.takeIf { isCapturing && captureMode == CaptureMode.PHOTO }
)
val currentDisabled by rememberUpdatedState(
@@ -2330,22 +2341,6 @@ fun CaptureButton(
)
)
if (isCapturing) {
Canvas(modifier = Modifier.fillMaxSize()) {
val strokeWidth = 3.dp.toPx()
drawArc(
color = Color(0xFFFFD700),
startAngle = rotation - 90f,
sweepAngle = 90f,
useCenter = false,
style = Stroke(
width = strokeWidth,
cap = StrokeCap.Round
)
)
}
}
// Center shutter surface
val centerPadding by animateDpAsState(
targetValue = if (captureMode == CaptureMode.VIDEO && isVideoRecording) 19.dp else 2.dp,
@@ -2408,6 +2403,25 @@ fun CaptureButton(
)
}
}
if (isCapturing && captureMode == CaptureMode.PHOTO) {
Canvas(modifier = Modifier.fillMaxSize()) {
val strokeWidth = 3.dp.toPx()
drawCircle(
color = Color.White.copy(alpha = 0.14f),
radius = (size.minDimension - strokeWidth) / 2f,
style = Stroke(width = strokeWidth)
)
drawArc(
color = Color(0xFFFFD700),
startAngle = if (isBurstCapturing) rotation - 90f else -90f,
sweepAngle = if (isBurstCapturing) 90f else 360f * captureProgress.value,
useCenter = false,
topLeft = Offset(strokeWidth / 2f, strokeWidth / 2f),
size = Size(size.width - strokeWidth, size.height - strokeWidth),
style = Stroke(width = strokeWidth, cap = StrokeCap.Round)
)
}
}
if (captureMode == CaptureMode.VIDEO && isVideoProcessing) {
CircularProgressIndicator(
modifier = Modifier.size(34.dp),
@@ -2418,6 +2432,22 @@ fun CaptureButton(
}
}
@Composable
private fun rememberPhotoCaptureProgress(progress: PhotoCaptureProgress?): State<Float> {
val fraction = remember(progress) {
mutableFloatStateOf(progress?.fractionAt(SystemClock.elapsedRealtimeNanos()) ?: 0f)
}
LaunchedEffect(progress) {
if (progress == null) return@LaunchedEffect
while (isActive && fraction.floatValue < 1f) {
withFrameNanos {
fraction.floatValue = progress.fractionAt(SystemClock.elapsedRealtimeNanos())
}
}
}
return fraction
}
@Composable
private fun CaptureModeSwitcher(
shootingMode: CameraShootingMode,