修改水印位置为底部中央
This commit is contained in:
@@ -99,41 +99,51 @@ object ImageProcessor {
|
||||
locationText: String,
|
||||
style: WatermarkStyle
|
||||
): Bitmap {
|
||||
android.util.Log.d("ImageProcessor", "addWatermark called, timeText=$timeText, locationText=$locationText")
|
||||
|
||||
val result = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true)
|
||||
val canvas = Canvas(result)
|
||||
|
||||
// 使用固定的字体大小,基于图片宽度比例
|
||||
val baseFontSize = result.width / 40f // 字体大小为图片宽度的1/40
|
||||
val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
textSize = style.fontSize * result.density
|
||||
textSize = baseFontSize
|
||||
color = style.textColor.toArgb()
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
textAlign = Paint.Align.CENTER
|
||||
}
|
||||
|
||||
val watermarkText = "$timeText $locationText"
|
||||
val textWidth = paint.measureText(watermarkText)
|
||||
val textHeight = paint.fontMetrics.let { it.descent - it.ascent }
|
||||
|
||||
// 计算位置(左下角)
|
||||
val padding = 20f * result.density
|
||||
val x = padding
|
||||
android.util.Log.d("ImageProcessor", "Watermark: width=$textWidth, height=$textHeight, text=$watermarkText")
|
||||
|
||||
// 计算位置(底部中央)
|
||||
val padding = result.width / 30f // 边距为图片宽度的1/30
|
||||
val x = result.width / 2f
|
||||
val y = result.height - padding
|
||||
|
||||
// 绘制背景
|
||||
// 通过ARGB判断来决定是否绘制背景,避免混用 Android Color 与 Compose Color 的类型问题
|
||||
val bgColorInt = style.backgroundColor.toArgb()
|
||||
android.util.Log.d("ImageProcessor", "Background color int: $bgColorInt")
|
||||
if (bgColorInt != 0) {
|
||||
val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
color = bgColorInt
|
||||
}
|
||||
val bgRect = RectF(
|
||||
x - 10,
|
||||
y - textHeight - 10,
|
||||
x + textWidth + 10,
|
||||
y + 10
|
||||
x - textWidth / 2 - padding / 2,
|
||||
y - textHeight - padding / 2,
|
||||
x + textWidth / 2 + padding / 2,
|
||||
y + padding / 2
|
||||
)
|
||||
canvas.drawRoundRect(bgRect, 8f, 8f, bgPaint)
|
||||
android.util.Log.d("ImageProcessor", "Background drawn at x=$x, y=$y")
|
||||
}
|
||||
|
||||
// 绘制文字
|
||||
canvas.drawText(watermarkText, x, y, paint)
|
||||
android.util.Log.d("ImageProcessor", "Text drawn at x=$x, y=$y")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user