Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setRGB() in java

I am using setRGB() for changing the values of the pixel of an image.

int rgb=new Color(0,0,0).getRGB();
image1.setRGB(i,j,rgb); //where i,j is the boundaries of the image

Here,i am setting all the pixel values with white. But the change is not getting reflected in the image. Any One knows about the setRGB() how it works?

like image 761
Rohit Avatar asked Aug 14 '12 11:08

Rohit


People also ask

How do I change the RGB of an image in Java?

Setting the ARGB values −Create a Color object bypassing the new RGB values as parameters. Get the pixel value from the color object using the getRGB() method of the Color class. Set the new pixel value to the image by passing the x and y positions along with the new pixel value to the setRGB() method.

What is the use of BufferedImage in Java?

Java BufferedImage class is a subclass of Image class. It is used to handle and manipulate the image data. A BufferedImage is made of ColorModel of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).

What is BufferedImage in Java 2D?

The BufferedImage class is a cornerstone of the Java 2D immediate-mode imaging API. It manages the image in memory and provides methods for storing, interpreting, and obtaining pixel data.

What is a BufferedImage?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImages have an upper left corner coordinate of 0,0.


2 Answers

White is in RGB 255,255,255 so:

Color myWhite = new Color(255, 255, 255); // Color white
int rgb = myWhite.getRGB();

try {
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("bubbles.bmp"));
    }
    catch (IOException e) {
    }

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            img.setRGB(i, j, rgb);
        }
    }

    // retrieve image
    File outputfile = new File("saved.png");
    ImageIO.write(img, "png", outputfile);
}
catch (IOException e) {
}
like image 68
Lo Juego Avatar answered Oct 09 '22 15:10

Lo Juego


 Color col = new Color(newValue, newValue, newValue);
            image1.setRGB(i, j, col.getRGB());
like image 2
Lê Quang Duy Avatar answered Oct 09 '22 17:10

Lê Quang Duy