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.
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.
You can use eraseColor on bitmap to set its color to Transparent. It will useable again without recreating it.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With