2012-08-07 38 views
0

我想通過使用位圖來顯示DICOM圖像的負片,我這樣做如何否定android中的位圖?

public static Bitmap getNegativeImage(Bitmap img) { 
      int w1 = img.getWidth(); 
      int h1 = img.getHeight(); 
      // int value[][] = new int[w1][h1]; 
      Bitmap gray = Bitmap.createBitmap(
        w1, h1, img.getConfig()); 
      int value, alpha, r, g, b; 
      for (int i = 0; i < w1; i++) { 
       for (int j = 0; j < h1; j++) { 
        value = img.getPixel(i, j); // store value 

        alpha = getAlpha(value); 
        r = 255 - getRed(value); 
        g = 255 - getGreen(value); 
        b = 255 - getBlue(value); 

        value = createRGB(alpha, r, g, b); 
        gray.setPixel(i, j, value); 
       } 
      } 
      return gray; 
     } 

     public static int createRGB(int alpha, int r, int g, int b) { 
      int rgb = (alpha << 24) + (r << 16) + (g << 8) + b; 
      return rgb; 
     } 

     public static int getAlpha(int rgb) { 
      return (rgb >> 24) & 0xFF; 
     } 

     public static int getRed(int rgb) { 
      return (rgb >> 16) & 0xFF; 
     } 

     public static int getGreen(int rgb) { 
      return (rgb >> 8) & 0xFF; 
     } 

     public static int getBlue(int rgb) { 
      return rgb & 0xFF; 
     } 

但反轉(負)圖像被黑掉,就再次單擊原來的形象出現,但倒象是不顯示。

問候 沙迪亞UM

回答

2

我不明白爲什麼你的代碼將無法正常工作,但這應該:

private static final int RGB_MASK = 0x00FFFFFF; 

public Bitmap invert(Bitmap original) { 
    // Create mutable Bitmap to invert, argument true makes it mutable 
    Bitmap inversion = original.copy(Config.ARGB_8888, true); 

    // Get info about Bitmap 
    int width = inversion.getWidth(); 
    int height = inversion.getHeight(); 
    int pixels = width * height; 

    // Get original pixels 
    int[] pixel = new int[pixels]; 
    inversion.getPixels(pixel, 0, width, 0, 0, width, height); 

    // Modify pixels 
    for (int i = 0; i < pixels; i++) 
     pixel[i] ^= RGB_MASK; 
    inversion.setPixels(pixel, 0, width, 0, 0, width, height); 

    // Return inverted Bitmap 
    return inversion; 
} 

它創建位圖的可變副本(不是所有的位圖是),反轉所有像素的rgb部分,使alpha保持完整

編輯 我有一個關於爲什麼你的代碼不工作的想法ing:你假設像素是AARRGGBB

+0

好吧讓我試試用 – Sathya 2012-08-07 10:51:22

+0

與之前一樣:( – Sathya 2012-08-07 10:56:43

+0

我假設像素爲ARGB_8888 – Sathya 2012-08-07 10:59:40