kotlin 拍照加水印
时间: 2023-12-04 20:04:08 浏览: 162
图片加水印
您可以使用Android中的Camera API来拍摄照片,然后使用Canvas在照片上绘制水印。以下是一个基本示例:
```kotlin
private fun addWatermarkToImage(bitmap: Bitmap, watermarkText: String): Bitmap {
val w = bitmap.width
val h = bitmap.height
val result = Bitmap.createBitmap(w, h, bitmap.config)
val canvas = Canvas(result)
canvas.drawBitmap(bitmap, 0f, 0f, null)
val paint = Paint()
paint.color = Color.WHITE
paint.textSize = 36f
paint.isAntiAlias = true
paint.isFakeBoldText = true
val bounds = Rect()
paint.getTextBounds(watermarkText, 0, watermarkText.length, bounds)
val x = w - bounds.width() - 20f
val y = h - bounds.height() - 20f
canvas.drawText(watermarkText, x, y, paint)
return result
}
private fun capturePhoto() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
val watermarkedBitmap = addWatermarkToImage(imageBitmap, "CSDN")
// do something with the watermarked bitmap
}
}
```
阅读全文