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?
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.
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).
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.
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.
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) {
}
Color col = new Color(newValue, newValue, newValue);
image1.setRGB(i, j, col.getRGB());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With