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?
Asked
Active
Viewed 1,282 times
1 Answers
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
-
Thanks! it worked very well – arsmn Aug 30 '16 at 18:32
-
very much slow process.. make it fast – Ahamadullah Saikat Mar 13 '17 at 19:59