Android位图保存没有透明区域 [英] Android Bitmap save without transparent area

查看:112
本文介绍了Android位图保存没有透明区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保存没有透明区域的位图.

I want to save bitmap without transparent area.

位图具有较大的透明像素.

Bitmap has large transparent pixel.

所以我想删除

我该怎么做?

我无法添加图片,所以请用符号解释.

I cant add picture so explain with symbols.

我不想裁剪功能. 我希望使用过滤器

I dont want to crop function. I hope use filter

┌──────────────┐

┌────────────────────────┐

│透明区域

│┌──────┐

│播种
└────────┘
────────────────┘

│ crop this
└────────┘
└────────────────────────┘

推荐答案

要查找位图的非透明区域,请在x和y中遍历位图,并找到非透明区域的最小值和最大值.然后将位图裁剪为这些坐标.

To find the non-transparent area of your bitmap, iterate across the bitmap in x and y and find the min and max of the non-transparent region. Then crop the bitmap to those co-ordinates.

Bitmap CropBitmapTransparency(Bitmap sourceBitmap)
{
    int minX = sourceBitmap.getWidth();
    int minY = sourceBitmap.getHeight();
    int maxX = -1;
    int maxY = -1;
    for(int y = 0; y < sourceBitmap.getHeight(); y++)
    {
        for(int x = 0; x < sourceBitmap.getWidth(); x++)
        {
            int alpha = (sourceBitmap.getPixel(x, y) >> 24) & 255;
            if(alpha > 0)   // pixel is not 100% transparent
            {
                if(x < minX)
                    minX = x;
                if(x > maxX)
                    maxX = x;
                if(y < minY)
                    minY = y;
                if(y > maxY)
                    maxY = y;
            }
        }
    }
    if((maxX < minX) || (maxY < minY))
        return null; // Bitmap is entirely transparent

    // crop bitmap to non-transparent area and return:
    return Bitmap.createBitmap(sourceBitmap, minX, minY, (maxX - minX) + 1, (maxY - minY) + 1);
}

这篇关于Android位图保存没有透明区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆