2013-02-26 57 views
1

我保存的JFreeChart为JPEG文件的方法是:保存为.jpg文件的图像:Java的

JFreeChart chart = ChartFactory.createXYLineChart(
"Hysteresis Plot", // chart title 
"Pounds(lb)", // domain axis label 
"Movement(inch)", // range axis label 
dataset, // data 
PlotOrientation.VERTICAL, // orientation 
false, // include legend 
true, // tooltips 
false // urls 
); 

然后:

image=chart.createBufferedImage(300, 200); 

的图像appeas为: enter image description here

我的保存功能是:

public static void saveToFile(BufferedImage img) 
    throws FileNotFoundException, IOException 
    { 
     FileOutputStream fos = new FileOutputStream("D:/Sample.jpg"); 
     JPEGImageEncoder encoder2 = 
     JPEGCodec.createJPEGEncoder(fos); 
     JPEGEncodeParam param2 = 
     encoder2.getDefaultJPEGEncodeParam(img); 
     param2.setQuality((float) 200, true); 
     encoder2.encode(img,param2); 
     fos.close(); 
    } 

我叫它为:

try{ 
      saveToFile(image); 
      }catch(Exception e){ 
      e.printStackTrace(); 
     } 

保存的图像appeas为:

enter image description here

任何建议,在那里我错了或如何保存它的显示方式,也可以我需要保存为.png。任何人都可以让我知道如何保存为.png?

感谢

+2

关于另存为png的信息,http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html – Coffee 2013-02-26 17:58:30

回答

6

一个简单的解决方案:

public static void saveToFile(BufferedImage img) 
    throws FileNotFoundException, IOException 
    { 

     File outputfile = new File("D:\\Sample.png"); 
    ImageIO.write(img, "png", outputfile); 
    } 

保存的图像,它的显示方式。

1

我宁愿建议,而不是使用ImageIo.write为了节省你的形象,你最好使用以下功能:

ChartUtilities.saveChartAsJPEG("name of your file", jfreechart, lenght, width); 

因为这样你可以管理图片的大小,而且还节省图片没有过滤器。

1

这是一个很好的例子,可以做到这一点。

File imageFile = new File("C:\\LineChart.png"); 
int width = 640; 
int height = 480; 
try { 
    ChartUtilities.saveChartAsPNG(imageFile, chart, width, height); 
} catch (IOException ex) { 
    System.err.println(ex); 
}