Bitmap handles changing the image to a transparent background and changing the main color

Picture first, practice with pictures

It is obvious that the former fingerprint covers the signature below, and the latter achieves the effect of pressing the fingerprint on the signature

Note : The test effect on Android7.0 is not good

Table of contents

foreword

1. What is Bitmap?

2. Use setPixel to modify the fingerprint background color

3. Use setPixel to modify the color of the fingerprint texture

Summarize


foreword

       The new requirement of the project is to generate a document at the back end based on the information used to fill in, and then sign and collect fingerprints on the terminal after filling in the information, and the fingerprints need to cover the previous name to achieve the effect of signing and pressing fingerprints.

        Think SO easy! , isn't it just that the two pictures overlap?

        However, the image of fingerprint extraction has a white background and needs to be changed to transparent. The fingerprint pattern is still black, and needs to be changed to red, which is difficult for a while, so I searched various Baidu.  

1. What is Bitmap?

       Bitmap ( Bitmap ), also known as raster graphics (English: Raster graphics ) or bitmap , is an image represented by a pixel array (Pixel-array/Dot-matrix lattice ) .

       It mainly uses the setPixel(int x, int y, int color) method, which is used to manipulate pixels. The meaning of these three parameters x – the x coordinate of the pixel to be replaced; y – the y coordinate of the pixel to be replaced; color – the ARGB color to be written to the bitmap

2. Use setPixel to modify the fingerprint background color

Suggestion: Work with solid background colors

The code example is as follows:

 /**
     * TODO 建议:适用于纯色背景颜色
     * 改变背景颜色
     * @param oldBitmap 原bm
     * @param config 配置的参数 如果修改后的背景还是有点瑕疵  可以尝试更改config值:ALPHA_8, RGB_565, ARGB_4444, ARGB_8888, RGBA_F16, HARDWARE
     * @param oldColor 需要替换的背景色
     */
    fun replaceBitmapColor(
        oldBitmap: Bitmap,
        config: Bitmap.Config = Bitmap.Config.ARGB_4444,
        oldColor: Int = Color.WHITE,
        newColor: Int = Color.TRANSPARENT
    ): Bitmap? {
        if (null == oldBitmap) return null
        val mBitmap = oldBitmap.copy(config, true)
//循环获得bitmap所有像素点
        val mBitmapWidth = mBitmap.width
        val mBitmapHeight = mBitmap.height
        for (i in 0 until mBitmapHeight) {
            for (j in 0 until mBitmapWidth) {
//获得Bitmap 图片中每一个点的color颜色值
//将需要填充的颜色值如果不是
//在这说明一下 如果color 是全透明 或者全黑 返回值为 0
//getPixel()不带透明通道 getPixel32()才带透明部分 以全透明是0x00000000
//而不透明黑色是0xFF000000 如果不计算透明部分就都是0了
                val color = mBitmap.getPixel(j, i)
                if (color == oldColor) {
                    mBitmap.setPixel(j, i, newColor) //将白色替换成透明色
                }
            }
        }
        oldBitmap.recycle()

        return mBitmap
    }

3. Use setPixel to modify the color of the fingerprint texture


    /**
     * 修改颜色
     */
    fun dealBackground(bm: Bitmap): Bitmap {
        for (i in 0 until bm.width) {
            for (j in 0 until bm.height) {
                val color = bm.getPixel(i, j)
//                获取像素点的RGB颜色值
                val R: Int = Color.red(color) ?: 0
                val G: Int = Color.green(color) ?: 0
                val B: Int = Color.blue(color) ?: 0
                "图片处理----$R--$G---$B".loge()
//                将接近白色的背景替换成为白色的背景,用于之后处理透明
//                将不是白色的背景替换为红色也就是指纹的纹路
                if (R > 240 && G > 240 && B > 240) {
                    val newColor: Int = Color.argb(0, 255, 255, 255)
                    bm.setPixel(i, j, newColor)
                } else {
                    val newColor: Int = Color.argb(0, 255, 0, 0)
                    bm.setPixel(i, j, newColor)
                }
            }
        }
        return bm
    }

full code


import android.graphics.Bitmap
import android.graphics.Color
import me.hgj.jetpackmvvm.ext.util.loge

/**
 * 改变颜色值、改变背景透明度
 */
object BitmapUtils {

    /**
     * TODO 建议:适用于纯色背景颜色
     * 改变背景颜色
     * @param oldBitmap 原bm
     * @param config 配置的参数 如果修改后的背景还是有点瑕疵  可以尝试更改config值:ALPHA_8, RGB_565, ARGB_4444, ARGB_8888, RGBA_F16, HARDWARE
     * @param oldColor 需要替换的背景色
     */
    fun replaceBitmapColor(
        oldBitmap: Bitmap,
        config: Bitmap.Config = Bitmap.Config.ARGB_4444,
        oldColor: Int = Color.WHITE,
        newColor: Int = Color.TRANSPARENT
    ): Bitmap? {
        if (null == oldBitmap) return null
        val mBitmap = oldBitmap.copy(config, true)
//循环获得bitmap所有像素点
        val mBitmapWidth = mBitmap.width
        val mBitmapHeight = mBitmap.height
        for (i in 0 until mBitmapHeight) {
            for (j in 0 until mBitmapWidth) {
//获得Bitmap 图片中每一个点的color颜色值
//将需要填充的颜色值如果不是
//在这说明一下 如果color 是全透明 或者全黑 返回值为 0
//getPixel()不带透明通道 getPixel32()才带透明部分 以全透明是0x00000000
//而不透明黑色是0xFF000000 如果不计算透明部分就都是0了
                val color = mBitmap.getPixel(j, i)
                if (color == oldColor) {
                    mBitmap.setPixel(j, i, newColor) //将白色替换成透明色
                }
            }
        }
        oldBitmap.recycle()

        return mBitmap
    }

    /**
     * 修改颜色
     */
    fun dealBackground(bm: Bitmap): Bitmap {
        for (i in 0 until bm.width) {
            for (j in 0 until bm.height) {
                val color = bm.getPixel(i, j)
//                获取像素点的RGB颜色值
                val R: Int = Color.red(color) ?: 0
                val G: Int = Color.green(color) ?: 0
                val B: Int = Color.blue(color) ?: 0
                "图片处理----$R--$G---$B".loge()
//                将接近白色的背景替换成为白色的背景,用于之后处理透明
//                将不是白色的背景替换为红色也就是指纹的纹路
                if (R > 240 && G > 240 && B > 240) {
                    val newColor: Int = Color.argb(0, 255, 255, 255)
                    bm.setPixel(i, j, newColor)
                } else {
                    val newColor: Int = Color.argb(0, 255, 0, 0)
                    bm.setPixel(i, j, newColor)
                }
            }
        }
        return bm
    } 
}


Summarize

 There are still many methods not found by Bitmap, and the setPixel method is mainly used.

Guess you like

Origin blog.csdn.net/weixin_41620505/article/details/129253874