如何在 java SWT 中显示 PNG 图像?

How to display a PNG image in a java SWT?

我需要在 SWT Java window 中显示 PNG 图像,我正在使用 WindowBuilder 和 Eclipse。

首先我尝试使用标签和此代码:

Label lblNewLabel = new Label(this, SWT.NONE);
        lblNewLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
        Image image = new Image(display, "img/selae_mini.png");     
        lblNewLabel.setImage(image)

在eclipse中执行时有效,但是当我生成jar时,它不起作用。然后我在 Whosebug 上发现你必须使用 ClassLoader.getSystemClassLoader().getResourceAsStream 来获得 bufferedImage 然后你必须将那个 bufferedImage 转换为 ImageData,最后将其转换为 SWT Image上帝之母。所以我尝试了这段代码:

protected Image readImage(String path, Display display) {
    InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
    BufferedImage bi = null;
    try {
        bi = ImageIO.read(stream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return new Image(display, convertToSWT(bi));
}

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

现在的问题是我的图像是一个 png 文件,在执行 convertToSWT 方法的 IF 时,图像有一个名为 #pixelBits 的 ColorModel,所以它 returns 在该方法上为 null !我找不到任何关于如何解决这个问题的信息

我真的被这个简单的事情困住了。任何帮助将不胜感激。

我以前遇到过这个问题,我已经解决了。假设您的图像位于项目的资源文件夹中:

String yourImg = "sampleImg.png";
...
Label swtImg = new Label(composite, SWT.NONE);
swtImg.setImage(new Image(display, YourClassName.calss.getResourceAsStream(yourImg)));

对我有用! 祝你好运;)