2011-06-27 45 views
7

經過多次尋找,我發現一些代碼,一個BufferedImage轉換爲SWT圖像(不打擾閱讀它尚未):轉換爲SWT圖像

public static ImageData convertToSWT(BufferedImage bufferedImage) { 
    if (bufferedImage.getColorModel() instanceof DirectColorModel) { 
     DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel(); 
     PaletteData palette = new PaletteData(
      colorModel.getRedMask(), 
      colorModel.getGreenMask(), 
      colorModel.getBlueMask() 
     ); 
     ImageData data = new ImageData(
      bufferedImage.getWidth(), 
      bufferedImage.getHeight(), colorModel.getPixelSize(), 
      palette 
     ); 
     WritableRaster raster = bufferedImage.getRaster(); 
     int[] pixelArray = new int[3]; 
     for (int y = 0; y < data.height; y++) { 
      for (int x = 0; x < data.width; x++) { 
       raster.getPixel(x, y, pixelArray); 
       int pixel = palette.getPixel(
        new RGB(pixelArray[0], pixelArray[1], pixelArray[2]) 
       ); 
       data.setPixel(x, y, pixel); 
      } 
     } 
     return data; 
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) { 
     IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel(); 
     int size = colorModel.getMapSize(); 
     byte[] reds = new byte[size]; 
     byte[] greens = new byte[size]; 
     byte[] blues = new byte[size]; 
     colorModel.getReds(reds); 
     colorModel.getGreens(greens); 
     colorModel.getBlues(blues); 
     RGB[] rgbs = new RGB[size]; 
     for (int i = 0; i < rgbs.length; i++) { 
      rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); 
     } 
     PaletteData palette = new PaletteData(rgbs); 
     ImageData data = new ImageData(
      bufferedImage.getWidth(), 
      bufferedImage.getHeight(), 
      colorModel.getPixelSize(), 
      palette 
     ); 
     data.transparentPixel = colorModel.getTransparentPixel(); 
     WritableRaster raster = bufferedImage.getRaster(); 
     int[] pixelArray = new int[1]; 
     for (int y = 0; y < data.height; y++) { 
      for (int x = 0; x < data.width; x++) { 
       raster.getPixel(x, y, pixelArray); 
       data.setPixel(x, y, pixelArray[0]); 
      } 
     } 
     return data; 
    } 
    return null; 
} 

(這裏找到:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/ConvertsabufferedimagetoSWTImageData.htm) 。

我測試過了,它工作得很好。問題是我不理解它(我最好的猜測是它使用兩者的原始數據接口進行傳輸)。我想到一個更簡單的解決方案是將BufferedImage輸出到ByteArrayOutputStream,然後將其讀回到帶有ByteArrayInputStream的SWT圖像中。這個解決方案有什麼問題嗎?速度怎麼樣?

此轉換是必要的,因爲所有的圖像大小調整庫都用於AWT,但我用SWT顯示圖像。

謝謝!

回答

6

代碼的複雜性主要是由於BufferedImage兩種可能的顏色模型。我不認爲你可以在這方面取得很大進步。首先,使用中間的Stream將需要兩個圖像系統具有通用格式,並且從Stream轉換到/來自Stream的轉換肯定會比當前代碼慢。

+0

謝謝,這完全回答我的問題。 – Jonah

5

這是一個更完整的版本...在這個問題中發佈的一個不適合我。

/** 
* snippet 156: convert between SWT Image and AWT BufferedImage. 
* <p> 
* For a list of all SWT example snippets see 
* http://www.eclipse.org/swt/snippets/ 
*/ 
public static ImageData convertToSWT(BufferedImage bufferedImage) { 
    if (bufferedImage.getColorModel() instanceof DirectColorModel) { 
     /* 
     DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel(); 
     PaletteData palette = new PaletteData(
       colorModel.getRedMask(), 
       colorModel.getGreenMask(), 
       colorModel.getBlueMask()); 
     ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), 
       colorModel.getPixelSize(), palette); 
     WritableRaster raster = bufferedImage.getRaster(); 
     int[] pixelArray = new int[3]; 
     for (int y = 0; y < data.height; y++) { 
      for (int x = 0; x < data.width; x++) { 
       raster.getPixel(x, y, pixelArray); 
       int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2])); 
       data.setPixel(x, y, pixel); 
      } 
     } 
     */ 
     DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel(); 
     PaletteData palette = new PaletteData(
       colorModel.getRedMask(), 
       colorModel.getGreenMask(), 
       colorModel.getBlueMask()); 
     ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), 
       colorModel.getPixelSize(), palette); 
     for (int y = 0; y < data.height; y++) { 
      for (int x = 0; x < data.width; x++) { 
       int rgb = bufferedImage.getRGB(x, y); 
       int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); 
       data.setPixel(x, y, pixel); 
       if (colorModel.hasAlpha()) { 
        data.setAlpha(x, y, (rgb >> 24) & 0xFF); 
       } 
      } 
     } 
     return data; 
    } 
    else if (bufferedImage.getColorModel() instanceof IndexColorModel) { 
     IndexColorModel colorModel = (IndexColorModel)bufferedImage.getColorModel(); 
     int size = colorModel.getMapSize(); 
     byte[] reds = new byte[size]; 
     byte[] greens = new byte[size]; 
     byte[] blues = new byte[size]; 
     colorModel.getReds(reds); 
     colorModel.getGreens(greens); 
     colorModel.getBlues(blues); 
     RGB[] rgbs = new RGB[size]; 
     for (int i = 0; i < rgbs.length; i++) { 
      rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); 
     } 
     PaletteData palette = new PaletteData(rgbs); 
     ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), 
       colorModel.getPixelSize(), palette); 
     data.transparentPixel = colorModel.getTransparentPixel(); 
     WritableRaster raster = bufferedImage.getRaster(); 
     int[] pixelArray = new int[1]; 
     for (int y = 0; y < data.height; y++) { 
      for (int x = 0; x < data.width; x++) { 
       raster.getPixel(x, y, pixelArray); 
       data.setPixel(x, y, pixelArray[0]); 
      } 
     } 
     return data; 
    } 
    else if (bufferedImage.getColorModel() instanceof ComponentColorModel) { 
     ComponentColorModel colorModel = (ComponentColorModel)bufferedImage.getColorModel(); 
     //ASSUMES: 3 BYTE BGR IMAGE TYPE 
     PaletteData palette = new PaletteData(0x0000FF, 0x00FF00,0xFF0000); 
     ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), 
       colorModel.getPixelSize(), palette); 
     //This is valid because we are using a 3-byte Data model with no transparent pixels 
     data.transparentPixel = -1; 
     WritableRaster raster = bufferedImage.getRaster(); 
     int[] pixelArray = new int[3]; 
     for (int y = 0; y < data.height; y++) { 
      for (int x = 0; x < data.width; x++) { 
       raster.getPixel(x, y, pixelArray); 
       int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2])); 
       data.setPixel(x, y, pixel); 
      } 
     } 
     return data; 
    } 
    return null; 
} 
+0

好奇心,你從哪裏得到這個完整的版本? – Galabyca

+1

http://www.eclipse.org/swt/snippets – marcolopes