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

 本文转载自: 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();
    }
}

更多原创访问我的blog
http://freshwind.cublog.cn/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值