2011-07-16 50 views
8

我有一組位圖。它們在一定程度上都是透明的,我不知道哪些部件是透明的。我想從排除透明部分的原始位圖中創建一個新的位圖,但在一個正方形中。我認爲這一形象解釋它:位圖轉換:創建從透明位圖排除透明邊的位圖

enter image description here

我知道如何創建位圖了現有的位圖,但我不知道如何找出哪一部分是透明的,以及如何用它來實現我的目標。

這就是我打算做這樣的:

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

有誰知道如何實現這一目標?

編輯:

我得到它的工作使用下面的算法來尋找左,右,上,下值:

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; 

    } 
} 
+0

喜,在我的應用我也想裁剪透明像素,請幫助我如何從位圖 – user1083266

回答

5

如果你要裁剪的圖片都或多或少在原始畫布的中心,我想你可能是這樣的:

  1. 從每個邊界開始工作,你的方式向內的圖像搜索非透明像素
  2. 一旦找到左上角的像素和右下角,就會得到您想要的目標。
  3. 複製,請你

現在的問題仍然是圖像就是你考慮一個透明像素。阿爾法透明度計數?如果是這樣的話,直到你決定它透明到足以從圖像中刪除多少阿爾法?

+0

除去透明像素到透明像素我的意思是用α0,完全透明的像素。我想裁剪的圖像並不總是在原始畫布的中心。他們甚至可能在右下角。這會影響你的答案嗎? – Emiam

+0

我會說從左上角搜索第一個,然後是最左側,最右側和底部。在給出的例子中,左上角和右下角會切斷相當多的圖像。 – Rob

6

我認爲這是一個比較有效的,它對於我來說

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

這對於Android 4 - 完美工作,已在3個設計上進行測試。感謝您的代碼,它幫助 –

0

的偉大工程,要找到自己的位圖的非透明區域,跨在x和y的位圖進行迭代,並找到最小值和最大值的不透明區域。然後裁剪位圖到這些座標。

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); 
} 
+0

忘記了學分? http://stackoverflow.com/a/27754016/649379 – SoftDesigner