最近有個(gè)工作任務(wù)就是給用戶拍的照片打上水印并且上傳到服務(wù)器
首先我們看看效果
話不多說 開搞
首先準(zhǔn)備一張圖片
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.nice_bg)
//然后我們需要獲取圖片的寬高來做一張跟原始圖片一樣大小的bitmap
val width: Int = bitmap.width
val height: Int = bitmap.height
//創(chuàng)建一個(gè)跟原圖一樣大的Bitmap 但是這上面現(xiàn)在啥也沒有
val newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
圖片準(zhǔn)備好了接下來要輪到我們的Canvas來表演了
//直接吧空白的Bitmap作為畫布
val canvas = Canvas(newBitmap)
//把我們選中的圖片給畫上去吧
//在畫布 0裹纳,0坐標(biāo)上開始繪制原始圖片
canvas.drawBitmap(bitmap, 0f, 0f, null)
畫布和圖片準(zhǔn)備好了那么我們開始吧文字繪制上去吧因?yàn)橛行﹫D片偏亮 那么白字就不容易辨識(shí)了 所以我們來給文字下面加個(gè)70%透明度的黑色底色吧
//設(shè)置畫筆顏色為白色
val paint = Paint()
paint.color = Color.WHITE
//蒙版畫筆為黑色70%透明度 #4C000000
val maskingPaint = Paint()
maskingPaint.color = ContextCompat.getColor(this, R.color.black70)
//文字的大小和文本框間距 文字14sp 間距6dp
val textSize = sp2px(14).toFloat()
paint.textSize = textSize
val margin = dp2px(6f).toFloat()
//在計(jì)算一下文本框的寬度
val textWidth = msg.length * textSize
好了一切準(zhǔn)備就緒開始繪制吧 先來個(gè)左上角的(此處給三倍margin是因?yàn)槲淖猪敳康牧舭讍栴})
//首先是底色 這里left top都以左上角來開始所以都是0 right就是左右margin外加文字的長度了 這里文字的高度也就是文字的大小 所以bottom 為文字高度 外加三倍的margin
canvas.drawRect(0f, 0f, textWidth + margin * 2, textSize + margin * 3, maskingPaint)
//接著我們來繪制文字 沒什么好說的x軸起點(diǎn)位置給個(gè)margin y軸為文字高度加頂部Margin
canvas.drawText(msg, margin, textSize + margin, paint)
//最后就保存一下我們的圖片吧
canvas.save()
canvas.restore()
現(xiàn)在這個(gè)Bitmap就可以直接使用了將他設(shè)置到ImageView上就可以了
這里是dp和sp轉(zhuǎn)PX的方法 很基礎(chǔ)就不贅述了
private fun dp2px(dpValue: Float): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, resources.displayMetrics).toInt()
}
private fun sp2px(spValue: Int): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue.toFloat(), resources.displayMetrics).toInt()
}
因?yàn)樯婕暗綀D片的上傳那么我們還需要將bitmap轉(zhuǎn)成文件提交給服務(wù)器
private fun saveBitmapFile(context: Context, bitmap: Bitmap, name: String): File {
//將要保存圖片的路徑
val file = File(context.getExternalFilesDir(null)?.absolutePath + name + ".jpg")
try {
val bos = BufferedOutputStream(FileOutputStream(file))
//100意味著不壓縮如有需要可以0-100按比例進(jìn)行壓縮
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)
bos.flush()
bos.close()
} catch (e: IOException) {
e.printStackTrace()
}
return file
}
最后是我封裝的用來在各個(gè)位置加上文字水印有需要可以直接拿去使用
/**1:左上角 2:底部中間 3:右上角 4:左下角 5:右下角 6:正中間*/
private fun handlerWaterRemark(bitmap: Bitmap, msg: String, type: Int): Bitmap {
//原始圖片寬高
val width: Int = bitmap.width
val height: Int = bitmap.height
//創(chuàng)建一個(gè)跟原圖一樣大的Bitmap
val newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) // 創(chuàng)建一個(gè)新的和SRC長度寬度一樣的位圖
//將該圖片作為畫布
val canvas = Canvas(newBitmap)
//在畫布 0,0坐標(biāo)上開始繪制原始圖片
canvas.drawBitmap(bitmap, 0f, 0f, null)
//設(shè)置畫筆顏色
val paint = Paint()
paint.color = Color.WHITE
//底色
//畫筆
val maskingPaint = Paint()
maskingPaint.color = ContextCompat.getColor(this, R.color.black70)
//設(shè)置文字大小
val textSize = sp2px(14).toFloat()
paint.textSize = textSize
//文字的Margin
val margin = dp2px(6f).toFloat()
//文字總寬度
val textWidth = msg.length * textSize
//在畫布上繪制水印圖片
when (type) {
1 -> {
//左上角 X軸Y軸都加上6dp的margin
//底色
canvas.drawRect(0f, 0f, textWidth + margin * 2, textSize + margin*3 , maskingPaint)
canvas.drawText(msg, margin, textSize + margin, paint)
}
2 -> {
//底部中間
val x = (width / 2f) - (textWidth / 2)
val y = height.toFloat() - margin
//底色
canvas.drawRect(x - margin, y - margin * 3, x + textWidth + margin, height.toFloat(), maskingPaint)
canvas.drawText(msg, x, y, paint)
}
3 -> {
//右上角
//底色
canvas.drawRect(width - textWidth - margin * 3, 0f, width.toFloat(), textSize + margin * 3, maskingPaint)
canvas.drawText(msg, width - textWidth - margin, textSize + margin, paint)
}
4 -> {
//左下角
//底色
canvas.drawRect(0f, height-textSize-margin*2, textWidth+margin*2, height.toFloat(), maskingPaint)
canvas.drawText(msg, margin, height - margin, paint)
}
5 -> {
//右下角
//底色
canvas.drawRect(width-textWidth-margin*2, height-textSize-margin*2, width.toFloat(), height.toFloat(), maskingPaint)
canvas.drawText(msg, width - textWidth - margin, height - margin, paint)
}
6 -> {
//正中間
val x = width / 2f - textWidth / 2
val y = height / 2f - textSize / 2f
//底色
canvas.drawRect(x - margin, y - margin*3, x + textWidth + margin, y + textSize-margin, maskingPaint)
canvas.drawText(msg, x, y, paint)
}
}
//保存圖片
canvas.save()
canvas.restore()
return newBitmap
}
完結(jié)散花