更改图像java中每个像素的颜色

时间:2014-11-21 22:20:34

标签: java

我想将不同的像素更改为不同的颜色。基本上,将像素的一部分更改为透明。

for(int i = 0; i < image.getWidth();i++)
        for(int j = 0; j < image.getHeight(); j ++)
        {
            image.setRGB(i,j , 0);
        }

//我还将第三个参数0更改为另一个属性。但它仍然无效。它都显示黑色。你有什么想法吗?

阴。 谢谢

class ImagePanel extends JPanel {

    private BufferedImage image;

    public ImagePanel(int width, int height, BufferedImage image) {
        this.image = image;
        image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        repaint();
    }

    /**
     * Draws the image.
     */

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < image.getWidth(); i++) {
            for (int j = 0; j < image.getHeight(); j++) {
                image.setRGB(i, j, 0);
            }
        }
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

    }

}

4 个答案:

答案 0 :(得分:8)

第三个参数是32位的ARGB值。这是以位形式列出的:

AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBBB

请参阅BufferedImage.setRGB的javadoc(假设您使用BufferedImage,您的问题实际上并没有说明......)

  

将此BufferedImage中的像素设置为指定的RGB值。该   假设像素位于默认的RGB颜色模型TYPE_INT_ARGB中,   和默认的sRGB颜色空间。对于带有IndexColorModel的图像,   选择最近颜色的索引

  • 如果您使用支持透明度的图像类型,则设置alpha 255表示完全不透明,0表示完全透明非常重要。

您可以使用位移创建这样的值。

int alpha = 255; 
int red   = 0;
int green = 255;
int blue  = 0;

int argb = alpha << 24 + red << 16 + green << 8 + blue

image.setRGB(i, j, argb);

幸运的是java.awt.Color个实例上有一个getRGB()方法,所以你可以使用

image.setRGB(i, j, Color.green.getRGB());

这是一个完整的工作示例,也许您可​​以与您的代码进行比较:

public class StackOverflow27071351 {
    private static class ImagePanel extends JPanel {
        private BufferedImage image;
        public ImagePanel(int width, int height, BufferedImage image) {
            this.image = image;
            image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_ARGB);
            repaint();
        }
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int i = 0; i < image.getWidth(); i++) {
                for (int j = 0; j < image.getHeight(); j++) {
                    image.setRGB(i, j, new Color(255, 0, 0, 127).getRGB());
                }
            }
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        int width = 640;
        int height = 480;
        frame.setSize(width, height);
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(new ImagePanel(width, height, image));
        frame.setVisible(true);
    }
}

答案 1 :(得分:2)

嗯,第三个参数是RGB中的颜色,因此如果将其设置为0,它将为黑色。

答案 2 :(得分:1)

这是一个示例代码:

private int colorToRGB(int alpha, int red, int green, int blue) {
        int newPixel = 0;
        newPixel += alpha;
        newPixel = newPixel << 8;
        newPixel += red;
        newPixel = newPixel << 8;
        newPixel += green;
        newPixel = newPixel << 8;
        newPixel += blue;

        return newPixel;
    }

然后

image.setRGB(i, j, colorToRGB(alpha, 0, 0, 0))

答案 3 :(得分:1)

我使用以下表格:

int[] pixel = new int[4];

// the following four ints must range 0..255
pixel[0] = redValue;
pixel[1] = greenValue;
pixel[2] = bluleValue;
pixel[3] = alphaValue;

raster.setPixel(x, y, pixel);

要获取BufferedImage的栅格,我这样做:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster(); 

我已经完成了一些性能测试,并没有发现将所有颜色值的字节填充到一个数字中以产生很大的差异。

了解可以用alpha值绘制不透明图像(例如RGB而不是ARGB)的技术也很好。

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (yourAlpha)));
g2d.drawImage(...);
相关问题