将子图像转换为RGB-A

时间:2018-04-23 08:28:58

标签: java sprite transparency bufferedimage

我正在开发2D平台游戏,我有一张精灵表,其中包括瓷砖和积木的精灵。

我注意到透明精灵后面有一个粉红色的背景,所以我认为Java并没有将精灵加载为PNG而我试图在新的bufferedImage上重新绘制精灵,逐像素检查如果像素 R = 255 G = 63 B = 52 但不幸的是,代码无法检测到或者在这一点上,我没有更多的选择可以尝试。

我确保"粉红色"使用颜色选择器颜色值是正确的 enter image description here enter image description here

原始spritesheet(透明):
enter image description here

加载精灵的类是:

public class SpriteSheet {
    private BufferedImage image;

    public SpriteSheet(BufferedImage image) {
        this.image = image;
    }


    public BufferedImage grabImage(int col, int row, int width, int height) {
        BufferedImage alpha = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        BufferedImage img = image.getSubimage(
                (col * width) - width, 
                (row * height) - height, 
                width, 
                height);


        int w = img.getWidth();
        int h = img.getHeight();

        for(int y = 0; y < h; y++) {
            for(int x = 0; x < w; x++) {
                int pixel = img.getRGB(x, y);
                int red, green, blue;

                red         = (pixel >> 16)     & 0xff;
                green   = (pixel >> 8)  & 0xff;
                blue    = (pixel)       & 0xff;

                if(red == 255 && green == 63 && blue == 52)
                    alpha.setRGB(x, y, new Color(0, 0, 0, 0).getRGB());         
                else
                    alpha.setRGB(x, y, pixel);  


            }   
        }

        return alpha;
    }


}

加载精灵表的类是:

public class Texture {

    SpriteSheet bs, ss;
    private BufferedImage block_sheet = null;
    public BufferedImage[] block = new BufferedImage[3];


    public Texture() {

        BufferedImageLoader loader = new BufferedImageLoader();
        try {
            block_sheet = loader.loadImage("/tiles.png");
        } catch(Exception e) {
            e.printStackTrace();
        }

        bs = new SpriteSheet(block_sheet);

        getTextures();

    }


    private void getTextures() {
        block[0] = bs.grabImage(1, 1, 32, 32);
        block[1] = bs.grabImage(2, 1, 32, 32);
        block[2] = bs.grabImage(4, 1, 32, 32);
    }

}

如何摆脱粉红色的背景并保持透明度?

2 个答案:

答案 0 :(得分:0)

我不明白你为什么要使用subImage

try {
            BufferedImage img = ImageIO.read(new File("D:/image.png"));
            for (int i = 0; i < img.getWidth(); i++) {
                for (int j = 0; j < img.getHeight(); j++) {

                    Color pixelcolor = new Color(img.getRGB(i, j));
                    int r = pixelcolor.getRed();
                    int g = pixelcolor.getGreen();
                    int b = pixelcolor.getBlue();

                    if (r == 255 && g == 63 && b == 52) {
                        int rgb = new Color(255, 255, 255).getRGB();
                        img.setRGB(i, j, rgb);
                    }
                }
            }

            ImageIO.write(img, "png", new File("D:/transparent.png"));

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

答案 1 :(得分:0)

咳嗽 ,一直都有效,我忘了禁用代表块的测试块。一段时间后意识到这一点。

所以透明度工作正常。我刚刚看到我正在后面画的矩形。

相关问题