Android Bitmap Saves Without Transparent Area

I want to save a bitmap without a transparent area.

The bitmap has a large transparent pixel.

So I want to delete this

How can i do this?

I can’t add a picture, so I explain it with symbols.

I do not want to use the crop function. I hope to use a filter

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚ transparent area

β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚ crop it
β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

+8
android bitmap crop
source share
3 answers

To find the opaque area of ​​your bitmap, iterate over the bitmap at x and y and find the min and max of the opaque area. Then draw a bitmap image at these coordinates.

Bitmap CropBitmapTransparency(Bitmap sourceBitmap) { int minX = sourceBitmap.getWidth(); int minY = sourceBitmap.getHeight(); int maxX = -1; int maxY = -1; for(int y = 0; y < sourceBitmap.getHeight(); y++) { for(int x = 0; x < sourceBitmap.getWidth(); x++) { int alpha = (sourceBitmap.getPixel(x, y) >> 24) & 255; if(alpha > 0) // pixel is not 100% transparent { if(x < minX) minX = x; if(x > maxX) maxX = x; if(y < minY) minY = y; if(y > maxY) maxY = y; } } } if((maxX < minX) || (maxY < minY)) return null; // Bitmap is entirely transparent // crop bitmap to non-transparent area and return: return Bitmap.createBitmap(sourceBitmap, minX, minY, (maxX - minX) + 1, (maxY - minY) + 1); } 
+16
source share

Trim transparent border with github .

 public Bitmap crop (Bitmap bitmap){ int height = bitmap.getHeight(); int width = bitmap.getWidth(); int[] empty = new int[width]; int[] buffer = new int[width]; Arrays.fill(empty,0); int top = 0; int left = 0; int botton = height; int right = width; for (int y = 0; y < height; y++) { bitmap.getPixels(buffer, 0, width, 0, y, width, 1); if (!Arrays.equals(empty, buffer)) { top = y; break; } } for (int y = height - 1; y > top; y--) { bitmap.getPixels(buffer, 0, width, 0, y, width, 1); if (!Arrays.equals(empty, buffer)) { botton = y; break; } } int bufferSize = botton -top +1; empty = new int[bufferSize]; buffer = new int[bufferSize]; Arrays.fill(empty,0); for (int x = 0; x < width; x++) { bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize); if (!Arrays.equals(empty, buffer)) { left = x; break; } } for (int x = width - 1; x > left; x--) { bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize); if (!Arrays.equals(empty, buffer)) { right = x; break; } } Bitmap cropedBitmap = Bitmap.createBitmap(bitmap, left, top, right-left, botton-top); return cropedBitmap; } 
+5
source share

@Alvaro Menezes responds as a function of Kotlin's extension

 fun Bitmap.trim(@ColorInt color: Int = Color.TRANSPARENT): Bitmap { var top = 0 var left = 0 var botton = height var right = width var colored = IntArray(width, , { color }) var buffer = IntArray(width) for (y in 0 until height) { getPixels(buffer, 0, width, 0, y, width, 1) if (!Arrays.equals(colored, buffer)) { top = y break } } for (y in height - 1 downTo top + 1) { getPixels(buffer, 0, width, 0, y, width, 1) if (!Arrays.equals(colored, buffer)) { botton = y break } } val bufferSize = botton - top + 1 colored = IntArray(bufferSize, { color }) buffer = IntArray(bufferSize) for (x in 0 until width) { getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize) if (!Arrays.equals(colored, buffer)) { left = x break } } for (x in width - 1 downTo left + 1) { getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize) if (!Arrays.equals(colored, buffer)) { right = x break } } return Bitmap.createBitmap(this, left, top, right - left, botton - top) } 
0
source share

All Articles