简体   繁体   English

将图像红色文本更改为黑色,将黑色背景更改为白色

[英]Changing Image red text to black and black background to white

I'm trying to edit the attached image by making the red image text black and the black background white.我正在尝试通过将红色图像文本设置为黑色并将黑色背景设置为白色来编辑附加图像。

I have attached the below code trying to achieve the results but the new image is coming out with white text and black background.我附上了下面的代码,试图实现结果,但新图像以白色文本和黑色背景出现。

Please assist!请协助!

IMAGE:图片: 好形象

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class BlackAndWhiteTest {

  public static void main(String[] args) throws IOException {

    File file = new File("C://picw//screenshot.png");
    BufferedImage orginalImage = ImageIO.read(file);

    BufferedImage blackAndWhiteImg = new BufferedImage(
        orginalImage.getWidth(), orginalImage.getHeight(),
        BufferedImage.TYPE_BYTE_BINARY);
    
Graphics2D graphics = blackAndWhiteImg.createGraphics();
graphics.drawImage(orginalImage, 0, 0, null);

    ImageIO.write(blackAndWhiteImg, "png", new File("C://picw//newscreenshot.png")); 
  }

}

You may convert blackAndWhiteImg to negative I guess.我猜您可以将 blackAndWhiteImg 转换为负数。

for (int x = 0; x < blackAndWhiteImg.getWidth(); x++) {
        for (int y = 0; y < blackAndWhiteImg.getHeight(); y++) {
            int rgba = blackAndWhiteImg.getRGB(x, y);
            Color col = new Color(rgba, true);
            col = new Color(255 - col.getRed(),
                            255 - col.getGreen(),
                            255 - col.getBlue());
            blackAndWhiteImg.setRGB(x, y, col.getRGB());
        }
    }

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

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