Changing the color of each pixel in a java image

I want to change different pixels to different colors. Basically, change the part of the 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 also change the third parameter 0 to another attribute. but it still does not work. it's all black. do you have any ideas

yin. thank

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
source share
4 answers

The third parameter is an ARGB value of 32 bits. This is laid out in the form of bits:

AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBBB

See javadoc for BufferedImage.setRGB (if you are using BufferedImage, your question doesn't really say ...)

BufferedImage RGB. , RGB , TYPE_INT_ARGB, sRGB . IndexColorModel

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

, getRGB() java.awt.Color,

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

, - RGB, , 0.

+2

:

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

I did some performance testing and did not find that overlaying all bytes of color values ​​into one number to make the most of the difference.

It's also good to know a technique where you can draw an opaque image (like RGB, not ARGB) with an alpha value.

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (yourAlpha)));
g2d.drawImage(...);
+1
source

Here is a sample code:

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

then

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

All Articles