feat: lut thumbnail optimize

This commit is contained in:
Hinnka committed 2026-01-25 18:04:12 +08:00
1 parent b1243d03c9
commit 7ed2e9f46f
6 files changed
+51 -91

No files matched your search

@@ -3,6 +3,7 @@ package com.hinnka.mycamera.data
import android.content.Context
import com.hinnka.mycamera.frame.FrameInfo
import com.hinnka.mycamera.frame.FrameManager
import com.hinnka.mycamera.lut.LutImageProcessor
import com.hinnka.mycamera.lut.LutInfo
import com.hinnka.mycamera.lut.LutManager
import kotlinx.coroutines.flow.MutableStateFlow
@@ -37,6 +38,8 @@ class ContentRepository private constructor(context: Context) {
val frameManager = FrameManager(appContext)
private val customImportManager = CustomImportManager(appContext)
val imageProcessor = LutImageProcessor()
// 使用 StateFlow 存储可用内容列表,支持响应式更新
private val _availableLuts = MutableStateFlow<List<LutInfo>>(emptyList())
val availableLuts: StateFlow<List<LutInfo>> = _availableLuts.asStateFlow()
@@ -38,10 +38,8 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import com.hinnka.mycamera.R
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.CameraState
import com.hinnka.mycamera.camera.CameraUtils
import com.hinnka.mycamera.ui.camera.autoRotate
import com.hinnka.mycamera.ui.components.GalleryThumbnail
import com.hinnka.mycamera.ui.components.HistogramView
import com.hinnka.mycamera.ui.components.LutControlPanel
@@ -418,7 +416,7 @@ fun CameraScreen(
LutControlPanel(
availableLuts = viewModel.availableLutList,
currentLutId = currentLutId,
lutPreviewBitmaps = viewModel.lutPreviewBitmaps,
thumbnail = viewModel.previewThumbnail,
onLutSelected = { viewModel.setLut(it) },
categoryOrder = categoryOrder,
modifier = Modifier.fillMaxWidth()
@@ -22,6 +22,7 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
@@ -29,9 +30,12 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hinnka.mycamera.R
import com.hinnka.mycamera.data.ContentRepository
import com.hinnka.mycamera.lut.LutInfo
import com.hinnka.mycamera.ui.camera.LutEditBottomSheet
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
* LUT 选择器组件
@@ -42,7 +46,7 @@ import kotlinx.coroutines.launch
fun LutSelector(
availableLuts: List<LutInfo>,
currentLutId: String?,
lutPreviewBitmaps: Map<String, Bitmap> = emptyMap(),
thumbnail: Bitmap?,
onLutSelected: (String?) -> Unit,
onEditClick: (() -> Unit)? = null,
categoryOrder: List<String> = emptyList(),
@@ -145,8 +149,9 @@ fun LutSelector(
// LUT 列表
items(filteredLuts, key = { it.id }) { lut ->
LutItem(
id = lut.id,
name = lut.getName(),
previewBitmap = lutPreviewBitmaps[lut.id],
previewBitmap = thumbnail,
isSelected = currentLutId == lut.id,
isVip = lut.isVip,
isCustom = !lut.isBuiltIn,
@@ -173,6 +178,7 @@ fun LutSelector(
@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun LutItem(
id: String,
name: String,
previewBitmap: Bitmap?,
isSelected: Boolean,
@@ -182,6 +188,7 @@ private fun LutItem(
isCustom: Boolean = false, // 添加自定义标识参数
modifier: Modifier = Modifier
) {
val context = LocalContext.current
val backgroundColor = if (isSelected) {
Color.White.copy(alpha = 0.3f)
} else {
@@ -194,6 +201,28 @@ private fun LutItem(
Color.Gray.copy(alpha = 0.5f)
}
var bitmap by remember(previewBitmap) { mutableStateOf(previewBitmap) }
LaunchedEffect(previewBitmap) {
val contentRepository = ContentRepository.getInstance(context)
val lutConfig = withContext(Dispatchers.IO) {
contentRepository.lutManager.loadLut(id)
}
if (lutConfig != null) {
val colorRecipeParams = contentRepository.lutManager.loadColorRecipeParams(id)
// LUT 预览生成:禁用软件处理(预览图小,不需要降噪/锐化)
bitmap = previewBitmap?.let {
contentRepository.imageProcessor.applyLut(
bitmap = it,
lutConfig = lutConfig,
colorRecipeParams = colorRecipeParams
// 预览不需要软件处理
)
}
}
}
Column(
modifier = modifier
.width(60.dp)
@@ -217,7 +246,7 @@ private fun LutItem(
.then(
if (isNone) {
Modifier.background(Color.DarkGray)
} else if (previewBitmap != null) {
} else if (bitmap != null) {
// 显示真实预览图
Modifier
} else {
@@ -236,9 +265,9 @@ private fun LutItem(
contentAlignment = Alignment.Center
) {
// 显示预览图片
if (!isNone && previewBitmap != null) {
if (!isNone && bitmap != null) {
Image(
bitmap = previewBitmap.asImageBitmap(),
bitmap = bitmap!!.asImageBitmap(),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.fillMaxSize()
@@ -345,7 +374,7 @@ private fun LutItem(
fun LutControlPanel(
availableLuts: List<LutInfo>,
currentLutId: String?,
lutPreviewBitmaps: Map<String, Bitmap> = emptyMap(),
thumbnail: Bitmap?,
onLutSelected: (String?) -> Unit,
categoryOrder: List<String> = emptyList(),
modifier: Modifier = Modifier
@@ -411,7 +440,7 @@ fun LutControlPanel(
LutSelector(
availableLuts = availableLuts,
currentLutId = currentLutId,
lutPreviewBitmaps = lutPreviewBitmaps,
thumbnail = thumbnail,
onLutSelected = onLutSelected,
onEditClick = { showLutEditDialog = true },
categoryOrder = categoryOrder
@@ -85,7 +85,7 @@ fun PhotoEditScreen(
// 预览 Bitmap 状态
var previewBitmap by remember { mutableStateOf<Bitmap?>(null) }
var lutPreviews by remember { mutableStateOf<Map<String, Bitmap>>(emptyMap()) }
var thumbnailBitmap by remember { mutableStateOf<Bitmap?>(null) }
// 边框编辑状态
val editFrameId by viewModel.editFrameId.collectAsState()
@@ -120,8 +120,8 @@ fun PhotoEditScreen(
previewBitmap = withContext(Dispatchers.IO) {
viewModel.getPreviewBitmap(currentPhoto, useGlobalEdit = true, showOrigin = showOrigin)
}
lutPreviews = withContext(Dispatchers.IO) {
viewModel.loadLutPreviews(currentPhoto)
thumbnailBitmap = withContext(Dispatchers.IO) {
viewModel.loadThumbnail(currentPhoto)
}
isLoadingPreview = false
}
@@ -371,7 +371,7 @@ fun PhotoEditScreen(
LutSelector(
availableLuts = viewModel.availableLuts,
currentLutId = editLutId,
lutPreviewBitmaps = lutPreviews,
thumbnail = thumbnailBitmap,
onLutSelected = { viewModel.setEditLut(it) },
onEditClick = { showLutEditDialog = true },
categoryOrder = categoryOrder
@@ -6,7 +6,6 @@ import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CaptureResult
import android.media.Image
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
@@ -23,7 +22,6 @@ import com.hinnka.mycamera.gallery.PhotoManager
import com.hinnka.mycamera.gallery.PhotoMetadata
import com.hinnka.mycamera.gallery.PhotoProcessor
import com.hinnka.mycamera.lut.LutConfig
import com.hinnka.mycamera.lut.LutImageProcessor
import com.hinnka.mycamera.lut.LutInfo
import com.hinnka.mycamera.model.ColorRecipeParams
import com.hinnka.mycamera.utils.OrientationObserver
@@ -52,7 +50,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 内容仓库(单例,与 GalleryViewModel 共享)
private val contentRepository = ContentRepository.getInstance(application)
private val lutImageProcessor = LutImageProcessor()
private val lutImageProcessor = contentRepository.imageProcessor
// 计费管理器
private val billingManager = com.hinnka.mycamera.billing.BillingManagerImpl(application)
@@ -99,8 +97,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
private set
// LUT 预览图缓存(lutId -> 预览Bitmap)
var lutPreviewBitmaps: Map<String, Bitmap> by mutableStateOf(emptyMap())
private set
var previewThumbnail by mutableStateOf<Bitmap?>(null)
// 是否正在生成预览
private var isGeneratingPreviews = false
@@ -654,39 +651,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 设置回调接收原始 YUV 预览帧
cameraController.onPreviewFrameCaptured = { bitmap ->
viewModelScope.launch(Dispatchers.Default) {
try {
// 生成所有 LUT 预览
val newPreviews = mutableMapOf<String, Bitmap>()
availableLutList.forEach { lutInfo ->
val lutConfig = withContext(Dispatchers.IO) {
contentRepository.lutManager.loadLut(lutInfo.id)
}
if (lutConfig != null) {
val colorRecipeParams = contentRepository.lutManager.loadColorRecipeParams(lutInfo.id)
// LUT 预览生成:禁用软件处理(预览图小,不需要降噪/锐化)
val previewBitmap = lutImageProcessor.applyLut(
bitmap = bitmap,
lutConfig = lutConfig,
colorRecipeParams = colorRecipeParams
// 预览不需要软件处理
)
newPreviews[lutInfo.id] = previewBitmap
}
}
// 更新预览图
withContext(Dispatchers.Main) {
lutPreviewBitmaps.values.forEach { it.recycle() }
lutPreviewBitmaps = newPreviews
isGeneratingPreviews = false
PLog.d(TAG, "Generated ${newPreviews.size} LUT previews from YUV")
}
} catch (e: Exception) {
PLog.e(TAG, "Failed to generate LUT previews", e)
isGeneratingPreviews = false
}
previewThumbnail = bitmap
isGeneratingPreviews = false
}
}
@@ -743,14 +709,6 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
}
/**
* 清除 LUT 预览缓存
*/
fun clearLutPreviews() {
lutPreviewBitmaps.values.forEach { it.recycle() }
lutPreviewBitmaps = emptyMap()
}
// ==================== 边框相关方法 ====================
/**
@@ -1254,8 +1212,5 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
lutImageProcessor.release()
contentRepository.frameManager.clearCache()
shutterSoundPlayer.release()
// 清理 LUT 预览图
clearLutPreviews()
}
}
@@ -46,7 +46,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
// 内容仓库(单例,与 CameraViewModel 共享)
private val contentRepository = ContentRepository.getInstance(application)
private val lutImageProcessor = LutImageProcessor()
private val lutImageProcessor = contentRepository.imageProcessor
private val frameRenderer = FrameRenderer(application)
private val photoProcessor = PhotoProcessor(
contentRepository.lutManager,
@@ -403,37 +403,12 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
}
}
suspend fun loadLutPreviews(photo: PhotoData): Map<String, Bitmap> {
fun loadThumbnail(photo: PhotoData): Bitmap? {
val context = getApplication<Application>()
val inputStream = context.contentResolver.openInputStream(photo.thumbnailUri)
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
if (bitmap == null) return emptyMap()
val previews = mutableMapOf<String, Bitmap>()
availableLuts.forEach { lutInfo ->
val lutConfig = withContext(Dispatchers.IO) {
contentRepository.lutManager.loadLut(lutInfo.id)
}
if (lutConfig != null) {
val colorRecipeParams = withContext(Dispatchers.IO) {
contentRepository.lutManager.loadColorRecipeParams(lutInfo.id)
}
lutImageProcessor.applyLut(
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false),
lutConfig = lutConfig,
colorRecipeParams = colorRecipeParams
// 预览不需要软件处理
).let { preview ->
previews[lutInfo.id] = preview
}
}
}
return previews
return bitmap
}
/**