Arsmn1996 Arsmn1996 - 3 months ago 40
Android Question

How to remove transparency of bitmap in android?

I used this for freehand cropping and it works well but when it wants to save cropped bitmap, it has too much addition transparency.
How can I remove it or scale cropped bitmap?

Answer

This code gonna remove all transparent pixels in your bitmap:

    public static Bitmap createTrimmedBitmap(Bitmap bmp) {

int imgHeight = bmp.getHeight();
int imgWidth  = bmp.getWidth();
int smallX=0,largeX=imgWidth,smallY=0,largeY=imgHeight;
int left=imgWidth,right=imgWidth,top=imgHeight,bottom=imgHeight;
for(int i=0;i<imgWidth;i++)
{
    for(int j=0;j<imgHeight;j++)
    {
        if(bmp.getPixel(i, j) != Color.TRANSPARENT){
            if((i-smallX)<left){
                left=(i-smallX);
            }
            if((largeX-i)<right)
            {
                right=(largeX-i);
            }
            if((j-smallY)<top)
            {
                top=(j-smallY);
            }
            if((largeY-j)<bottom)
            {
                bottom=(largeY-j);
            }
        }
    }
}
Log.d(TAG, "left:" + left + " right:" + right + " top:" + top + " bottom:" + bottom);
bmp=Bitmap.createBitmap(bmp,left,top,imgWidth-left-right, imgHeight-top-bottom);

return bmp;

}