2012-06-02 61 views
0

我想運行簡單的JavaFx(2.0)應用程序,它將以縮略圖顯示三個圖像。 OS是Windows7中,我使用NetBeans 7.2 代碼是這個 -Javafx outofmemory錯誤

public class FX17 extends Application{ 

    public static void main(String[] args){ 
     launch(args); 
    } 
    public void start(Stage stage){ 

     HBox photoBar = new HBox(); 
     Group root = new Group(); 
     File f1 = new File("C:\\Users\\Pictures\\IMG_0021.jpg"); 
     File f2 = new File("C:\\Users\\Pictures\\IMG_0022.jpg"); 
     File f3 = new File("C:\\Users\\Pictures\\IMG_0023.jpg"); 

     Image i1 = new Image(f1.toURI().toString()); 
     Image i2 = new Image(f2.toURI().toString()); 
     Image i3 = new Image(f3.toURI().toString()); 
     ImageView iv1 = new ImageView(i1); 
     //iv1.setImage(i1); 
     iv1.setFitWidth(50); 
     iv1.setPreserveRatio(true); 
     iv1.setCache(true); 

     ImageView iv2 = new ImageView(i2); 
     //iv2.setImage(i2); 
     iv2.setFitWidth(50); 
     iv2.setPreserveRatio(true); 
     iv2.setCache(true); 

     ImageView iv3 = new ImageView(i3); 
     // iv3.setImage(i3); 
     iv3.setFitWidth(50); 
     iv3.setPreserveRatio(true); 
     iv3.setCache(true); 

     photoBar.getChildren().add(iv1); 
     photoBar.getChildren().add(iv2); 
     photoBar.getChildren().add(iv3); 
     //C:\Users\Public\Pictures\Sample Pictures 

     BorderPane pane = new BorderPane(); 
     pane.setTop(photoBar); 
     root.getChildren().add(photoBar); 
     //pane.setLeft(linkBar); 

     Scene scene = new Scene(root); 
     scene.setFill(Color.BLACK); 

     stage.setScene(scene); 
     stage.setWidth(415); 
     stage.setHeight(200); 
     stage.sizeToScene(); 
     stage.show(); 
    } 

} 

對於兩個圖像的程序運行和顯示兩個縮略圖,但3倍或更多的圖像,程序引發的OutOfMemoryError。圖像是JPG格式,平均大小爲2.5MB。有一些設置或圖像格式需要檢查嗎? 以下構造函數適合我。 Image img = new Image(file.toURI()。toString(),100,100,false,false);寬高比,平滑爲false。

+0

大家好,下面的構造函數工作正常 - – Chirota

回答

0

我想你的jpgs在高度和寬度方面都非常大。

jpg與加載的圖像緩衝區所佔用的內存不同。加載的圖像是解碼的未壓縮位圖,取決於圖像高度/寬度/顏色深度,而不是原始jpg文件大小。使用程序外部的圖像編輯器和/或

    1. 調整大小JPG文件到一個較小的高度和寬度(和/或較低的顏色深度)僅裝載一個單個:

      若要修正內存不足條件jpg和/或

    2. 增加可用於你的java應用程序的內存(例如-Xmx1500m)和/或
    3. 在圖像構造函數而不是圖像視圖中指定大小。

    指定圖像構造函數中的大小可確保一旦加載圖像,Java只保留足夠大的緩衝區以容納調整大小的圖像,而不是全尺寸的未壓縮圖像。

  • +0

    謝謝。這樣可行。 – Chirota