How can I change image color using JColorChooser in Java

93 Views Asked by At

how can I change image color using jcolorchooser like this in java
Image 1
Image 1
to
Image 2
Image 2

File input = new File("dprocessing.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();

for(int i=0; i<height; i++){
  for(int j=0; j<width; j++){
    Color c = new Color(image.getRGB(j, i));
    int red = (int)(c.getRed() * 0.299);
    int green = (int)(c.getGreen() * 0.587);
    int blue = (int)(c.getBlue() *0.114);
    Color newColor = new Color(red+green+blue,red+green+blue,red+green+blue);

    image.setRGB(j,i,newColor.getRGB());
  }
}

File ouptut = new File("new_Image.jpg");
ImageIO.write(image, "jpg", ouptut);

Any help will be appreciated...

1

There are 1 best solutions below

3
Jamal H On

You can solve this by changing it to this:

File input = new File("dprocessing.jpg");
     image = ImageIO.read(input);
     width = image.getWidth();
     height = image.getHeight();

     for(int i=0; i<height; i++){

        for(int j=0; j<width; j++){

           Color c = new Color(image.getRGB(j, i));
           int red = (int)(c.getRed() * 0.299);
           int green = (int)(c.getGreen() * 0.587);
           int blue = (int)(c.getBlue() *0.114);
           if(c.getRed()>200&&c.getGreen()>200&&c.getBlue()<100){ //this is to make sure that the pixel is some form of yellow. if it is yellow, change it to purple. if it isn't keep the color the same
                 Color newColor = new Color(red,green,blue); //before you were making every field the sum of all 3 colors, I don't know why
                 image.setRGB(j,i,newColor.getRGB());
           }
           else{
                 image.setRGB(j,i,c.getRGB());
           }

        }
     }

     File ouptut = new File("new_Image.jpg");
     ImageIO.write(image, "jpg", ouptut);

let me know if this works for you