kotlin 照片加水印
时间: 2023-12-04 11:04:08 浏览: 38
可以使用 Kotlin 中的 `Bitmap` 和 `Canvas` 类来实现在照片上添加水印。
以下是一个简单的示例代码:
```kotlin
fun addWatermark(bitmap: Bitmap, watermarkText: String): Bitmap {
val bitmapConfig = bitmap.config ?: Bitmap.Config.ARGB_8888
val watermarkedBitmap = Bitmap.createBitmap(bitmap.width, bitmap.height, bitmapConfig)
val canvas = Canvas(watermarkedBitmap)
canvas.drawBitmap(bitmap, 0f, 0f, null)
val paint = Paint()
paint.color = Color.WHITE
paint.textSize = 30f
paint.isAntiAlias = true
val bounds = Rect()
paint.getTextBounds(watermarkText, 0, watermarkText.length, bounds)
val x = (bitmap.width - bounds.width()) / 2f
val y = (bitmap.height + bounds.height()) / 2f
canvas.drawText(watermarkText, x, y, paint)
return watermarkedBitmap
}
```
调用 `addWatermark` 函数,并传入要添加水印的照片和水印文本,即可返回添加了水印的照片。