Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap conversion: Creating bitmap that excludes transparent sides from transparent bitmap

I have a set of bitmaps. They are all transparent to some extent, and I don't know in advance which parts are transparent. I would like to create a new bitmap out of the original bitmap that excludes the transparent parts, but in a square. I think this image explains it:

enter image description here

I know how to create a bitmap out of a existing bitmap, but I don't know how to find out which part is transparent and how to use that to achieve my goal.

This is how I plan on doing this:

public Bitmap cutImage(Bitmap image) {
        Bitmap newBitmap = null; 

        int width = image.getWidth(); 
        int height = image.getHeight(); 

        newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

        Canvas canvas = new Canvas(newBitmap); 

        //This is where I need to find out correct values of r1 and r1.

        Rect r1 = new Rect(?, ?, ?, ?);
        Rect r2 = new Rect(?, ?, ?, ?);

        canvas.drawBitmap(image, r1, r2, null);

        return newBitmap; 
     }

Does anyone know how to achieve this?

EDIT:

I got it work using the following algorithm to find left, right, top and bottom values:

private int x1;
private int x2;
private int y1;
private int y2;

private void findRectValues(Bitmap image)
{
    for(int x = 0; x < image.getWidth(); x++)
    {
        for(int y = 0; y < image.getHeight(); y++)
        {
            if(image.getPixel(x, y) != Color.TRANSPARENT)
            {
                System.out.println("X1 is: " + x);
                x1 = x;
                break;
            }
        }

        if(x1 != 0)
            break;

    }

    for(int x = image.getWidth()-1; x > 0; x--)
    {
        for(int y = 0; y < image.getHeight(); y++)
        {
            if(image.getPixel(x, y) != Color.TRANSPARENT)
            {
                System.out.println("X2 is: " + x);
                x2 = x;
                break;
            }
        }

        if(x2 != 0)
            break;

    }

    for(int y = 0; y < image.getHeight(); y++)
    {
        for(int x = 0; x < image.getWidth(); x++)
        {
            if(image.getPixel(x, y) != Color.TRANSPARENT)
            {
                System.out.println("Y1 is: " + y);
                y1 = y;
                break;
            }
        }

        if(y1 != 0)
            break;

    }

    for(int y = image.getHeight()-1; y > 0; y--)
    {
        for(int x = 0; x < image.getWidth(); x++)
        {
            if(image.getPixel(x, y) != Color.TRANSPARENT)
            {
                System.out.println("Y2 is: " + y);
                y2 = y;
                break;
            }
        }

        if(y2 != 0)
            break;

    }
}
like image 322
Emir Kuljanin Avatar asked Jul 16 '11 12:07

Emir Kuljanin


1 Answers

i think this is a bit more efficient and it works great for me

public Bitmap cropBitmapToBoundingBox(Bitmap picToCrop, int unusedSpaceColor) {
    int[] pixels = new int[picToCrop.getHeight() * picToCrop.getWidth()];
    int marginTop = 0, marginBottom = 0, marginLeft = 0, marginRight = 0, i;
    picToCrop.getPixels(pixels, 0, picToCrop.getWidth(), 0, 0,
            picToCrop.getWidth(), picToCrop.getHeight());

    for (i = 0; i < pixels.length; i++) {
        if (pixels[i] != unusedSpaceColor) {
            marginTop = i / picToCrop.getWidth();
            break;
        }
    }

    outerLoop1: for (i = 0; i < picToCrop.getWidth(); i++) {
        for (int j = i; j < pixels.length; j += picToCrop.getWidth()) {
            if (pixels[j] != unusedSpaceColor) {
                marginLeft = j % picToCrop.getWidth();
                break outerLoop1;
            }
        }
    }

    for (i = pixels.length - 1; i >= 0; i--) {
        if (pixels[i] != unusedSpaceColor) {
            marginBottom = (pixels.length - i) / picToCrop.getWidth();
            break;
        }
    }

    outerLoop2: for (i = pixels.length - 1; i >= 0; i--) {
        for (int j = i; j >= 0; j -= picToCrop.getWidth()) {
            if (pixels[j] != unusedSpaceColor) {
                marginRight = picToCrop.getWidth()
                        - (j % picToCrop.getWidth());
                break outerLoop2;
            }
        }
    }

    return Bitmap.createBitmap(picToCrop, marginLeft, marginTop,
            picToCrop.getWidth() - marginLeft - marginRight,
            picToCrop.getHeight() - marginTop - marginBottom);
}
like image 84
grubFX Avatar answered Nov 09 '22 03:11

grubFX