2013-02-26 60 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); 
}