现在的位置: 首页 > 综合 > 正文

Swing图片ImageIcon对象到SWT图片Image对象的转换

2013年01月06日 ⁄ 综合 ⁄ 共 3129字 ⁄ 字号 评论关闭

 本文转载自:http://bbs.chinaunix.net/viewthread.php?tid=871416


到这个标题也许会觉得很奇怪,有这个必要吗?
   
答案是肯定的,说一种情况,比如代码复用,如果我曾经的项目中有一段程序是生成一个swing图片的,但是现在的界面要用swt实现了,我是不是应该将生
成swing图片的代码改成生成swt图片,如果时间允许,无可厚非,但是很多情况下不仅是时间不允许,更有可能只能生成swing图片。
 
 
下面转入正题,至于如何使用一段很长的代码产生一个swing图片就不说了,这里为了举例而举例,直接读入一个swing图片而后转换成swt图片,重要
的是转换过程。

import java.awt.Graphics;
import
java.awt.image.BufferedImage;
import
java.awt.image.DirectColorModel;
import
java.awt.image.ImageObserver;
import java.awt.image.WritableRaster;
import javax.swing.ImageIcon;
import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Color;
import
org.eclipse.swt.graphics.Image;
import
org.eclipse.swt.graphics.ImageData;
import
org.eclipse.swt.graphics.PaletteData;
import
org.eclipse.swt.graphics.RGB;
import
org.eclipse.swt.layout.FillLayout;
import
org.eclipse.swt.widgets.Display;
import
org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
*
* Change javax.swing.ImageIcon object to
org.eclipse.swt.graphics.Image object.
*
* @author Freshwind
*/
public class ImageConvertor {
    public ImageConvertor() {
        super();
    }
   
    /**
     * change
ImageIcon to BufferedImage
     * @param icon
     * @return
     */
    public static BufferedImage getBufferedImage(ImageIcon
icon){
        int width = icon.getIconWidth();
        int
height = icon.getIconHeight();
        ImageObserver observer =
icon.getImageObserver();
        BufferedImage bufferedImage = new
BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    
   Graphics gc = bufferedImage.createGraphics();
    
   gc.drawImage(icon.getImage(), 0, 0, observer);
        return
bufferedImage;
    }
   
    /**
     * change
BufferedImage to ImageData
     * @param bufferedImage
     *
@return
     */
    public static ImageData
getImageData(BufferedImage bufferedImage){
        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;
    }
   
    public static void main(String[] args){
        ImageIcon icon =
new ImageIcon("C://temp//freshwind.jpg");
        BufferedImage
bufferedImage = getBufferedImage(icon);
        ImageData data =
getImageData(bufferedImage);
        
        Display display =
new Display();
        Shell shell = new Shell(display);
    
   shell.setText("Image Convertor by Freshwind");
    
   shell.setLayout(new FillLayout());
    
   shell.setBackground(new Color(display, new RGB(255, 255, 255)));
 
      shell.setSize(350, 100);
        
        Label label =
new Label(shell, SWT.NONE);
        //new Image with ImageData which
is generated previously.
        Image image = new Image(display,
data);
        label.setImage(image);
        shell.open();
 
      while (!shell.isDisposed()) {
            if
(!display.readAndDispatch())
                display.sleep();
 
      }
        display.dispose();
    }
}

抱歉!评论已关闭.