2013-06-03 218 views
4

中從位圖中刪除透明像素在我的應用程序中,如果圖像不填充imageView,那麼會截圖,然後將透明像素添加到位圖中。可以從位圖中刪除透明像素或者不進行截圖透明像素。提前感謝。是否有可能在android

+0

您使用PNG? – Hasandroid

+0

查看內容= findViewById(R.id.imageView2); \t \t \t content.setDrawingCacheEnabled(true); 位圖位圖= content.getDrawingCache(); – user1083266

+0

我通過使用此代碼捕獲屏幕 – user1083266

回答

5

我知道這太晚了,但它可能對某人有幫助。我已經這樣做了,而且效果很好。

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; 

}

+0

什麼是完美的答案。 !謝謝親愛的! –

+0

完美!只有在使用這個之前,先考慮一下確保Bitmap的Config是否是'Bitmap.Config.ARGB_8888',如果是這樣的話,它就像一個魅力! –

-1

要修剪在Android上的圖像的裁切透明邊框你CA使用 這個安排。工作得更快,因爲不需要讀取所有像素,它只是切片位圖,更多的細節:CropTrimTransparentImage

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; 
} 
4

此方法是快了很多:

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.getHeight(); 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); 
} 
+1

謝謝你,你的回答救了我。但我不明白,爲什麼它更快? –

+1

我想我應該說它通常要快很多。首先從左到右掃描以找到最左邊不透明的像素。然後它從上到下進行掃描,但只查看與此X座標相等或更高的值。然後從底部和右側的像素做同樣的事情。這樣它不必掃描所有像素。 –

+2

另外,將位圖複製到數組並處理該數組比使用Bitmap.getPixel()要快得多, –