图片上面加水印

un ArrayList<CommonImg>?.getWaterMarkImgList(context: Context): ArrayList<CommonImg> {
    
    
    this?.forEach {
    
    
        val newPath =
            saveBitmap(
                context,
                drawTextToRightBottom(
                    context,
                    BitmapFactory.decodeFile(it.realPath),
                    System.currentTimeMillis().toDate().d2s(FORMAT_LONG) ?: "",
                    context.dp2px(8),
                    context.dp2px(8)
                ), it.realPath
            )
                ?: it.realPath
        it.url = newPath
        it.realPath = newPath
    }
    return this ?: arrayListOf()
}

/**
 * 绘制文字水印到右下角
 */
private fun drawTextToRightBottom(
    context: Context,
    bitmap: Bitmap,
    text: String,
    paddingRight: Int,
    paddingBottom: Int
): Bitmap {
    
    
    val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    paint.color = Color.parseColor("#B3FFFFFF")
    paint.setShadowLayer(8f, 0f, 0f, Color.parseColor("#99000000"))
    paint.textSize = context.dp2px(16f)
    val bounds = Rect()
    paint.getTextBounds(text, 0, text.length, bounds)
    return drawTextToBitmap(
        bitmap,
        text,
        paint,
        bitmap.width - bounds.width() - context.dp2px(paddingRight),
        bitmap.height - context.dp2px(paddingBottom)
    )
}

//图片上绘制文字
private fun drawTextToBitmap(
    bitmap: Bitmap, text: String,
    paint: Paint, paddingLeft: Int, paddingTop: Int
): Bitmap {
    
    
    var bitmap = bitmap
    var bitmapConfig = bitmap.config
    paint.isDither = true // 获取跟清晰的图像采样
    paint.isFilterBitmap = true // 过滤一些
    if (bitmapConfig == null) {
    
    
        bitmapConfig = Bitmap.Config.ARGB_8888
    }
    bitmap = bitmap.copy(bitmapConfig, true)
    val canvas = Canvas(bitmap)
    drawMultiLineText(text, paddingLeft.toFloat(), paddingTop.toFloat(), paint, canvas)
    return bitmap
}

private fun drawMultiLineText(
    str: String, x: Float, y: Float, paint: Paint,
    canvas: Canvas
) {
    
    
    val lines = str.split("\n".toRegex()).toTypedArray()
    var txtSize = -paint.ascent() + paint.descent()
    if (paint.style === Paint.Style.FILL_AND_STROKE
        || paint.style === Paint.Style.STROKE
    ) {
    
    
        txtSize += paint.strokeWidth // add stroke width to the text
    }
    val lineSpace = txtSize * 0.1f // default line spacing
    for (i in lines.indices) {
    
    
        canvas.drawText(lines[i], x, y + (txtSize + lineSpace) * i, paint)
    }
}

/**
 * 保存图片到本地
 */
private fun saveBitmap(context: Context, bitmap: Bitmap?, path: String): String? {
    
    
    if (bitmap == null) {
    
    
        return ""
    }
    val dir = File(FileUtils.getDir(context) + TakePhotoActivity.imgDirPath)
    if (!dir.exists()) {
    
    
        dir.mkdirs()
    }
    val cropFile =
        File(
            FileUtils.getDir(context) + TakePhotoActivity.imgDirPath,
            "img_${System.currentTimeMillis()}"
        )
    var out: FileOutputStream? = null
    //若本地存在旧照片,拍照前删除(兼容部分系统拍照不成功情况)
    if (cropFile.exists()) {
    
    
        cropFile.delete()
    }
    try {
    
    
        out = FileOutputStream(cropFile)
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
        out.flush()
        out.close()
    } catch (e: FileNotFoundException) {
    
    
        e.printStackTrace()
    } catch (e: IOException) {
    
    
        e.printStackTrace()
    } finally {
    
    
        if (out != null) {
    
    
            try {
    
    
                out.close()
                //删除旧照片
                val f = File(path)
                if (f.exists()) {
    
    
                    f.delete()
                }
            } catch (e: IOException) {
    
    
                e.printStackTrace()
            }
        }
    }
    return cropFile.absolutePath
}

猜你喜欢

转载自blog.csdn.net/u010194271/article/details/128001101