Android Bitmap save without transparent area

前端 未结 4 1931
暖寄归人
暖寄归人 2021-02-05 19:24

I want to save bitmap without transparent area.

Bitmap has large transparent pixel.

So i want to remove that

How can i do this?

I cant add pictur

4条回答
  •  遇见更好的自我
    2021-02-05 19:39

    I took @Alvaro Menezes's answer and improved it as a Kotlin extension function. I tweaked it a bit, changed some variable names for better readability and it adds more fixes to the issue mentioned by @Ahamadullah Saikat that throws an IllegalArgumentException

    Note that reading pixels by line improve a lot the performances against reading this independently as the accepted answer suggest.

    /**
     * Trims a bitmap borders of a given color.
     *
     */
    fun Bitmap.trim(@ColorInt color: Int = Color.TRANSPARENT): Bitmap {
    
        var top = height
        var bottom = 0
        var right = width
        var left = 0
    
        var colored = IntArray(width, { color })
        var buffer = IntArray(width)
    
        for (y in bottom until top) {
            getPixels(buffer, 0, width, 0, y, width, 1)
            if (!Arrays.equals(colored, buffer)) {
                bottom = y
                break
            }
        }
    
        for (y in top - 1 downTo bottom) {
            getPixels(buffer, 0, width, 0, y, width, 1)
            if (!Arrays.equals(colored, buffer)) {
                top = y
                break
            }
        }
    
        val heightRemaining = top - bottom
        colored = IntArray(heightRemaining, { color })
        buffer = IntArray(heightRemaining)
    
        for (x in left until right) {
            getPixels(buffer, 0, 1, x, bottom, 1, heightRemaining)
            if (!Arrays.equals(colored, buffer)) {
                left = x
                break
            }
        }
    
        for (x in right - 1 downTo left) {
            getPixels(buffer, 0, 1, x, bottom, 1, heightRemaining)
            if (!Arrays.equals(colored, buffer)) {
                right = x
                break
            }
        }
        return Bitmap.createBitmap(this, left, bottom, right - left, top - bottom)
    }
    

提交回复
热议问题