feat: burst capture (2)
This commit is contained in:
1 parent
69ce0f720d
commit
1510e8eadc
10 files changed
+334
-130
No files matched your search
Binary file not shown.
Binary file not shown.
@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import com.hinnka.mycamera.livephoto.LivePhotoRecorder
|
||||
import com.hinnka.mycamera.model.SafeImage
|
||||
import kotlinx.coroutines.delay
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
@@ -521,13 +522,13 @@ class Camera2Controller(private val context: Context) {
|
||||
).apply {
|
||||
setOnImageAvailableListener({ reader ->
|
||||
try {
|
||||
PLog.d(TAG, "ImageReader onImageAvailableListener triggered")
|
||||
// PLog.d(TAG, "ImageReader onImageAvailableListener triggered")
|
||||
if (openImagesCount.get() >= MAX_IMAGES) {
|
||||
PLog.w(TAG, "Too many open images ($openImagesCount), skipping acquire")
|
||||
return@setOnImageAvailableListener
|
||||
}
|
||||
val rawImage = when {
|
||||
state.value.burstCapturing -> reader.acquireLatestImage()
|
||||
state.value.burstCapturing -> reader.acquireNextImage()
|
||||
state.value.useMultiFrame -> reader.acquireNextImage()
|
||||
else -> reader.acquireLatestImage()
|
||||
}
|
||||
@@ -2333,7 +2334,7 @@ class Camera2Controller(private val context: Context) {
|
||||
try {
|
||||
val width = image.width
|
||||
val height = image.height
|
||||
PLog.d(TAG, "Matching Image and CaptureResult found for timestamp ${image.timestamp}")
|
||||
// PLog.d(TAG, "Matching Image and CaptureResult found for timestamp ${image.timestamp}")
|
||||
|
||||
// 构建 CaptureInfo
|
||||
val captureInfo = rebuildCaptureInfo(result, width, height)
|
||||
@@ -2422,13 +2423,36 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
|
||||
val request = captureBuilder.build()
|
||||
session.capture(request, null, cameraHandler)
|
||||
val requests = mutableListOf<CaptureRequest>()
|
||||
for (i in 0 until 8) {
|
||||
requests.add(request)
|
||||
}
|
||||
session.captureBurst(requests, object : CameraCaptureSession.CaptureCallback() {
|
||||
override fun onCaptureSequenceCompleted(
|
||||
session: CameraCaptureSession,
|
||||
sequenceId: Int,
|
||||
frameNumber: Long
|
||||
) {
|
||||
checkBurstCaptureContinue()
|
||||
}
|
||||
}, cameraHandler)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to start hardware burst capture", e)
|
||||
_state.value = _state.value.copy(burstCapturing = false, isCapturing = false)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkBurstCaptureContinue() {
|
||||
if (!state.value.burstCapturing) return
|
||||
if (MAX_IMAGES - openImagesCount.get() < 8) {
|
||||
cameraHandler?.postDelayed({
|
||||
checkBurstCaptureContinue()
|
||||
}, 100)
|
||||
return
|
||||
}
|
||||
startBurstCapture()
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止连拍
|
||||
*/
|
||||
|
||||
@@ -102,6 +102,7 @@ object PhotoManager {
|
||||
noiseReductionValue: Float,
|
||||
chromaNoiseReductionValue: Float,
|
||||
photoQuality: Int = 95,
|
||||
suffix: String? = null,
|
||||
onComplete: (Boolean) -> Unit = {}
|
||||
) {
|
||||
withContext(Dispatchers.IO) {
|
||||
@@ -118,8 +119,10 @@ object PhotoManager {
|
||||
) ?: return@withContext
|
||||
|
||||
// 保存到指定目录
|
||||
val date = metadata.dateTaken ?: System.currentTimeMillis()
|
||||
val withSuffix = suffix?.let { "_$it" } ?: "_${metadata.exportedUris.size + 1}"
|
||||
val filename =
|
||||
"PhotonCamera_${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())}.jpg"
|
||||
"PhotonCamera_${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date(date))}$withSuffix.jpg"
|
||||
val contentValues = ContentValues().apply {
|
||||
put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
|
||||
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
|
||||
@@ -858,6 +861,30 @@ object PhotoManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将连拍照片设为主图并重新生成缩略图
|
||||
*/
|
||||
suspend fun setMainBurstPhoto(context: Context, photoId: String, burstFile: File): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val photoDir = getPhotoDir(context, photoId)
|
||||
val mainPhotoFile = File(photoDir, PHOTO_FILE)
|
||||
val thumbnailFile = File(photoDir, THUMBNAIL_FILE)
|
||||
|
||||
if (burstFile.exists()) {
|
||||
burstFile.copyTo(mainPhotoFile, overwrite = true)
|
||||
generateThumbnail(burstFile, thumbnailFile)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to set main burst photo", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有连拍照片
|
||||
*/
|
||||
|
||||
@@ -71,10 +71,10 @@ fun BurstCaptureOverlay(
|
||||
) {
|
||||
Text(
|
||||
text = count.toString(),
|
||||
fontSize = 60.sp,
|
||||
fontSize = 36.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = Color.White,
|
||||
modifier = Modifier.padding(top = 20.dp).scale(scale)
|
||||
modifier = Modifier.padding(top = 16.dp).scale(scale)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,13 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.ui.draw.clip
|
||||
import coil.compose.AsyncImage
|
||||
import coil.request.ImageRequest
|
||||
import com.hinnka.mycamera.R
|
||||
import com.hinnka.mycamera.gallery.PhotoManager
|
||||
@@ -39,16 +46,19 @@ import java.io.File
|
||||
import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.ui.input.pointer.PointerEvent
|
||||
import androidx.compose.ui.input.pointer.PointerEventPass
|
||||
import androidx.compose.ui.input.pointer.PointerEventType
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.core.content.FileProvider
|
||||
import com.hinnka.mycamera.gallery.PhotoData
|
||||
import com.hinnka.mycamera.viewmodel.GalleryViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@@ -96,6 +106,12 @@ fun BurstDetailScreen(
|
||||
val pagerState = rememberPagerState(pageCount = { burstFiles.size })
|
||||
val currentFile = burstFiles.getOrNull(pagerState.currentPage)
|
||||
|
||||
val mainPhotoFile = remember(photoId) { PhotoManager.getPhotoFile(context, photoId) }
|
||||
val refreshKey = viewModel.photoRefreshKeys[photoId] ?: 0L
|
||||
val isMainPhoto = remember(currentFile, refreshKey) {
|
||||
currentFile?.exists() == true && mainPhotoFile.exists() && currentFile.length() == mainPhotoFile.length()
|
||||
}
|
||||
|
||||
var showDeleteDialog by remember { mutableStateOf(false) }
|
||||
var isZoomed by remember { mutableStateOf(false) }
|
||||
var showExportDialog by remember { mutableStateOf(false) }
|
||||
@@ -155,9 +171,21 @@ fun BurstDetailScreen(
|
||||
)
|
||||
}*/
|
||||
|
||||
// 导出
|
||||
// 设为主图
|
||||
IconButton(
|
||||
onClick = { currentFile?.let { showExportDialog = true } },
|
||||
onClick = {
|
||||
currentFile?.let { file ->
|
||||
isSaving = true
|
||||
viewModel.setMainBurstPhoto(photoData, file) { success ->
|
||||
isSaving = false
|
||||
if (success) {
|
||||
Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.size(56.dp)
|
||||
.background(Color.White.copy(alpha = 0.1f), CircleShape)
|
||||
@@ -170,13 +198,27 @@ fun BurstDetailScreen(
|
||||
)
|
||||
} else {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Output,
|
||||
contentDescription = stringResource(R.string.export),
|
||||
tint = AccentOrange
|
||||
imageVector = if (isMainPhoto) Icons.Default.Star else Icons.Default.StarBorder,
|
||||
contentDescription = "设为主图",
|
||||
tint = if (isMainPhoto) AccentOrange else Color.White
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 导出
|
||||
IconButton(
|
||||
onClick = { currentFile?.let { showExportDialog = true } },
|
||||
modifier = Modifier
|
||||
.size(56.dp)
|
||||
.background(Color.White.copy(alpha = 0.1f), CircleShape)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Output,
|
||||
contentDescription = stringResource(R.string.export),
|
||||
tint = AccentOrange
|
||||
)
|
||||
}
|
||||
|
||||
// 导出全部
|
||||
IconButton(
|
||||
onClick = { if (burstFiles.isNotEmpty()) showExportAllDialog = true },
|
||||
@@ -192,7 +234,7 @@ fun BurstDetailScreen(
|
||||
)
|
||||
} else {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Collections,
|
||||
painterResource(R.drawable.ic_output_all),
|
||||
contentDescription = stringResource(R.string.export_all),
|
||||
tint = AccentOrange
|
||||
)
|
||||
@@ -218,7 +260,7 @@ fun BurstDetailScreen(
|
||||
containerColor = Color.Black,
|
||||
modifier = modifier
|
||||
) { paddingValues ->
|
||||
Box(
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues)
|
||||
@@ -235,7 +277,7 @@ fun BurstDetailScreen(
|
||||
HorizontalPager(
|
||||
state = pagerState,
|
||||
key = { page -> if (page < burstFiles.size) burstFiles[page].absolutePath else page },
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
modifier = Modifier.fillMaxWidth().weight(1f),
|
||||
userScrollEnabled = !isZoomed,
|
||||
beyondViewportPageCount = 1
|
||||
) { page ->
|
||||
@@ -244,7 +286,6 @@ fun BurstDetailScreen(
|
||||
key(photo.absolutePath) {
|
||||
|
||||
var showOrigin by remember { mutableStateOf(false) }
|
||||
var isPlaying by remember { mutableStateOf(false) }
|
||||
|
||||
Box(modifier = Modifier.fillMaxSize().pointerInput(Unit) {
|
||||
awaitPointerEventScope {
|
||||
@@ -294,7 +335,6 @@ fun BurstDetailScreen(
|
||||
}
|
||||
}
|
||||
showOrigin = false
|
||||
isPlaying = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -317,41 +357,73 @@ fun BurstDetailScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AnimatedVisibility(visible = !isZoomed) {
|
||||
val lazyListState = rememberLazyListState()
|
||||
|
||||
LaunchedEffect(pagerState.currentPage) {
|
||||
lazyListState.animateScrollToItem(max(0, pagerState.currentPage - 2))
|
||||
}
|
||||
|
||||
LazyRow(
|
||||
state = lazyListState,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(bottom = 16.dp),
|
||||
contentPadding = PaddingValues(horizontal = 16.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
itemsIndexed(burstFiles) { index, file ->
|
||||
val isSelected = pagerState.currentPage == index
|
||||
val isThisMainPhoto = remember(file, refreshKey) {
|
||||
file.exists() && mainPhotoFile.exists() && file.length() == mainPhotoFile.length()
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(60.dp)
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.border(
|
||||
width = if (isSelected) 2.dp else 0.dp,
|
||||
color = if (isSelected) AccentOrange else Color.Transparent,
|
||||
shape = RoundedCornerShape(8.dp)
|
||||
)
|
||||
.clickable {
|
||||
coroutineScope.launch {
|
||||
pagerState.animateScrollToPage(index)
|
||||
}
|
||||
}
|
||||
) {
|
||||
AsyncImage(
|
||||
model = file,
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
)
|
||||
if (isThisMainPhoto) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Star,
|
||||
contentDescription = "",
|
||||
tint = AccentOrange,
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopEnd)
|
||||
.padding(horizontal = 4.dp, vertical = 2.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除确认对话框
|
||||
if (showDeleteDialog) {
|
||||
var deleteExported by remember { mutableStateOf(false) }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = { showDeleteDialog = false },
|
||||
title = { Text(stringResource(R.string.delete)) },
|
||||
text = {
|
||||
Column {
|
||||
Text(stringResource(R.string.delete_confirm))
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.clickable { deleteExported = !deleteExported }
|
||||
) {
|
||||
Checkbox(
|
||||
checked = deleteExported,
|
||||
onCheckedChange = { deleteExported = it },
|
||||
colors = CheckboxDefaults.colors(
|
||||
checkedColor = Color(0xFFFF6B35),
|
||||
uncheckedColor = Color.White.copy(alpha = 0.6f)
|
||||
)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = stringResource(R.string.delete_exported_photos),
|
||||
color = Color.White.copy(alpha = 0.9f),
|
||||
fontSize = 14.sp
|
||||
)
|
||||
}
|
||||
}
|
||||
Text(stringResource(R.string.delete_confirm))
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
@@ -399,7 +471,8 @@ fun BurstDetailScreen(
|
||||
isSaving = true
|
||||
coroutineScope.launch(Dispatchers.IO) {
|
||||
val bitmap = BitmapFactory.decodeFile(it.path)
|
||||
viewModel.exportPhoto(photoData, bitmap) { success ->
|
||||
val suffix = "burst${pagerState.currentPage + 1}"
|
||||
viewModel.exportPhoto(photoData, bitmap, suffix = suffix) { success ->
|
||||
isSaving = false
|
||||
if (success) {
|
||||
Toast.makeText(context, R.string.export_success, Toast.LENGTH_SHORT).show()
|
||||
@@ -441,10 +514,11 @@ fun BurstDetailScreen(
|
||||
isSavingAll = true
|
||||
coroutineScope.launch(Dispatchers.IO) {
|
||||
var allSuccess = true
|
||||
for (file in burstFiles) {
|
||||
burstFiles.forEachIndexed { index, file ->
|
||||
val success = suspendCancellableCoroutine { continuation ->
|
||||
val bitmap = BitmapFactory.decodeFile(file.path)
|
||||
viewModel.exportPhoto(photoData, bitmap) { result ->
|
||||
val suffix = "burst${index + 1}"
|
||||
viewModel.exportPhoto(photoData, bitmap, suffix = suffix) { result ->
|
||||
continuation.resume(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,13 @@ class ShutterSoundPlayer(private val context: Context) {
|
||||
companion object {
|
||||
private const val TAG = "ShutterSoundPlayer"
|
||||
private const val SHUTTER_SOUND_PATH = "shutter.mp3"
|
||||
private const val BURST_SOUND_PATH = "burst.mp3"
|
||||
}
|
||||
|
||||
private var soundPool: SoundPool? = null
|
||||
private var soundId: Int = -1
|
||||
private var burstSoundId: Int = -1
|
||||
private var burstStreamId: Int = -1
|
||||
private var isLoaded = false
|
||||
|
||||
init {
|
||||
@@ -52,7 +55,9 @@ class ShutterSoundPlayer(private val context: Context) {
|
||||
}
|
||||
|
||||
val afd = context.assets.openFd(SHUTTER_SOUND_PATH)
|
||||
val burstAfd = context.assets.openFd(BURST_SOUND_PATH)
|
||||
soundId = soundPool?.load(afd, 1) ?: -1
|
||||
burstSoundId = soundPool?.load(burstAfd, 1) ?: -1
|
||||
afd.close()
|
||||
|
||||
PLog.d(TAG, "Shutter sound player initialized with SoundPool")
|
||||
@@ -82,6 +87,36 @@ class ShutterSoundPlayer(private val context: Context) {
|
||||
PLog.e(TAG, "Failed to play shutter sound", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放连拍快门音效
|
||||
*/
|
||||
fun playBurst() {
|
||||
if (soundPool == null || burstSoundId == -1 || !isLoaded) {
|
||||
PLog.w(TAG, "SoundPool not ready: soundPool=$soundPool, burstSoundId=$soundId, isLoaded=$isLoaded")
|
||||
// 如果是因为未初始化或加载失败,尝试重新初始化
|
||||
if (soundPool == null) {
|
||||
initializePlayer()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 播放音效:左声道音量, 右声道音量, 优先级, 循环次数(循环), 播放速率(1.0正常)
|
||||
burstStreamId = soundPool?.play(burstSoundId, 1.0f, 1.0f, 1, -1, 1.0f) ?: -1
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to play shutter sound", e)
|
||||
}
|
||||
}
|
||||
|
||||
fun stopBurst() {
|
||||
if (soundPool == null || burstStreamId == -1) return
|
||||
try {
|
||||
soundPool?.stop(burstStreamId)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to stop shutter sound", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
@@ -91,6 +126,7 @@ class ShutterSoundPlayer(private val context: Context) {
|
||||
soundPool?.release()
|
||||
soundPool = null
|
||||
soundId = -1
|
||||
burstSoundId = -1
|
||||
isLoaded = false
|
||||
PLog.d(TAG, "Shutter sound player released")
|
||||
} catch (e: Exception) {
|
||||
|
||||
@@ -219,13 +219,6 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
burstCaptureInfo = captureInfo
|
||||
}
|
||||
burstImages.add(image)
|
||||
burstImageCount++
|
||||
viewModelScope.launch {
|
||||
processBurst()
|
||||
if (state.value.burstCapturing) {
|
||||
cameraController.startBurstCapture()
|
||||
}
|
||||
}
|
||||
} else if (state.value.useMultiFrame) {
|
||||
val count = state.value.multiFrameCount
|
||||
PLog.d(TAG, "Burst frame received: ${stackingImages.size + 1}/$count")
|
||||
@@ -487,7 +480,11 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
burstImages.clear()
|
||||
burstImageCount = 0
|
||||
burstPhotoId = UUID.randomUUID().toString()
|
||||
shutterSoundPlayer.playBurst()
|
||||
cameraController.startBurstCapture()
|
||||
viewModelScope.launch {
|
||||
processBurst()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -496,10 +493,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
fun stopContinuousCapture() {
|
||||
if (state.value.useRaw && state.value.isRawSupported) return
|
||||
cameraController.stopBurstCapture()
|
||||
burstImageCount = 0
|
||||
burstPhotoId = null
|
||||
shutterSoundPlayer.stopBurst()
|
||||
viewModelScope.launch {
|
||||
burstImages.clear()
|
||||
_imageSavedEvent.emit(Unit)
|
||||
}
|
||||
}
|
||||
@@ -1567,82 +1562,104 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun prepareBurst(context: Context, photoId: String, image: SafeImage, captureInfo: CaptureInfo) {
|
||||
// 保存当前配置信息
|
||||
val lutIdToSave = currentLutId.value
|
||||
val aspectRatio = state.value.aspectRatio
|
||||
val frameIdToSave = currentFrameId
|
||||
val sharpeningValue = sharpening.firstOrNull() ?: 0f
|
||||
val noiseReductionValue = noiseReduction.firstOrNull() ?: 0f
|
||||
val chromaNoiseReductionValue = chromaNoiseReduction.firstOrNull() ?: 0f
|
||||
val currentCameraId = cameraController.getCurrentCameraId()
|
||||
|
||||
// 计算旋转角度
|
||||
val sensorOrientation = cameraController.getSensorOrientation()
|
||||
val lensFacing = cameraController.getLensFacing()
|
||||
val deviceRotation = OrientationObserver.rotationDegrees.toInt()
|
||||
|
||||
// 基础旋转角度计算
|
||||
val baseRotation = if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {
|
||||
(sensorOrientation - deviceRotation + 360) % 360
|
||||
} else {
|
||||
(sensorOrientation + deviceRotation) % 360
|
||||
}
|
||||
|
||||
// 获取用户配置的摄像头方向偏移
|
||||
val userPrefs = userPreferencesRepository.userPreferences.firstOrNull()
|
||||
val orientationOffset = userPrefs?.cameraOrientationOffsets?.get(currentCameraId) ?: 0
|
||||
|
||||
// 应用方向偏移
|
||||
val rotation = (baseRotation + orientationOffset) % 360
|
||||
|
||||
// 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息
|
||||
val metadata = PhotoMetadata(
|
||||
lutId = lutIdToSave,
|
||||
frameId = frameIdToSave,
|
||||
colorRecipeParams = currentRecipeParams.value,
|
||||
sharpening = sharpeningValue,
|
||||
noiseReduction = noiseReductionValue,
|
||||
chromaNoiseReduction = chromaNoiseReductionValue,
|
||||
width = image.width,
|
||||
height = image.height,
|
||||
ratio = aspectRatio,
|
||||
rotation = rotation,
|
||||
deviceModel = captureInfo.model,
|
||||
brand = captureInfo.make.replaceFirstChar { it.uppercase() },
|
||||
dateTaken = captureInfo.captureTime,
|
||||
iso = captureInfo.iso,
|
||||
shutterSpeed = captureInfo.formatExposureTime(),
|
||||
focalLength = captureInfo.formatFocalLength(),
|
||||
focalLength35mm = captureInfo.formatFocalLength35mm(),
|
||||
aperture = captureInfo.formatAperture(),
|
||||
)
|
||||
|
||||
PhotoManager.preparePhoto(
|
||||
context, metadata, null, previewThumbnail,
|
||||
false, false, photoId
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun processBurst() = withContext(Dispatchers.IO) {
|
||||
val image = burstImages.removeFirstOrNull() ?: return@withContext
|
||||
val captureInfo = burstCaptureInfo ?: return@withContext
|
||||
val context = getApplication<Application>()
|
||||
val photoId = burstPhotoId ?: return@withContext
|
||||
try {
|
||||
val metadata = PhotoManager.loadMetadata(context, photoId)
|
||||
if (metadata == null) {
|
||||
// 保存当前配置信息
|
||||
val lutIdToSave = currentLutId.value
|
||||
val aspectRatio = state.value.aspectRatio
|
||||
val frameIdToSave = currentFrameId
|
||||
val sharpeningValue = sharpening.firstOrNull() ?: 0f
|
||||
val noiseReductionValue = noiseReduction.firstOrNull() ?: 0f
|
||||
val chromaNoiseReductionValue = chromaNoiseReduction.firstOrNull() ?: 0f
|
||||
val currentCameraId = cameraController.getCurrentCameraId()
|
||||
|
||||
// 计算旋转角度
|
||||
val sensorOrientation = cameraController.getSensorOrientation()
|
||||
val lensFacing = cameraController.getLensFacing()
|
||||
val deviceRotation = OrientationObserver.rotationDegrees.toInt()
|
||||
|
||||
// 基础旋转角度计算
|
||||
val baseRotation = if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {
|
||||
(sensorOrientation - deviceRotation + 360) % 360
|
||||
} else {
|
||||
(sensorOrientation + deviceRotation) % 360
|
||||
while (true) {
|
||||
if (!state.value.burstCapturing && burstImages.isEmpty()) run {
|
||||
burstPhotoId = null
|
||||
burstImageCount = 0
|
||||
burstCaptureInfo = null
|
||||
break
|
||||
}
|
||||
val image = burstImages.removeFirstOrNull() ?: run {
|
||||
delay(33)
|
||||
continue
|
||||
}
|
||||
val captureInfo = burstCaptureInfo ?: run {
|
||||
delay(33)
|
||||
continue
|
||||
}
|
||||
val context = getApplication<Application>()
|
||||
val photoId = burstPhotoId ?: run {
|
||||
delay(33)
|
||||
continue
|
||||
}
|
||||
burstImageCount++
|
||||
try {
|
||||
val metadata = PhotoManager.loadMetadata(context, photoId)
|
||||
if (metadata == null) {
|
||||
prepareBurst(context, photoId, image, captureInfo)
|
||||
}
|
||||
|
||||
// 获取用户配置的摄像头方向偏移
|
||||
val userPrefs = userPreferencesRepository.userPreferences.firstOrNull()
|
||||
val orientationOffset = userPrefs?.cameraOrientationOffsets?.get(currentCameraId) ?: 0
|
||||
|
||||
// 应用方向偏移
|
||||
val rotation = (baseRotation + orientationOffset) % 360
|
||||
|
||||
// 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息
|
||||
val metadata = PhotoMetadata(
|
||||
lutId = lutIdToSave,
|
||||
frameId = frameIdToSave,
|
||||
colorRecipeParams = currentRecipeParams.value,
|
||||
sharpening = sharpeningValue,
|
||||
noiseReduction = noiseReductionValue,
|
||||
chromaNoiseReduction = chromaNoiseReductionValue,
|
||||
width = image.width,
|
||||
height = image.height,
|
||||
ratio = aspectRatio,
|
||||
rotation = rotation,
|
||||
deviceModel = captureInfo.model,
|
||||
brand = captureInfo.make.replaceFirstChar { it.uppercase() },
|
||||
dateTaken = captureInfo.captureTime,
|
||||
iso = captureInfo.iso,
|
||||
shutterSpeed = captureInfo.formatExposureTime(),
|
||||
focalLength = captureInfo.formatFocalLength(),
|
||||
focalLength35mm = captureInfo.formatFocalLength35mm(),
|
||||
aperture = captureInfo.formatAperture(),
|
||||
val shouldAutoSave = autoSaveAfterCapture.firstOrNull() ?: false
|
||||
val photoQualityValue = photoQuality.firstOrNull() ?: 95
|
||||
PhotoManager.saveBurstPhoto(
|
||||
context,
|
||||
photoId,
|
||||
image,
|
||||
shouldAutoSave,
|
||||
photoProcessor,
|
||||
photoQualityValue,
|
||||
)
|
||||
|
||||
PhotoManager.preparePhoto(
|
||||
context, metadata, null, previewThumbnail,
|
||||
false, false, photoId
|
||||
)
|
||||
// PLog.d(TAG, "Image saved: $photoId")
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to save image", e)
|
||||
}
|
||||
val shouldAutoSave = autoSaveAfterCapture.firstOrNull() ?: false
|
||||
val photoQualityValue = photoQuality.firstOrNull() ?: 95
|
||||
PhotoManager.saveBurstPhoto(
|
||||
context,
|
||||
photoId,
|
||||
image,
|
||||
shouldAutoSave,
|
||||
photoProcessor,
|
||||
photoQualityValue,
|
||||
)
|
||||
PLog.d(TAG, "Image saved: $photoId")
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to save image", e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -600,6 +600,23 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设为连拍主图
|
||||
*/
|
||||
fun setMainBurstPhoto(photo: PhotoData, burstFile: File, onComplete: (Boolean) -> Unit) {
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
val success = PhotoManager.setMainBurstPhoto(context, photo.id, burstFile)
|
||||
if (success) {
|
||||
photoRefreshKeys[photo.id] = System.currentTimeMillis()
|
||||
loadPhotos()
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
onComplete(success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除选中的照片
|
||||
*
|
||||
@@ -1084,13 +1101,13 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
/**
|
||||
* 导出照片到公共目录(带 LUT 烘焙)
|
||||
*/
|
||||
fun exportPhoto(photo: PhotoData, bitmap: Bitmap? = null, onComplete: (Boolean) -> Unit = {}) {
|
||||
fun exportPhoto(photo: PhotoData, bitmap: Bitmap? = null, suffix: String? = null, onComplete: (Boolean) -> Unit = {}) {
|
||||
viewModelScope.launch {
|
||||
val metadata = photo.metadata ?: PhotoManager.loadMetadata(getApplication(), photo.id) ?: PhotoMetadata()
|
||||
val metadata = PhotoManager.loadMetadata(getApplication(), photo.id) ?: photo.metadata ?: PhotoMetadata()
|
||||
val context = getApplication<Application>()
|
||||
PhotoManager.exportPhoto(context, photo.id, bitmap, photoProcessor, metadata,
|
||||
sharpening.value, noiseReduction.value,
|
||||
chromaNoiseReduction.value, photoQuality.firstOrNull() ?: 95) { success ->
|
||||
chromaNoiseReduction.value, photoQuality.firstOrNull() ?: 95, suffix) { success ->
|
||||
if (success) {
|
||||
exitEditMode()
|
||||
loadPhotos()
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="1024"
|
||||
android:viewportHeight="1024">
|
||||
<path
|
||||
android:pathData="M512.1,55.2a39.6,39.6 0,0 1,39.7 39.7,39.6 39.6,0 0,1 -39.7,39.7L302.5,134.6a18.9,18.9 0,0 0,-18.9 18.9L283.6,711.8a18.9,18.9 0,0 0,18.9 18.9h566.8a18.9,18.9 0,0 0,18.9 -18.9v-71.8a39.6,39.6 0,0 1,39.7 -39.7,39.6 39.6,0 0,1 39.7,39.7v71.9l-0,2.5c-1.2,53 -44.9,95.8 -98.2,95.8h-97.2v62.2c0,54.3 -43.4,98.7 -97.1,98.7L151.9,971.1c-53.7,0 -97.1,-44.4 -97.1,-98.7L54.8,349.4c0,-54.2 43.4,-98.7 97.1,-98.7h52.3L204.2,153.5c0,-54.3 44.1,-98.3 98.3,-98.3h209.6zM145.1,963.1zM681.9,963.1zM142.9,962.9l0.6,0.1 -0.6,-0.1zM683.5,963l0.6,-0.1 -0.6,0.1zM134,961.6zM693,961.6zM131.8,961.1zM693.6,961.4zM129.7,960.6zM695.7,961zM698.4,960.3l0.9,-0.2 -0.9,0.2zM703.1,958.8l0.3,-0.1 -0.3,0.1zM121.4,958l0.7,0.2 -0.7,-0.2zM704.9,958.2l0.7,-0.2 -0.7,0.2zM119.5,957.2c0.2,0.1 0.4,0.2 0.6,0.2 -0.2,-0.1 -0.4,-0.2 -0.6,-0.2zM706.9,957.5l0.6,-0.2 -0.6,0.2zM117.4,956.3l0.8,0.3 -0.8,-0.3zM708.8,956.7l0.8,-0.3 -0.8,0.3zM115.2,955.4zM710.4,956l1.4,-0.6 -1.4,0.6zM113.3,954.5zM712.2,955.2l1.5,-0.7 -1.5,0.7zM111.5,953.6c0.4,0.2 0.8,0.4 1.2,0.6 -0.4,-0.2 -0.8,-0.4 -1.2,-0.6zM714.3,954.2c0.4,-0.2 0.8,-0.4 1.2,-0.6 -0.4,0.2 -0.8,0.4 -1.2,0.6zM109.6,952.6zM716.4,953.1l1,-0.5 -1,0.5zM107.6,951.4c0.3,0.2 0.7,0.4 1.1,0.6 -0.4,-0.2 -0.7,-0.4 -1.1,-0.6zM718.3,952l1.1,-0.6 -1.1,0.6zM105.7,950.3zM720,951.1l1.3,-0.8 -1.3,0.8zM104,949.2zM721.8,950c0.4,-0.2 0.8,-0.5 1.2,-0.7 -0.4,0.3 -0.8,0.5 -1.2,0.7zM102.3,948.1zM723.9,948.6l0.7,-0.5 -0.7,0.5zM100.4,946.8zM725.8,947.3l0.5,-0.4 -0.5,0.4zM98.7,945.5zM727.3,946.2l1,-0.7 -1,0.7zM97.1,944.3l0.8,0.6 -0.8,-0.6zM729.1,944.9l0.8,-0.6 -0.8,0.6zM95.3,942.8zM730.8,943.5l0.9,-0.7 -0.9,0.7zM93.7,941.5c0.3,0.3 0.7,0.6 1,0.8 -0.3,-0.3 -0.7,-0.6 -1,-0.8zM732.3,942.3l1,-0.8 -1,0.8zM92.1,940zM734,940.8l0.9,-0.8 -0.9,0.8zM90.4,938.4zM735.5,939.4zM736.9,938.1zM89,937.1zM738.5,936.5c0.4,-0.4 0.7,-0.7 1.1,-1.1 -0.3,0.4 -0.7,0.7 -1.1,1.1zM87.5,935.4zM86.2,934.1l0.7,0.8 -0.7,-0.8zM740.1,934.8l0.7,-0.8 -0.7,0.8zM84.9,932.6zM741.8,933l0.3,-0.4 -0.3,0.4zM83.3,930.6l0.6,0.7c-0.2,-0.2 -0.4,-0.5 -0.6,-0.7zM743.1,931.4l0.6,-0.7 -0.6,0.7zM82,929.1zM744.3,929.9zM745.8,928.1zM79.3,925.5l0.7,0.9 -0.7,-0.9zM747,926.4zM78.1,923.7zM748.2,924.7l0.7,-1 -0.7,1zM76.8,921.8l0.8,1.2 -0.8,-1.2zM749.4,922.9l0.8,-1.2 -0.8,1.2zM75.7,919.9l0.8,1.4 -0.8,-1.4zM750.5,921.3c0.3,-0.5 0.6,-0.9 0.8,-1.4 -0.3,0.5 -0.6,0.9 -0.8,1.4zM74.6,918.1zM751.7,919.4zM73.7,916.4zM752.8,917.4zM72.6,914.4c0.2,0.4 0.4,0.7 0.5,1.1l-0.5,-1.1zM753.8,915.5l0.6,-1.1 -0.6,1.1zM71.6,912.4zM754.8,913.5c0.2,-0.4 0.4,-0.8 0.6,-1.2 -0.2,0.4 -0.4,0.8 -0.6,1.2zM70.4,909.8zM755.8,911.6zM69.8,908.3l0.6,1.4 -0.6,-1.4zM756.7,909.6c0.2,-0.4 0.4,-0.9 0.6,-1.3 -0.2,0.5 -0.4,0.9 -0.6,1.3zM69.1,906.6l0.4,0.9 -0.4,-0.9zM757.6,907.5l0.4,-0.9 -0.4,0.9zM68.2,904.3zM758.4,905.5c0.2,-0.4 0.3,-0.8 0.4,-1.2 -0.1,0.4 -0.3,0.8 -0.4,1.2zM67.5,902.3zM759.1,903.5zM148,901.6zM678.9,901.6zM760.6,899.2zM134.1,895.1zM151.9,327.7c-10.7,0 -20.1,9.4 -20.1,21.7L131.8,872.5c0,12.4 9.4,21.7 20.1,21.7h523.2c10.7,0 20.1,-9.4 20.1,-21.7v-62.3L302.5,810.2c-54.2,0 -98.3,-44.1 -98.3,-98.3L204.2,327.7L151.9,327.7zM762.2,892.8zM63.7,887c0.1,0.5 0.2,1 0.2,1.5 -0.1,-0.5 -0.2,-1 -0.2,-1.5zM763.1,888.5l0.3,-1.5 -0.3,1.5zM63.1,882.9zM763.9,882.9zM702.2,878.9c0,-0.2 0.1,-0.3 0.1,-0.5l0,-0.1c-0,0.2 -0.1,0.4 -0.1,0.6zM295.6,802.2zM876.3,802.2zM293.3,802c0.2,0 0.5,0 0.8,0.1 -0.3,-0 -0.5,-0 -0.8,-0.1zM288.9,801.5zM883,801.5zM887.5,800.7zM286.5,801.1l1.2,0.2 -1.2,-0.2zM284.4,800.7zM890.9,799.9l1,-0.2 -1,0.2zM892.7,799.4l1.2,-0.3 -1.2,0.3zM277.9,799.1zM895,798.8c0.4,-0.1 0.8,-0.2 1.2,-0.4 -0.4,0.1 -0.8,0.2 -1.2,0.4zM275.7,798.4c0.3,0.1 0.7,0.2 1,0.3 -0.3,-0.1 -0.7,-0.2 -1,-0.3zM896.8,798.2zM273.6,797.8zM899.1,797.5l1.2,-0.4 -1.2,0.4zM271.6,797zM269.6,796.3l1,0.4 -1,-0.4zM901.5,796.6c0.2,-0.1 0.5,-0.2 0.7,-0.3 -0.2,0.1 -0.5,0.2 -0.7,0.3zM903.6,795.8l0.8,-0.3 -0.8,0.3zM267.6,795.5zM905.5,795zM265.6,794.6l0.7,0.3 -0.7,-0.3zM907.3,794.1zM263.4,793.6l1,0.5 -1,-0.5zM909,793.3zM261.5,792.7c0.4,0.2 0.8,0.4 1.2,0.6 -0.4,-0.2 -0.8,-0.4 -1.2,-0.6zM911.4,792.1zM259.7,791.7zM913.1,791.2zM257.7,790.6l1,0.6 -1,-0.6zM914.8,790.2l1.4,-0.8 -1.4,0.8zM255.8,789.5c0.4,0.2 0.7,0.4 1.1,0.7 -0.4,-0.2 -0.7,-0.4 -1.1,-0.7zM916.8,789.1c0.4,-0.2 0.7,-0.4 1.1,-0.7 -0.4,0.2 -0.7,0.5 -1.1,0.7zM254.2,788.5zM918.8,787.8zM252.2,787.2l0.8,0.5 -0.8,-0.5zM250.3,785.9l1.1,0.8 -1.1,-0.8zM920.5,786.6c0.4,-0.3 0.7,-0.5 1.1,-0.8 -0.4,0.3 -0.7,0.5 -1.1,0.8zM248.5,784.6zM922.1,785.5zM246.9,783.4zM923.9,784.1zM245.1,781.9c0.4,0.3 0.7,0.6 1.1,0.9l-1.1,-0.9zM925.7,782.8l1,-0.8 -1,0.8zM243.5,780.6zM927.4,781.3l0.8,-0.7 -0.8,0.7zM242,779.2zM929.1,779.9zM240.5,777.9l0.6,0.6 -0.6,-0.6zM930.8,778.4l0.6,-0.6 -0.6,0.6zM238.9,776.3l0.6,0.6 -0.6,-0.6zM932.3,776.9l0.6,-0.6 -0.6,0.6zM237.3,774.7l0.8,0.8 -0.8,-0.8zM933.8,775.5zM235.9,773.2l0.7,0.7 -0.7,-0.7zM935.3,773.9l0.7,-0.7 -0.7,0.7zM234.5,771.7l0.5,0.6 -0.5,-0.6zM936.8,772.3l0.5,-0.6 -0.5,0.6zM233.1,770.1l0.4,0.5 -0.4,-0.5zM938.3,770.6l0.4,-0.5 -0.4,0.5zM231.7,768.3zM939.6,769l0.8,-1 -0.8,1zM940.8,767.5zM230.3,766.6zM942.2,Line truncated
|
||||
android:fillColor="#ffffff"/>
|
||||
</vector>
|
||||
Reference in new issue
Block a user