使用 Graphics2d 修改图像输出空白图像

时间:2021-07-18 08:11:34

标签: java graphics2d

我正在尝试使用 Graphics2D 库向给定图像添加噪声。例如,这个:enter image description here。当前的问题是输出文件为空白。这是我的代码:

BufferedImage image = ImageIO.read(new File("src/digit.png"));
BufferedImage output = new BufferedImage(28, 28, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = output.createGraphics();
Random random = new Random();
for (int i = 0; i < output.getHeight(); i++) {
  for (int j = 0; j < output.getWidth(); j++) {
    Color color = new Color(image.getRGB(j, i));
    int choice = random.nextInt(2);
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();

    int rand = random.nextInt(10);
    if (choice == 0) {
      r += rand;
      g += rand;
      b += rand;
    } else {
      r -= rand;
      g -= rand;
      b -= rand;
    }

    if (r < 0) {
      r = 0;
    }
    if (g < 0) {
      g = 0;
    }
    if (b < 0) {
      b = 0;
    }
    if (r > 255) {
      r = 255;
    }
    if (g > 255) {
      g = 255;
    }
    if (b > 255) {
      b = 255;
    }
    graphics2D.setColor(new Color(r, g, b));
    graphics2D.fillRect(j, i, 1, 1);
  }
}
File outFile = new File("output.png");
ImageIO.write(output, "png", outFile);

它的主要问题是什么?

1 个答案:

答案 0 :(得分:0)

首先,我要感谢@user16320675 帮助调试代码问题并提供建议以使其更高效。最后,问题在于图像具有透明像素,默认情况下(用于创建 color 时)为黑色。相反,我编写了一个基于 this post 的实用函数来识别透明像素并使它们变白。

现在得到的图像是: enter image description here

代码:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class NoiseImage {
  public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("src/digit.png"));
    BufferedImage output = new BufferedImage(28, 28, BufferedImage.TYPE_INT_ARGB);
    Random random = new Random();
    for (int i = 0; i < output.getHeight(); i++) {
      for (int j = 0; j < output.getWidth(); j++) {
        Color color = new Color(image.getRGB(j, i), true);
        if (isTransparent(image.getRGB(j, i))) {
          color = new Color(255, 255, 255);
        }
        int choice = random.nextInt(2);
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();
        int a = color.getAlpha();

        int rand = random.nextInt(20);
        if (choice == 0) {
          r += rand;
          g += rand;
          b += rand;
          a += rand;
        } else {
          r -= rand;
          g -= rand;
          b -= rand;
          a -= rand;
        }

        r = isOutOfBounds(r);
        g = isOutOfBounds(g);
        b = isOutOfBounds(b);
        a = isOutOfBounds(a);
        output.setRGB(j, i, new Color(r, g, b, a).getRGB());
      }
    }
    File outFile = new File("output.png");
    ImageIO.write(output, "png", outFile);
  }

  private static int isOutOfBounds(int val) {
    if (val > 255) {
      return 255;
    }
    return Math.max(val, 0);
  }

  private static boolean isTransparent(int val) {
    return val >> 24 == 0x00;
  }
}
相关问题