Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFreeChart & Image

Tags:

jfreechart

Is it possible to cast an image/BufferedImage to JFreeChart?

like image 835
Suma Avatar asked Mar 11 '11 12:03

Suma


1 Answers

Casting an image to JFree is not possbile. To create an image from JFreechart you can do the following:

BufferedImage objBufferedImage=objJFreechart.createBufferedImage(600,800);
ByteArrayOutputStream bas = new ByteArrayOutputStream();
        try {
            ImageIO.write(objBufferedImage, "png", bas);
        } catch (IOException e) {
            e.printStackTrace();
        }

byte[] byteArray=bas.toByteArray();

This creates the byte[] .

Now you need to create the image from byte[]. The following does this.

InputStream in = new ByteArrayInputStream(obj);
BufferedImage image = ImageIO.read(in);
File outputfile = new File("image.png");
ImageIO.write(image, "png", outputfile);

The image gets created at the place where your project is created(local drive).

like image 79
Anuj Balan Avatar answered Oct 06 '22 00:10

Anuj Balan