java change image color

Solutions on MaxInterview for java change image color by the best coders in the world

showing results for - "java change image color"
Emanuele
31 Jul 2019
1import java.awt.image.BufferedImage;
2import java.awt.image.WritableRaster;
3import java.io.File;
4// w w w  . jav  a 2s  . com
5import javax.imageio.ImageIO;
6
7public class Main {
8
9  public static void main(String[] args) throws Exception {
10    BufferedImage img = colorImage(ImageIO.read(new File("NWvnS.png")));
11    ImageIO.write(img, "png", new File("Test.png"));
12  }
13
14  private static BufferedImage colorImage(BufferedImage image) {
15    int width = image.getWidth();
16    int height = image.getHeight();
17    WritableRaster raster = image.getRaster();
18
19    for (int xx = 0; xx < width; xx++) {
20      for (int yy = 0; yy < height; yy++) {
21        int[] pixels = raster.getPixel(xx, yy, (int[]) null);
22        pixels[0] = 0;
23        pixels[1] = 255;
24        pixels[2] = 255;
25        raster.setPixel(xx, yy, pixels);
26      }
27    }
28    return image;
29  }
30}
31