package demo;
import java.io.File;
import org.jfree.chart.JFreeChart;
import java.io.IOException;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.awt.image.BufferedImage;
import org.jfree.chart.encoders.ImageFormat;
import java.awt.Graphics2D;
import java.awt.*;
import java.awt.*;
import org.jfree.chart.encoders.ImageEncoder;
import org.jfree.chart.encoders.ImageEncoderFactory;
import javax.swing.JFrame;
import javax.imageio.ImageIO;
public class ToImg {
public ToImg() {
}
/** *//**
* Saves a chart to a file in JPEG format.
*
* @param file the file (<code>null</code> not permitted).
* @param quality the JPEG quality setting.
* @param chart the chart (<code>null</code> not permitted).
* @param width the image width.
* @param height the image height.
*
* @throws IOException if there are any I/O errors.
*/
public static void saveChartAsJPEG(File file,
float quality,
JFreeChart chart,
int width,
int height) throws IOException {
if (file == null) {
throw new IllegalArgumentException("Null 'file' argument.");
}
if (chart == null) {
throw new IllegalArgumentException("Null 'chart' argument.");
}
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
BufferedImage images = chart.createBufferedImage(width, height, null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
// draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info);
g2.dispose();
writeBufferedImage(image, ImageFormat.JPEG, out, quality);
out.close();
}
public static void writeBufferedImage(BufferedImage image, String format,
OutputStream outputStream, float quality)
throws IOException {
ImageEncoder imageEncoder
= ImageEncoderFactory.newInstance(format, quality);
imageEncoder.encode(image, outputStream);
}
public static void main(String[] args) {
Robot robot = null;
try {
JFrame f = new Frame1();
f.setSize(500,600);
f.setVisible(true);
f.setDefaultCloseOperation(2);
BufferedImage image = new BufferedImage(500, 600, BufferedImage.TYPE_INT_RGB);
f.getContentPane().paint(image.getGraphics());
FileOutputStream out = new FileOutputStream("d:\\b.jpg");
ImageIO.write(image, "jpeg" , out);
out.close();
/** *//**
抓图
Robot robot = new java.awt.Robot();
BufferedImage image = robot.createScreenCapture(new Rectangle(100, 100));
ImageEncoder imageEncoder
= ImageEncoderFactory.newInstance(ImageFormat.JPEG, 100);
FileOutputStream out = new FileOutputStream("d:\\a.jpg");
imageEncoder.encode(taget, out);
}*/
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
posted on 2005-12-20 17:16
鱼上游 阅读(755)
评论(0) 编辑 收藏 所属分类:
爪哇风景欣赏 、
爪哇世界探险