mizzo
mizzo

Reputation: 51

Setting the new pixel values

I have the following code to get the pixel values of RGB

BufferedImage bi=ImageIO.read(new File("C:/Users/MyDell/workspace/Chaotic/bear.jpg"));      
int[] pixel;
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
    pixel = bi.getRaster().getPixel(x, y, new int[3]);
    System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - "(bi.getWidth() * y + x));      
      }
}

How can i change the pixel values of RGB to new values?

Upvotes: 1

Views: 304

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50687

You can set individual pixels on a BufferedImage img as follows:

int r = // red component 0...255
int g = // green component 0...255
int b = // blue component 0...255
int col = (r << 16) | (g << 8) | b;
img.setRGB(x, y, col);

Check out here for more info.

Upvotes: 1

Related Questions