diff --git a/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt b/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt index 22dda48d..1ee1aeaf 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt @@ -33,6 +33,9 @@ class CustomImportManager(private val context: Context) { private const val CUSTOM_LUT_CONFIG = "custom_luts.json" private const val CUSTOM_FRAME_CONFIG = "custom_frames.json" private const val CATEGORY_OVERRIDES_CONFIG = "category_overrides.json" + + // 自定义字体目录 + private const val CUSTOM_FONT_DIR = "custom_fonts" } /** @@ -60,6 +63,9 @@ class CustomImportManager(private val context: Context) { private val customFrameDir: File get() = File(context.filesDir, CUSTOM_FRAME_DIR).apply { mkdirs() } + private val customFontDir: File + get() = File(context.filesDir, CUSTOM_FONT_DIR).apply { mkdirs() } + /** * 导入 LUT 文件 (.cube) * @@ -248,6 +254,27 @@ class CustomImportManager(private val context: Context) { } } + /** + * 导入字体文件 + */ + fun importFont(uri: Uri): String? { + return try { + val fileName = getFileName(uri) ?: "font_${UUID.randomUUID()}.ttf" + val fontFile = File(customFontDir, fileName) + + context.contentResolver.openInputStream(uri)?.use { inputStream -> + fontFile.outputStream().use { outputStream -> + inputStream.copyTo(outputStream) + } + } ?: return null + + fontFile.absolutePath + } catch (e: Exception) { + PLog.e(TAG, "Failed to import font", e) + null + } + } + /** * 获取所有自定义 LUT */ diff --git a/app/src/main/java/com/hinnka/mycamera/frame/FrameRenderer.kt b/app/src/main/java/com/hinnka/mycamera/frame/FrameRenderer.kt index 646d3c2a..7f20d204 100644 --- a/app/src/main/java/com/hinnka/mycamera/frame/FrameRenderer.kt +++ b/app/src/main/java/com/hinnka/mycamera/frame/FrameRenderer.kt @@ -440,7 +440,7 @@ class FrameRenderer(private val context: Context) { is FrameElement.Text -> { val text = getTextContent(element, metadata) ?: return 0f textPaint.textSize = spToPx(element.fontSizeSp) * scale - textPaint.typeface = getTypeface(element.fontWeight, element.fontFamily) + textPaint.typeface = getTextTypeface(element, metadata) textPaint.measureText(text) + dpToPx(8) * scale } @@ -521,7 +521,7 @@ class FrameRenderer(private val context: Context) { textPaint.color = element.color textPaint.textSize = spToPx(element.fontSizeSp) * scale - textPaint.typeface = getTypeface(element.fontWeight, element.fontFamily) + textPaint.typeface = getTextTypeface(element, metadata) val textWidth = textPaint.measureText(text) val textHeight = textPaint.descent() - textPaint.ascent() @@ -754,7 +754,7 @@ class FrameRenderer(private val context: Context) { inMutable = true inPreferredConfig = Bitmap.Config.ARGB_8888 } - + // 优先使用文件路径(外部导入),其次使用资源名称(内置资源) when { layout.imagePath != null -> { @@ -764,6 +764,7 @@ class FrameRenderer(private val context: Context) { return originalBitmap } } + layout.imageResName != null -> { val resId = context.resources.getIdentifier(layout.imageResName, "drawable", context.packageName) if (resId == 0) { @@ -776,6 +777,7 @@ class FrameRenderer(private val context: Context) { return originalBitmap } } + else -> { PLog.e(TAG, "No image source specified for IMAGE frame") return originalBitmap @@ -797,8 +799,8 @@ class FrameRenderer(private val context: Context) { try { val originalFrame = frameBitmap val rotatedFrame = Bitmap.createBitmap( - originalFrame, 0, 0, - originalFrame.width, originalFrame.height, + originalFrame, 0, 0, + originalFrame.width, originalFrame.height, matrix, true ) frameBitmap = rotatedFrame @@ -810,7 +812,7 @@ class FrameRenderer(private val context: Context) { PLog.e(TAG, "Failed to rotate frame bitmap", e) } } - + // 检测透明区域的边界 val transparentBounds = detectTransparentBounds(frameBitmap) if (transparentBounds.width() <= 0 || transparentBounds.height() <= 0) { @@ -818,55 +820,55 @@ class FrameRenderer(private val context: Context) { frameBitmap.recycle() return originalBitmap } - + PLog.d(TAG, "Transparent bounds: $transparentBounds, frame size: ${frameBitmap.width}x${frameBitmap.height}") - + // 计算缩放比例,使透明区域能够容纳照片(以照片尺寸为准) val photoWidth = originalBitmap.width val photoHeight = originalBitmap.height val transparentWidth = transparentBounds.width() val transparentHeight = transparentBounds.height() - + // 计算边框需要缩放的比例,使透明区域与照片大小匹配 val scaleX = photoWidth.toFloat() / transparentWidth val scaleY = photoHeight.toFloat() / transparentHeight // 使用较大的缩放比例,确保照片完全填充透明区域(center crop 效果) val frameScale = maxOf(scaleX, scaleY) - + // 缩放后的边框尺寸 val scaledFrameWidth = (frameBitmap.width * frameScale).toInt() val scaledFrameHeight = (frameBitmap.height * frameScale).toInt() - + // 缩放后的透明区域边界 val scaledTransparentLeft = (transparentBounds.left * frameScale).toInt() val scaledTransparentTop = (transparentBounds.top * frameScale).toInt() val scaledTransparentWidth = (transparentWidth * frameScale).toInt() val scaledTransparentHeight = (transparentHeight * frameScale).toInt() - + // 计算照片在缩放后透明区域中的居中位置 val photoOffsetX = (scaledTransparentWidth - photoWidth) / 2 val photoOffsetY = (scaledTransparentHeight - photoHeight) / 2 - + // 缩放边框图片 val scaledFrame = Bitmap.createScaledBitmap(frameBitmap, scaledFrameWidth, scaledFrameHeight, true) frameBitmap.recycle() - + // 创建输出 Bitmap(与缩放后的边框大小相同) val output = createBitmap(scaledFrameWidth, scaledFrameHeight) val canvas = Canvas(output) - + // 先绘制照片到缩放后的透明区域位置(居中) val photoDrawX = scaledTransparentLeft + photoOffsetX val photoDrawY = scaledTransparentTop + photoOffsetY canvas.drawBitmap(originalBitmap, photoDrawX.toFloat(), photoDrawY.toFloat(), null) - + // 再绘制缩放后的边框图片(透明区域会显示下面的照片) canvas.drawBitmap(scaledFrame, 0f, 0f, null) scaledFrame.recycle() - + return output } - + /** * 检测图片中透明区域的边界 * @@ -878,24 +880,24 @@ class FrameRenderer(private val context: Context) { private fun detectTransparentBounds(bitmap: Bitmap): Rect { val width = bitmap.width val height = bitmap.height - + // 透明度阈值(低于此值认为是透明的) val alphaThreshold = 10 - + var minX = width var minY = height var maxX = 0 var maxY = 0 - + // 扫描所有像素找出透明区域边界 val pixels = IntArray(width * height) bitmap.getPixels(pixels, 0, width, 0, 0, width, height) - + for (y in 0 until height) { for (x in 0 until width) { val pixel = pixels[y * width + x] val alpha = (pixel shr 24) and 0xFF - + if (alpha < alphaThreshold) { if (x < minX) minX = x if (x > maxX) maxX = x @@ -904,12 +906,12 @@ class FrameRenderer(private val context: Context) { } } } - + // 如果没有找到透明区域,返回空矩形 if (minX > maxX || minY > maxY) { return Rect(0, 0, 0, 0) } - + return Rect(minX, minY, maxX + 1, maxY + 1) } @@ -984,4 +986,32 @@ class FrameRenderer(private val context: Context) { FontWeight.BOLD -> Typeface.DEFAULT_BOLD } } + + private fun getTextTypeface(element: FrameElement.Text, metadata: PhotoMetadata): Typeface { + if (element.textType == TextType.DEVICE_MODEL) { + val customFont = metadata.customProperties["DEVICE_MODEL_FONT"] + if (customFont == "Default") { + return getTypeface(element.fontWeight, null) + } else if (customFont == "SlacksideOne") { + return getTypeface(element.fontWeight, "SlacksideOne.ttf") + } else if (customFont != null && customFont.startsWith("/")) { + // It's a file path + val cacheKey = "file-$customFont-${element.fontWeight}" + typefaceCache[cacheKey]?.let { return it } + try { + val base = Typeface.createFromFile(customFont) + val style = when (element.fontWeight) { + FontWeight.BOLD -> Typeface.BOLD + else -> Typeface.NORMAL + } + val typeface = Typeface.create(base, style) + typefaceCache[cacheKey] = typeface + return typeface + } catch (e: Exception) { + PLog.e(TAG, "Failed to load custom font from file: $customFont", e) + } + } + } + return getTypeface(element.fontWeight, element.fontFamily) + } } diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt index cba7c2d1..e29123d6 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt @@ -947,20 +947,19 @@ class LutImageProcessor { color.rgb *= pow(2.0, uExposure); // 2. 高光/阴影调整(分区调整,基于亮度 mask) - // 使用标准的 NTSC 权重 float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114)); - float highlightMask = smoothstep(0.5, 1.0, luma); - float shadowMask = smoothstep(0.5, 0.0, luma); - float highlightFactor = 1.0 + uHighlights * 0.5; - color.rgb = mix(color.rgb, color.rgb * highlightFactor, highlightMask); - - vec3 shadowTarget; - if (uShadows > 0.0) { - shadowTarget = mix(color.rgb, vec3(1.0) * luma, uShadows * 0.2) + (color.rgb * uShadows * 0.5); - } else { - shadowTarget = color.rgb * (1.0 + uShadows * 0.5); - } - color.rgb = mix(color.rgb, shadowTarget, shadowMask); + float shadowMask = smoothstep(0.5, 0.2, luma); // 暗部 + float highlightMask = smoothstep(0.5, 0.8, luma); // 亮部 + + // --- 阴影处理 (Shadows) --- + float shadowIntensity = uShadows * shadowMask; + color.rgb += color.rgb * shadowIntensity * 0.6; + + // --- 高光处理 (Highlights) --- + float highlightIntensity = uHighlights * highlightMask; + vec3 highlightTarget = mix(color.rgb, vec3(1.0), 0.5); // 提亮目标偏向一点白 + if (uHighlights < 0.0) highlightTarget = color.rgb * 0.8; // 压暗目标 + color.rgb = mix(color.rgb, highlightTarget, abs(highlightIntensity)); // 3. 对比度(围绕中灰点调整) color.rgb = (color.rgb - 0.5) * uContrast + 0.5; diff --git a/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt b/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt index 65497bec..53e9c73d 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt @@ -192,20 +192,19 @@ object Shaders { color.rgb *= pow(2.0, uExposure); // 2. 高光/阴影调整(分区调整,基于亮度 mask) - // 使用标准的 NTSC 权重 float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114)); - float highlightMask = smoothstep(0.5, 1.0, luma); - float shadowMask = smoothstep(0.5, 0.0, luma); - float highlightFactor = 1.0 + uHighlights * 0.5; - color.rgb = mix(color.rgb, color.rgb * highlightFactor, highlightMask); - - vec3 shadowTarget; - if (uShadows > 0.0) { - shadowTarget = mix(color.rgb, vec3(1.0) * luma, uShadows * 0.2) + (color.rgb * uShadows * 0.5); - } else { - shadowTarget = color.rgb * (1.0 + uShadows * 0.5); - } - color.rgb = mix(color.rgb, shadowTarget, shadowMask); + float shadowMask = smoothstep(0.5, 0.2, luma); // 暗部 + float highlightMask = smoothstep(0.5, 0.8, luma); // 亮部 + + // --- 阴影处理 (Shadows) --- + float shadowIntensity = uShadows * shadowMask; + color.rgb += color.rgb * shadowIntensity * 0.6; + + // --- 高光处理 (Highlights) --- + float highlightIntensity = uHighlights * highlightMask; + vec3 highlightTarget = mix(color.rgb, vec3(1.0), 0.5); // 提亮目标偏向一点白 + if (uHighlights < 0.0) highlightTarget = color.rgb * 0.8; // 压暗目标 + color.rgb = mix(color.rgb, highlightTarget, abs(highlightIntensity)); // 3. 对比度(围绕中灰点调整) color.rgb = (color.rgb - 0.5) * uContrast + 0.5; diff --git a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt index 2b0b246f..f0a48b91 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt @@ -336,18 +336,18 @@ class RawDemosaicProcessor { // 2. 高光/阴影调整(分区调整,基于亮度 mask) float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114)); - float highlightMask = smoothstep(0.5, 1.0, luma); - float shadowMask = smoothstep(0.5, 0.0, luma); - float highlightFactor = 1.0 + uHighlights * 0.5; - color.rgb = mix(color.rgb, color.rgb * highlightFactor, highlightMask); - - vec3 shadowTarget; - if (uShadows > 0.0) { - shadowTarget = mix(color.rgb, vec3(1.0) * luma, uShadows * 0.2) + (color.rgb * uShadows * 0.5); - } else { - shadowTarget = color.rgb * (1.0 + uShadows * 0.5); - } - color.rgb = mix(color.rgb, shadowTarget, shadowMask); + float shadowMask = smoothstep(0.5, 0.2, luma); // 暗部 + float highlightMask = smoothstep(0.5, 0.8, luma); // 亮部 + + // --- 阴影处理 (Shadows) --- + float shadowIntensity = uShadows * shadowMask; + color.rgb += color.rgb * shadowIntensity * 0.6; + + // --- 高光处理 (Highlights) --- + float highlightIntensity = uHighlights * highlightMask; + vec3 highlightTarget = mix(color.rgb, vec3(1.0), 0.5); // 提亮目标偏向一点白 + if (uHighlights < 0.0) highlightTarget = color.rgb * 0.8; // 压暗目标 + color.rgb = mix(color.rgb, highlightTarget, abs(highlightIntensity)); // 3. 对比度(围绕中灰点调整) color.rgb = (color.rgb - 0.5) * uContrast + 0.5; diff --git a/app/src/main/java/com/hinnka/mycamera/ui/common/WatermarkEditSheet.kt b/app/src/main/java/com/hinnka/mycamera/ui/common/WatermarkEditSheet.kt new file mode 100644 index 00000000..4abd533c --- /dev/null +++ b/app/src/main/java/com/hinnka/mycamera/ui/common/WatermarkEditSheet.kt @@ -0,0 +1,227 @@ +package com.hinnka.mycamera.ui.common + +import android.net.Uri +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyRow +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.hinnka.mycamera.R +import com.hinnka.mycamera.frame.TextType +import com.hinnka.mycamera.ui.theme.AccentOrange + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun WatermarkEditSheet( + customProperties: Map, + onPropertiesChange: (Map) -> Unit, + onDismiss: () -> Unit, + originalValues: Map = emptyMap(), + onImportFont: (Uri) -> String? = { null } // Returns the stored font path/id +) { + val sheetState = rememberModalBottomSheetState() + val properties = remember(customProperties) { + mutableStateMapOf().apply { + putAll(customProperties) + } + } + + val context = LocalContext.current + val fontPicker = rememberLauncherForActivityResult( + contract = ActivityResultContracts.OpenDocument() + ) { uri: Uri? -> + uri?.let { + val fontPath = onImportFont(it) + if (fontPath != null) { + properties["DEVICE_MODEL_FONT"] = fontPath + onPropertiesChange(properties.toMap()) + } + } + } + + ModalBottomSheet( + onDismissRequest = onDismiss, + sheetState = sheetState, + containerColor = Color(0xFF1A1A1A), + contentColor = Color.White, + scrimColor = Color.Transparent + ) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp) + .padding(bottom = 32.dp) + ) { + Text( + text = stringResource(R.string.watermark_adjustment), + fontSize = 18.sp, + fontWeight = FontWeight.Bold, + modifier = Modifier.padding(bottom = 16.dp) + ) + + // Logo 选择 + Text( + text = stringResource(R.string.logo), + fontSize = 14.sp, + color = Color.White.copy(alpha = 0.6f), + modifier = Modifier.padding(bottom = 8.dp) + ) + + val logos = listOf( + "none" to stringResource(R.string.none), + "apple" to "Apple", + "samsung" to "Samsung", + "xiaomi" to "Xiaomi", + "huawei" to "Huawei", + "honor" to "Honor", + "oppo" to "OPPO", + "vivo" to "Vivo", + "sony" to "Sony", + "canon" to "Canon", + "nikon" to "Nikon", + "fujifilm" to "Fujifilm", + "leica" to "Leica", + "hasselblad" to "Hasselblad", + "dji" to "DJI", + "panasonic" to "Panasonic", + "olympus" to "Olympus", + "pentax" to "Pentax", + "ricoh" to "Ricoh" + ) + + val effectiveLogo = properties["LOGO"] ?: "none" + + LazyRow( + horizontalArrangement = Arrangement.spacedBy(8.dp), + modifier = Modifier.padding(bottom = 16.dp) + ) { + items(logos) { (id, name) -> + val isSelected = effectiveLogo == id + Surface( + onClick = { + properties["LOGO"] = id + onPropertiesChange(properties.toMap()) + }, + color = if (isSelected) AccentOrange.copy(alpha = 0.2f) else Color.White.copy(alpha = 0.1f), + shape = RoundedCornerShape(8.dp), + border = if (isSelected) BorderStroke(1.dp, AccentOrange) else null + ) { + Text( + text = name, + modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp), + fontSize = 13.sp, + color = if (isSelected) AccentOrange else Color.White + ) + } + } + } + + // 文字编辑 + Text( + text = stringResource(R.string.text_content), + fontSize = 14.sp, + color = Color.White.copy(alpha = 0.6f), + modifier = Modifier.padding(bottom = 8.dp) + ) + + val textTypes = listOf( + TextType.DEVICE_MODEL to stringResource(R.string.device_model), + TextType.BRAND to stringResource(R.string.brand_name), + TextType.DATE to stringResource(R.string.date_label), + TextType.TIME to stringResource(R.string.time_label), + TextType.DATETIME to stringResource(R.string.datetime_label) + ) + + textTypes.forEach { (type, label) -> + val originalValue = originalValues[type] ?: "" + + Column(modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp)) { + OutlinedTextField( + value = properties[type.name] ?: originalValue, + onValueChange = { + properties[type.name] = it + onPropertiesChange(properties.toMap()) + }, + label = { Text(label) }, + placeholder = { Text(stringResource(R.string.default_from_photo)) }, + modifier = Modifier.fillMaxWidth(), + colors = OutlinedTextFieldDefaults.colors( + focusedBorderColor = AccentOrange, + unfocusedBorderColor = Color.White.copy(alpha = 0.2f), + focusedLabelColor = AccentOrange, + unfocusedLabelColor = Color.White.copy(alpha = 0.5f), + cursorColor = AccentOrange + ), + singleLine = true + ) + + // 字体选项 (仅对 DEVICE_MODEL 生效) + if (type == TextType.DEVICE_MODEL) { + Spacer(modifier = Modifier.height(8.dp)) + Text( + text = stringResource(R.string.font_option), + fontSize = 12.sp, + color = Color.White.copy(alpha = 0.6f), + modifier = Modifier.padding(bottom = 4.dp) + ) + val fontOptions = listOf( + "Default" to stringResource(R.string.default_text), + "SlacksideOne" to "SlacksideOne", + "Custom" to stringResource(R.string.custom_import) + ) + val currentFont = properties["DEVICE_MODEL_FONT"] + + LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + items(fontOptions) { (id, name) -> + // 如果是自定义字体,且当前值是一个路径,则显示为选中自定义 + val isSelected = if (id == "Custom") { + currentFont != null && currentFont != "Default" && currentFont != "SlacksideOne" + } else { + currentFont == id + } + + Surface( + onClick = { + if (id == "Custom") { + fontPicker.launch(arrayOf("font/*", "application/octet-stream")) + } else { + properties["DEVICE_MODEL_FONT"] = id + onPropertiesChange(properties.toMap()) + } + }, + color = if (isSelected) AccentOrange.copy(alpha = 0.2f) else Color.White.copy(alpha = 0.1f), + shape = RoundedCornerShape(8.dp), + border = if (isSelected) BorderStroke(1.dp, AccentOrange) else null + ) { + Text( + text = if (id == "Custom" && isSelected) { + // 截取文件名显示 + currentFont?.substringAfterLast('/') ?: name + } else name, + modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp), + fontSize = 11.sp, + color = if (isSelected) AccentOrange else Color.White + ) + } + } + } + Spacer(modifier = Modifier.height(8.dp)) + } + } + } + } + } +} diff --git a/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt index b1ae05ee..b25dac1b 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt @@ -22,6 +22,7 @@ import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import com.hinnka.mycamera.ui.common.WatermarkEditSheet import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.asImageBitmap @@ -253,7 +254,7 @@ fun PhotoEditScreen( isMultiTouch = true break } - + val currentPosition = event.changes[0].position if ((currentPosition - initialPosition).getDistance() > touchSlop) { isMoved = true @@ -505,10 +506,30 @@ fun PhotoEditScreen( } if (viewModel.showWatermarkSheet) { + val currentPhoto = viewModel.getCurrentPhoto() + val originalValues = remember(currentPhoto) { + mutableMapOf().apply { + currentPhoto?.metadata?.let { metadata -> + put(TextType.DEVICE_MODEL, metadata.deviceModel ?: "") + put(TextType.BRAND, metadata.brand ?: "") + metadata.dateTaken?.let { date -> + put(TextType.DATE, SimpleDateFormat("yyyy.MM.dd", Locale.getDefault()).format(Date(date))) + put(TextType.TIME, SimpleDateFormat("HH:mm", Locale.getDefault()).format(Date(date))) + put( + TextType.DATETIME, + SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault()).format(Date(date)) + ) + } + } + } + } WatermarkEditSheet( - viewModel = viewModel, - onDismiss = { - viewModel.showWatermarkSheet = false + customProperties = editFrameCustomProperties, + onPropertiesChange = { viewModel.saveEditCustomProperties(it) }, + onDismiss = { viewModel.showWatermarkSheet = false }, + originalValues = originalValues, + onImportFont = { uri -> + viewModel.getCustomImportManager().importFont(uri) } ) } @@ -695,159 +716,6 @@ private fun FrameOption( } } -@OptIn(ExperimentalMaterial3Api::class) -@Composable -private fun WatermarkEditSheet( - viewModel: GalleryViewModel, - onDismiss: () -> Unit -) { - val sheetState = rememberModalBottomSheetState() - val customProperties = remember(viewModel.editFrameId) { - mutableStateMapOf().apply { - putAll(viewModel.editFrameCustomProperties.value) - } - } - - ModalBottomSheet( - onDismissRequest = { - onDismiss() - }, - sheetState = sheetState, - containerColor = Color(0xFF1A1A1A), - contentColor = Color.White, - scrimColor = Color.Transparent - ) { - Column( - modifier = Modifier - .fillMaxWidth() - .padding(16.dp) - .padding(bottom = 32.dp) - ) { - Text( - text = stringResource(R.string.watermark_adjustment), - fontSize = 18.sp, - fontWeight = FontWeight.Bold, - modifier = Modifier.padding(bottom = 16.dp) - ) - - // Logo 选择 - Text( - text = stringResource(R.string.logo), - fontSize = 14.sp, - color = Color.White.copy(alpha = 0.6f), - modifier = Modifier.padding(bottom = 8.dp) - ) - - val logos = listOf( - "none" to stringResource(R.string.none), - "apple" to "Apple", - "samsung" to "Samsung", - "xiaomi" to "Xiaomi", - "huawei" to "Huawei", - "honor" to "Honor", - "oppo" to "OPPO", - "vivo" to "Vivo", - "sony" to "Sony", - "canon" to "Canon", - "nikon" to "Nikon", - "fujifilm" to "Fujifilm", - "leica" to "Leica", - "hasselblad" to "Hasselblad", - "dji" to "DJI", - "panasonic" to "Panasonic", - "olympus" to "Olympus", - "pentax" to "Pentax", - "ricoh" to "Ricoh" - ) - - val currentPhoto = viewModel.getCurrentPhoto() - val currentBrand = currentPhoto?.metadata?.brand?.lowercase() ?: "" - val effectiveActiveId = - customProperties["LOGO"] ?: logos.find { it.first != "none" && currentBrand.contains(it.first) }?.first - ?: "none" - - LazyRow( - horizontalArrangement = Arrangement.spacedBy(8.dp), - modifier = Modifier.padding(bottom = 16.dp) - ) { - items(logos) { (id, name) -> - val isSelected = effectiveActiveId == id - Surface( - onClick = { - customProperties["LOGO"] = id - viewModel.saveEditCustomProperties(customProperties) - }, - color = if (isSelected) AccentOrange.copy(alpha = 0.2f) else Color.White.copy(alpha = 0.1f), - shape = RoundedCornerShape(8.dp), - border = if (isSelected) BorderStroke(1.dp, AccentOrange) else null - ) { - Text( - text = name, - modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp), - fontSize = 13.sp, - color = if (isSelected) AccentOrange else Color.White - ) - } - } - } - - // 文字编辑 - Text( - text = stringResource(R.string.text_content), - fontSize = 14.sp, - color = Color.White.copy(alpha = 0.6f), - modifier = Modifier.padding(bottom = 8.dp) - ) - - val textTypes = listOf( - TextType.DEVICE_MODEL to stringResource(R.string.device_model), - TextType.BRAND to stringResource(R.string.brand_name), - TextType.DATE to stringResource(R.string.date_label), - TextType.TIME to stringResource(R.string.time_label), - TextType.DATETIME to stringResource(R.string.datetime_label) - ) - - textTypes.forEach { (type, label) -> - val currentPhoto = viewModel.getCurrentPhoto() - val originalValue = when (type) { - TextType.DEVICE_MODEL -> currentPhoto?.metadata?.deviceModel - TextType.BRAND -> currentPhoto?.metadata?.brand - TextType.DATE -> currentPhoto?.metadata?.dateTaken?.let { - SimpleDateFormat("yyyy.MM.dd", Locale.getDefault()).format(Date(it)) - } - - TextType.TIME -> currentPhoto?.metadata?.dateTaken?.let { - SimpleDateFormat("HH:mm", Locale.getDefault()).format(Date(it)) - } - - TextType.DATETIME -> currentPhoto?.metadata?.dateTaken?.let { - SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault()).format(Date(it)) - } - - else -> null - } ?: "" - - OutlinedTextField( - value = customProperties[type.name] ?: originalValue, - onValueChange = { - customProperties[type.name] = it - viewModel.saveEditCustomProperties(customProperties) - }, - label = { Text(label) }, - modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp), - colors = OutlinedTextFieldDefaults.colors( - focusedBorderColor = AccentOrange, - unfocusedBorderColor = Color.White.copy(alpha = 0.2f), - focusedLabelColor = AccentOrange, - unfocusedLabelColor = Color.White.copy(alpha = 0.5f), - cursorColor = AccentOrange - ), - singleLine = true - ) - } - } - } -} /** * 用于编辑界面的可缩放图片组件 diff --git a/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt index 4c314e0c..bc5adfcf 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt @@ -32,6 +32,7 @@ import com.hinnka.mycamera.R import com.hinnka.mycamera.frame.FrameInfo import com.hinnka.mycamera.frame.TextType import com.hinnka.mycamera.ui.camera.autoRotate +import com.hinnka.mycamera.ui.common.WatermarkEditSheet import com.hinnka.mycamera.ui.theme.AccentOrange import com.hinnka.mycamera.viewmodel.CameraViewModel import kotlinx.coroutines.Dispatchers @@ -388,12 +389,28 @@ fun FrameManagementScreen( // 自定义属性编辑底部弹窗 if (showFrameEditSheet && editingFrameId != null) { - FrameEditBottomSheet( - viewModel = viewModel, - frameId = editingFrameId!!, + var properties by remember(editingFrameId) { mutableStateOf>(emptyMap()) } + LaunchedEffect(editingFrameId) { + properties = withContext(Dispatchers.IO) { + viewModel.getFrameCustomProperties(editingFrameId!!) + } + } + WatermarkEditSheet( + customProperties = properties, + onPropertiesChange = { + properties = it + scope.launch { + withContext(Dispatchers.IO) { + viewModel.saveFrameCustomProperties(editingFrameId!!, it) + } + } + }, onDismiss = { showFrameEditSheet = false editingFrameId = null + }, + onImportFont = { uri -> + viewModel.getCustomImportManager().importFont(uri) } ) } @@ -542,136 +559,3 @@ private fun FrameManagementItem( } } -/** - * 边框自定义属性编辑底部弹窗 - */ -@OptIn(ExperimentalMaterial3Api::class) -@Composable -private fun FrameEditBottomSheet( - viewModel: CameraViewModel, - frameId: String, - onDismiss: () -> Unit -) { - val scope = rememberCoroutineScope() - val sheetState = rememberModalBottomSheetState() - val customProperties = remember(frameId) { - mutableStateMapOf() - } - - // 加载已保存的自定义属性 - LaunchedEffect(frameId) { - val savedProperties = withContext(Dispatchers.IO) { - viewModel.getFrameCustomProperties(frameId) - } - customProperties.clear() - customProperties.putAll(savedProperties) - } - - ModalBottomSheet( - onDismissRequest = { - // 关闭时保存属性 - scope.launch { - withContext(Dispatchers.IO) { - viewModel.saveFrameCustomProperties(frameId, customProperties.toMap()) - } - } - onDismiss() - }, - sheetState = sheetState, - containerColor = Color(0xFF1A1A1A), - contentColor = Color.White, - scrimColor = Color.Transparent - ) { - Column( - modifier = Modifier - .fillMaxWidth() - .padding(16.dp) - .padding(bottom = 32.dp) - ) { - Text( - text = stringResource(R.string.watermark_adjustment), - fontSize = 18.sp, - fontWeight = FontWeight.Bold, - modifier = Modifier.padding(bottom = 16.dp) - ) - - // Logo 选择 - Text( - text = stringResource(R.string.logo), - fontSize = 14.sp, - color = Color.White.copy(alpha = 0.6f), - modifier = Modifier.padding(bottom = 8.dp) - ) - - val logos = listOf( - "none" to stringResource(R.string.none), - "apple" to "Apple", - "leica" to "Leica", - "hasselblad" to "Hasselblad", - "sony" to "Sony", - "canon" to "Canon", - "nikon" to "Nikon", - "fujifilm" to "Fujifilm", - "xiaomi" to "Xiaomi", - "huawei" to "Huawei", - "oppo" to "OPPO", - "vivo" to "Vivo" - ) - - val effectiveLogo = customProperties["LOGO"] ?: "none" - - LazyRow( - horizontalArrangement = Arrangement.spacedBy(8.dp), - modifier = Modifier.padding(bottom = 16.dp) - ) { - items(logos) { (id, name) -> - val isSelected = effectiveLogo == id - Surface( - onClick = { customProperties["LOGO"] = id }, - color = if (isSelected) AccentOrange.copy(alpha = 0.2f) else Color.White.copy(alpha = 0.1f), - shape = RoundedCornerShape(8.dp), - border = if (isSelected) BorderStroke(1.dp, AccentOrange) else null - ) { - Text( - text = name, - modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp), - fontSize = 13.sp, - color = if (isSelected) AccentOrange else Color.White - ) - } - } - } - - // 文字默认值编辑 - Text( - text = stringResource(R.string.text_content), - fontSize = 14.sp, - color = Color.White.copy(alpha = 0.6f), - modifier = Modifier.padding(bottom = 8.dp) - ) - - val textTypes = listOf( - TextType.DEVICE_MODEL to stringResource(R.string.device_model), - TextType.BRAND to stringResource(R.string.brand_name) - ) - - textTypes.forEach { (type, label) -> - OutlinedTextField( - value = customProperties[type.name] ?: "", - onValueChange = { customProperties[type.name] = it }, - label = { Text(label) }, - placeholder = { Text(stringResource(R.string.default_from_photo)) }, - modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp), - colors = OutlinedTextFieldDefaults.colors( - focusedBorderColor = AccentOrange, - unfocusedBorderColor = Color.White.copy(alpha = 0.2f), - focusedLabelColor = AccentOrange, - unfocusedLabelColor = Color.White.copy(alpha = 0.5f), - cursorColor = AccentOrange - ), - singleLine = true - ) - } - } - } -} diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 4d58bf13..a6731ee4 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -86,6 +86,9 @@ 时间 (TIME) 日期时间 (DATETIME) 默认读取照片信息 + 字体 + 默认 + 导入... 设置 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 909cf801..67778030 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -87,6 +87,9 @@ Time (TIME) Date/Time (DATETIME) Default from photo + Font + Default + Import... Settings