简体   繁体   English

如何在Java中使用JColorChooser更改图像颜色

[英]How can I change image color using JColorChooser in Java

how can I change image color using jcolorchooser like this in java 我如何在Java中使用jcolorchooser更改图像颜色
Image 1 图片1
图片1
to
Image 2 图片2
图片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... 任何帮助将不胜感激...

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 让我知道这是否适合您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM