【问题标题】:I am using .getRGB() and .setRGB() to get a section of a BufferedImage, how do I also copy transparency?我正在使用 .getRGB() 和 .setRGB() 来获取 BufferedImage 的一部分,我如何也复制透明度?
【发布时间】:2011-07-13 18:07:23
【问题描述】:

我正在使用以下代码“裁剪图像”,但是它忽略了透明度,因此从此方法获得的任何 BufferedImages 都是完全不透明的,并且似乎没有任何 .getARGB() 或 .setARGB()方法。我该如何解决这个问题?

        private static BufferedImage getCroppedImage(BufferedImage wholeImage, int xPos, int yPos, int width, int height)
        {
            GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
            BufferedImage croppedImage = null;

            try
                {
                    GraphicsDevice screen = graphEnv.getDefaultScreenDevice();
                    GraphicsConfiguration gc = screen.getDefaultConfiguration();
                    croppedImage = gc.createCompatibleImage(width, height, Transparency.BITMASK);
                }
            catch (Exception e)
                {
                    new errorWindow(e, "crop, in Images");
                }

            if (croppedImage == null)
                {
                    croppedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                }

            int[] pixels = new int[width * height];
            wholeImage.getRGB(xPos, yPos, width, height, pixels, 0, width);
            croppedImage.setRGB(0, 0, width, height, pixels, 0, width);

            return croppedImage;
        }

【问题讨论】:

  • 尝试改用Transparency.TRANSLUCENT
  • 没有区别,我需要一些方法来获取和设置每像素透明度的整数值,BufferedImage 的格式很好(.png)
  • SORRY @mre 是正确的,Transparency.TRANSLUCENT 是前进的方向,我只是在两个地方都没有改变它!

标签: java transparency bufferedimage


【解决方案1】:

请改用Transparency.TRANSLUCENT。这不会忽略 alpha 值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2012-12-17
    相关资源
    最近更新 更多