Java AWT BufferedImage save to JPG image file

Description

Java AWT BufferedImage save to JPG image file


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

import javax.imageio.ImageIO;

public class Main {
  public static void main(String[] args) {
    // draw 2D rounded rectangle with a buffered background
    BufferedImage buffImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);

    // obtain Graphics2D from buffImage and draw on it
    Graphics2D gg = buffImage.createGraphics();
    gg.setColor(Color.YELLOW);/*from  w w  w.  j a va2s  . co m*/
    gg.fillRect(0, 0, 10, 10);
    gg.setColor(Color.BLACK);
    gg.drawRect(1, 1, 6, 6);

    try {
      ImageIO.write(buffImage, "JPG", new File( "image.jpg"));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related