Android 給圖片打上水印

最近有個工作任務就是給用戶拍的照片打上水印并且上傳到服務器
首先我們看看效果


話不多說 開搞


首先準備一張圖片

 val bitmap = BitmapFactory.decodeResource(resources, R.drawable.nice_bg) 
//然后我們需要獲取圖片的寬高來做一張跟原始圖片一樣大小的bitmap
 val width: Int = bitmap.width
 val height: Int = bitmap.height
//創(chuàng)建一個跟原圖一樣大的Bitmap 但是這上面現(xiàn)在啥也沒有
val newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) 

圖片準備好了接下來要輪到我們的Canvas來表演了

//直接吧空白的Bitmap作為畫布
 val canvas = Canvas(newBitmap)
//把我們選中的圖片給畫上去吧
 //在畫布 0,0坐標上開始繪制原始圖片
canvas.drawBitmap(bitmap, 0f, 0f, null)

畫布和圖片準備好了那么我們開始吧文字繪制上去吧因為有些圖片偏亮 那么白字就不容易辨識了 所以我們來給文字下面加個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()
//在計算一下文本框的寬度
val textWidth = msg.length * textSize

好了一切準備就緒開始繪制吧 先來個左上角的(此處給三倍margin是因為文字頂部的留白問題)

//首先是底色 這里left  top都以左上角來開始所以都是0 right就是左右margin外加文字的長度了 這里文字的高度也就是文字的大小 所以bottom 為文字高度 外加三倍的margin 
 canvas.drawRect(0f, 0f, textWidth + margin * 2, textSize + margin * 3, maskingPaint)
//接著我們來繪制文字 沒什么好說的x軸起點位置給個margin y軸為文字高度加頂部Margin
canvas.drawText(msg, margin, textSize + margin, paint)
//最后就保存一下我們的圖片吧
 canvas.save()
 canvas.restore()
現(xiàn)在這個Bitmap就可以直接使用了將他設(shè)置到ImageView上就可以了
頂部留白導致下方只給一個margin很不協(xié)調(diào)

這里是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()
}

因為涉及到圖片的上傳那么我們還需要將bitmap轉(zhuǎn)成文件提交給服務器

 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按比例進行壓縮
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)
            bos.flush()
            bos.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return file
    }

最后是我封裝的用來在各個位置加上文字水印有需要可以直接拿去使用

/**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)建一個跟原圖一樣大的Bitmap
        val newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) // 創(chuàng)建一個新的和SRC長度寬度一樣的位圖
        //將該圖片作為畫布
        val canvas = Canvas(newBitmap)
        //在畫布 0,0坐標上開始繪制原始圖片
        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é)散花


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容