This may not be the most elegant method, but it will work:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageTest {
public static void main(String[] args) throws IOException {
int width = 10
int height = 10
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
int grayscale = ...
int colorValue = grayscale | grayscale << 8 | grayscale << 16;
img.setRGB(x, y, colorValue);
}
}
ImageIO.write(img, "png", new File("output.png"));
}
}
The colors colorValue consist of r, g, and b (b in low byte, g one before and r one before). Since your image is grayscale, you can simply use the same r, g, and b values ββfor your image so you can just do:
int colorValue = grayscale | grayscale << 8 | grayscale << 16;
source
share