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

问题描述:

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

I wanna change different pixel to different color. Basically, change part of pixel to transparent.

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

//I也将第三个参数0更改为另一个属性.但它仍然无法正常工作.全部显示为黑色.你有什么想法吗?

//I aslo change the third parameter 0 to another attribute. but it still does not work. it all show black. do you have some ideas?

阴.谢谢

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

    }

}

第三个参数是32位的ARGB值.这是按位格式布置的:

The third parameter is ARGB value in 32 bits. This is laid out in bit form as:

AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBBB

有关Java文档,请参见

See the javadoc for BufferedImage.setRGB (assuming your using BufferedImage, your question doesn't actually say...)

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

Sets a pixel in this BufferedImage to the specified RGB value. The pixel is assumed to be in the default RGB color model, TYPE_INT_ARGB, and default sRGB color space. For images with an IndexColorModel, the index with the nearest color is chosen

  • 如果您使用的图像类型支持透明性,那么将alpha设置为255表示完全不透明,将0设置为完全透明很重要.
  • 您可以使用移位创建这样的值.

    You can create such a value using bit shifting.

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

幸运的是,

Luckily there is a getRGB() method on java.awt.Color instances, so you could use

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

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

Here's a full working example, perhaps you can compare to your code:

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