JavaSheriff
JavaSheriff

Reputation: 7687

Use java to render a jpg file

I am trying to write a simple code to write a red 100x100 jpg
For some reason the colors are not right,
I am only setting the color RED :

renderdImg.setRGB(x, y, Color.RED.getRGB());

but the fnal image comes out purplish, what am I doing wrong?

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

import javax.imageio.ImageIO;

public class ImageWriter {

    public static void main(String[] args) throws IOException {
     String fileName = "red_100.jpg";
     String filePath = "c:\\temp\\";
     int width = 100;
     int height = 100;
     BufferedImage renderdImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);
      for(int x=0;x< width; x++) {
          for(int y=0;y<height; y++) {
              renderdImg.setRGB(x, y, Color.RED.getRGB());
          }}
       File fileToWrite  = new File(filePath + fileName);
       ImageIO.write(renderdImg, "jpg", fileToWrite);   
    }
}

Upvotes: 0

Views: 914

Answers (1)

Antot
Antot

Reputation: 3964

Set the image type to BufferedImage.TYPE_INT_RGB and it should become reddish:

BufferedImage renderdImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);

Upvotes: 3

Related Questions