javaimagejavafximageviewscrollpane

Why are some images rotated when showing it in javafx ImageView?


Images that are 960x1280 are rotated horizontally. How can I force them to stay vertical?

ScrollPane sp = new ScrollPane();
sp.setPrefSize(900, 700);

public void showPicture(String str){
  try{
            
             
            File file = new File("pictures\\"+str+".jpg");
            Image image = new Image(file.toURI().toString());
            
            if(image.getWidth()>900)
            {
                Double x=image.getWidth();
                Double y=image.getHeight();
                Double z=x/900;
                if((y/z)>700)
                {
                    z=y/700;
    
                }
                
                
                image = new Image(file.toURI().toString(),x/z,y/z,false,false);
            }
            
            
            
                ImageView imageView = ImageViewBuilder.create()
                .image(image)
                .build();
                
                
                sp.setContent(imageView);
                
   }catch(Exception e){System.err.println(e);}
}

I tried to change ScrollPane to 700x700, didn't work. It happens only to pictures 960x1280, others are shown fine. Pictures in the folder are vertical.


Solution

  • Code was fine. The problem was incorrect EXIF orientation of pictures.

    More details here: https://sirv.com/help/articles/rotate-photos-to-be-upright/