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

问题描述 投票:0回答:1

我需要在 SWT Java 窗口中显示 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 时,它不起作用。然后我在 Stack Overflow 上发现,您必须使用

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,所以它在该方法上返回null!我找不到任何有关如何解决这个问题的信息。

java eclipse swt bufferedimage windowbuilder
1个回答
3
投票

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

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

这对我有用! 祝你好运;)

© www.soinside.com 2019 - 2024. All rights reserved.