Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to remove transparent pixels from bitmap in android

Tags:

android

bitmap

In my application i am taking screenshot if the image doesn't fill imageView then transparent pixels are added to bitmap.Is it possible to remove transparent pixels from bitmap or take screenshot without transparent pixels.Thanks in advance.

like image 743
user1083266 Avatar asked Jun 03 '13 11:06

user1083266


People also ask

How do I turn off transparency on Android?

Background Eraser Download the app from the Google Play Store. Open the app, choose your photo from your gallery by tapping the “Eraser” button. Then, remove the transparent background by using one of the remover tools. Once done editing, click the diskette like icon at the upper right corner to save your photo.

How do you clear a bitmap?

You can use eraseColor on bitmap to set its color to Transparent. It will useable again without recreating it.

Can pixels be transparent?

In general, it is not a question of a single pixel being transparent or not. From the outset, JPEG s do not support transparency, GIF and PNG can support transparency but they are not necessarily always transparent. So, assuming you have a PNG or a GIF , it could have a transparency layer.


1 Answers

This method is a lot faster:

static Bitmap trim(Bitmap source) {
    int firstX = 0, firstY = 0;
    int lastX = source.getWidth();
    int lastY = source.getHeight();
    int[] pixels = new int[source.getWidth() * source.getHeight()];
    source.getPixels(pixels, 0, source.getWidth(), 0, 0, source.getWidth(), source.getHeight());
    loop:
    for (int x = 0; x < source.getWidth(); x++) {
        for (int y = 0; y < source.getHeight(); y++) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                firstX = x;
                break loop;
            }
        }
    }
    loop:
    for (int y = 0; y < source.getHeight(); y++) {
        for (int x = firstX; x < source.getWidth(); x++) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                firstY = y;
                break loop;
            }
        }
    }
    loop:
    for (int x = source.getWidth() - 1; x >= firstX; x--) {
        for (int y = source.getHeight() - 1; y >= firstY; y--) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                lastX = x;
                break loop;
            }
        }
    }
    loop:
    for (int y = source.getHeight() - 1; y >= firstY; y--) {
        for (int x = source.getWidth() - 1; x >= firstX; x--) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                lastY = y;
                break loop;
            }
        }
    }
    return Bitmap.createBitmap(source, firstX, firstY, lastX - firstX, lastY - firstY);
}
like image 120
Will Poitras Avatar answered Sep 19 '22 23:09

Will Poitras