-3

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?

Community
  • 1
  • 1
arsmn
  • 451
  • 2
  • 7
  • 12

1 Answers1

0

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;

}
LychmanIT
  • 695
  • 6
  • 13