Monday, November 15, 2004

Create Jpeg picture with Java

/*Note: Under unix or Mac, Use Java 1.4, which supports headless Java (no more X server required). Run your application with -Djava.awt.headless=true given as parameter to the virtual machine.
When running Tomcat, use export JAVA_OPTS=-Djava.awt.headless=true*/

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

public class Hello {

public static void main(String[] args) throws Exception {
BufferedImage img = new BufferedImage(600, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.red);
g2d.fillRect(0, 0, 150, 300);
g2d.fillRect(450, 0, 150, 300);
g2d.setColor(Color.white);
g2d.fillRect(149, 0, 300, 300);
File imgfile = new File("/Users/zhibin/tmp/test.jpg");
javax.imageio.ImageIO.write(img, "jpg", imgfile);
}

}///:~

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home