【问题标题】:Converting subimage to RGB-A将子图像转换为 RGB-A
【发布时间】:2018-04-23 08:28:57
【问题描述】:

我正在开发一个 2D 平台游戏,我有一个包含图块和块的精灵的精灵表。

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

我使用颜色选择器确保“粉红色”颜色值正确。

原始精灵表(透明):

加载精灵的类是:

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

}

如何去除粉红色的背景并保持透明度?

【问题讨论】:

  • 我认为发生了什么,您正在将图像加载为不支持 Alpha 通道的格式。问题将出现在您最初加载图像的位置。那么你可以将代码发布到你的图像加载器

标签: java sprite transparency bufferedimage


【解决方案1】:

我不明白你为什么使用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());
        }

【讨论】:

  • 我使用 subimage 的原因是因为我想获得 spritesheet 的那一部分,就像你在 css 文件中所做的那样。 getSubimage(int x, int y, int w, int h) Returns a subimage defined by a specified rectangular region.
  • 我在没有它的情况下尝试将此代码添加到您的图像中,它有效
  • 是您的代码,正在创建一个新的图像文件吗?如果是这样,那不是我要找的。我想直接从精灵表加载它。可能会通过某种过程推动它以保存透明度。
  • 是的,我刚刚创建了新图像以检查代码是否有效
【解决方案2】:

,它一直有效,我忘记禁用代表块的测试块。过了一段时间才意识到这一点。

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

【讨论】:

  • 很高兴您找到了解决方案!但是,我建议您删除整个问题,因为它并不能真正帮助其他有同样问题的人(问题中甚至没有错误代码)。
猜你喜欢
  • 2019-03-16
  • 2021-12-17
  • 2010-12-14
  • 1970-01-01
  • 2017-08-02
  • 2021-12-15
  • 2014-02-26
  • 2014-12-22
相关资源
最近更新 更多