图片上面加水印

Posted 黄毛火烧雪下

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片上面加水印相关的知识,希望对你有一定的参考价值。

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

以上是关于图片上面加水印的主要内容,如果未能解决你的问题,请参考以下文章

图片上面加水印

php --图片加图片水印

如何在word中给图片加水印(怎样用Word给图片加水印)

Thumbnails为图片加水印

在自己的照片上加水印 logo用啥软件?

在自己的照片上加水印 logo用啥软件?