Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - Loading images and memory problems

I've got a problem with loading images into my application. I'm trying to make a simple image browser. On the left I've got a list of folders. After clicking on folder's name on the list images should appear on the right(flow pane). Every image is in a HBox with a border. But I'm getting error like this quickly:

java.lang.outofmemoryerror java heap space

I looked on task manager - if I load only 6 photos the app takes 500mb of memory! And another thing - if I change folder on the list the memory remains taken. While choosing another folder I'm creating new flowPane

flowPane = new FlowPane();

So the old one with all the ImageViews should be deleted by the garbage collector, right?

How can I manage images in my app effectively?

  for(int i = 0 ; i < zdjecia.length; i++){
        ImageView iv2 = new ImageView();
        Image image = new Image("file:"+zdjecia[i].getAbsolutePath());
         iv2.setImage(image);
         if( image.getHeight() > image.getWidth()){
                iv2.fitHeightProperty().bind(szerokoscZdjecia.multiply(0.8).subtract(6));
        }else
                iv2.fitWidthProperty().bind(szerokoscZdjecia.subtract(6));

         iv2.setPreserveRatio(true);
         iv2.setSmooth(true);
         iv2.setCache(false);

         String styl = "-fx-border-color: #b2b3b3;"
                 + "-fx-border-width: 2;";

         HBox boxNaFotke = new HBox();
         boxNaFotke.prefWidthProperty().bind(szerokoscZdjecia);
         boxNaFotke.prefHeightProperty().bind(szerokoscZdjecia.multiply(0.8));
         boxNaFotke.setAlignment(Pos.CENTER);
         boxNaFotke.setStyle(styl);
         boxNaFotke.getChildren().add(iv2);
         fotki.add(boxNaFotke);
         flowPane.getChildren().add(boxNaFotke);
    }
like image 750
damian Avatar asked Feb 26 '13 11:02

damian


People also ask

How to load image in JavaFX?

1 Loading an Image. You can load an image in JavaFX by instantiating the class named Image of the package javafx.scene.image. 2 Multiple Views of an Image. You can also set multiple views for an image in the same scene. ... 3 Writing Pixels. JavaFX provides classes named PixelReader and PixelWriter classes to read and write pixels of an image.

What is pixel format in JavaFX?

3.2 Pixel Formats The image API in JavaFX gives you access to each pixel in an image. A pixel stores information about its color (red, green, blue) and opacity (alpha). The pixel information can be stored in several formats. An instance the PixelFormat<T extends Buffer>represents the layout of data for a pixel.

What is the use of imageview in Java?

An instance of the ImageViewclass is used to display an image loaded in an Imageobject. The ImageViewclass inherits from the Nodeclass, which makes an ImageViewsuitable to be added to a scene graph. The class contains several constructors: ImageView()

How do I set multiple views for an image in JavaFX?

You can also set multiple views for an image in the same scene. The following program is an example that demonstrates how to set various views for an image in a scene in JavaFX. Save this code in a file with the name MultipleViews.java. Compile and execute the saved java file from the command prompt using the following commands.


1 Answers

If you scale the Image using the constructor, the amount of the needed memory will be reduced dramatically.

Some example from the documentation:

// load an image and resize it to 100x150 without preserving its original aspect ratio
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);

// load an image and resize it to width of 100 while preserving its
// original aspect ratio, using faster filtering method
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);

like image 153
DVarga Avatar answered Oct 07 '22 12:10

DVarga