2 Replies Latest reply: Feb 18, 2013 2:15 AM by 966041 RSS

    Image is not saved in jpeg/bmp file but ok with png

    966041
      Hi i am unable to save the image in disk in jpeg format.

      Here is the code.
      public void saveImage(){
      
      //if no filechooser is there means no image file opened.
              if(fileChooser==null){
                  if(errorWindow==null)
                      errorWindow=new ErrorWindow(XXX.getPrimaryStage());
                  errorWindow.showErrorWindow("There is no image to save !!\n");
              }
              else{
                  fileChooser.setTitle("Save The Image");
                  File saveFile=fileChooser.showSaveDialog(XXX.getPrimaryStage());
                  if(saveFile!=null){
                      System.out.println("saveFile path= "+saveFile);
                     
                      //get the extension of output image to be saved.
                      //XXX: is the main class name(say)
                      outExtension=*XXX*.getImageExtension(saveFile);
                      
                      //if user does not give extension: set default extension as jpg
                      if(outExtension.equalsIgnoreCase(saveFile.getName())){
                          outExtension="jpg";                    
                          String newPath=(saveFile.toString())+".jpg";
                          saveFile=new File(newPath);                    
                      }
                      
                      Task task = new Task<Void>() {
                          @Override
                          public Void call() {
                              Platform.runLater(
                                      new Runnable() {
                                          public void run() {
                                              try {
                                                  //The image is inside ImageView->inside ScrollPane-> inside TabPane.
                                                  Image curImage=XXX.getCurrentImage();
                                                  int width=(int)curImage.getWidth();
                                                  int height=(int)curImage.getHeight();                               
                                                  System.out.println("cur image width= "+width+" ht= "+height);
      
                                                  bufferedImage=new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB );
                                                  //set the current image to awt Buffered Image
                                                  SwingFXUtils.fromFXImage(curImage, bufferedImage);
                                                  
                                                    /*
                                                      imageTabPane=xxx.getImageTabPane();
                                                      Tab tab=imageTabPane.getSelectionModel().getSelectedItem();                                     
                                                      TabPane childTabPane=(TabPane)tab.getContent();
                                                      ScrollPane sp=(ScrollPane)childTabPane.getSelectionModel().getSelectedItem().getContent();
                                                      final ImageView imageView=(ImageView)sp.getContent();
                                                      
                                                      WritableImage wim = new WritableImage(width,height);
                                                      
                                                      imageView.snapshot(null, wim);
                                                      System.out.println(" snapShot width= "+wim.getWidth()+" ht="+wim.getHeight());                              
                                                      *ImageIO.write(SwingFXUtils.fromFXImage(wim,null), outExtension, saveFile);*
                                                         //<------ its not working  for png,jpeg,bmp,gif                                           
                                                      */                                    
                                                      // the below lines are working only for png and gif
                                                      OutputStream out = new FileOutputStream(saveFile);                                                 
                                                      ImageIO.write((RenderedImage) bufferedImage, outExtension, out);
                                                      out.flush();
                                                                                         
                                                  
                                              } catch (Exception ex) {
                                                  Logger.getLogger(FileMenuController.class.getName()).log(Level.SEVERE, null, ex);
                                                  //ex.printStackTrace();
                                              }
                                              System.out.println("finished");
                                      }
                                      });
                              
                              return null;
                          }
                      };
                      
                      
                      Thread th = new Thread(task);
                      th.start();
                      ////      
                                      
                  }else{
                      System.out.println("File is not saved");
                  }
              }
          }//saveImage
      Note: The above code is executed in ubuntu(10.04) using Netbeans 7.2(Javafx(2.2), java(1.7.0_11).

      When i run(from terminal) separately in a new class with an image in an Imageview( inside a Vbox) and take the snapshot
      of the imageview and write into the file using ImageIO.write(SwingFXUtils.fromFXImage(wim,null), outExtension, saveFile);
      with any extension (jpeg,png etc) it is working fine.

      Please help me.


      Any small hint is also helpful.Please feel free to comment or give suggestion.

      Edited by: 963038 on Feb 17, 2013 7:14 PM
        • 1. Re: Image is not saved in jpeg/bmp file but ok with png
          e-dubkendo
          When you try saving code as jpeg, you have the catch block logging exceptions. What's in the log, might be useful.
          • 2. Re: Image is not saved in jpeg/bmp file but ok with png
            966041
            When i omit the line OutputStream out = new FileOutputStream(saveFile);
            and write only
            ImageIO.write((RenderedImage) bufferedImage, outExtension,saveFile);
            //out.flush();
            then saving the image even in "png" also fails.
            The following is the error code:
            Note : FileMenuController is my file where the saveImage() is wrtten.
            javax.imageio.IIOException: I/O error writing PNG file!
            finished
                 at com.sun.imageio.plugins.png.PNGImageWriter.write(PNGImageWriter.java:1168)
                 at javax.imageio.ImageWriter.write(ImageWriter.java:615)
                 at javax.imageio.ImageIO.doWrite(ImageIO.java:1612)
                 at javax.imageio.ImageIO.write(ImageIO.java:1536)
                 at *newciptk.controls.menu.file.FileMenuController$1$1.run(FileMenuController.java:205)*
                 at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
                 at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
                 at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
                 at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
                 at java.lang.Thread.run(Thread.java:722)
            Caused by: java.io.IOException: Operation not supported
                 at java.io.RandomAccessFile.writeBytes(Native Method)
                 at java.io.RandomAccessFile.write(RandomAccessFile.java:499)
                 at javax.imageio.stream.FileImageOutputStream.write(FileImageOutputStream.java:124)
                 at javax.imageio.stream.ImageOutputStreamImpl.writeInt(ImageOutputStreamImpl.java:91)
                 at com.sun.imageio.plugins.png.ChunkStream.finish(PNGImageWriter.java:136)
                 at com.sun.imageio.plugins.png.PNGImageWriter.write_IHDR(PNGImageWriter.java:401)
                 at com.sun.imageio.plugins.png.PNGImageWriter.write(PNGImageWriter.java:1135)
                 ... 9 more
            
            javax.imageio.IIOException: I/O error writing PNG file!
                 at com.sun.imageio.plugins.png.PNGImageWriter.write(PNGImageWriter.java:1168)
                 at javax.imageio.ImageWriter.write(ImageWriter.java:615)
                 at javax.imageio.ImageIO.doWrite(ImageIO.java:1612)
                 at javax.imageio.ImageIO.write(ImageIO.java:1536)
                 at newciptk.controls.menu.file.FileMenuController$1$1.run(FileMenuController.java:205)
                 at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
                 at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
                 at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
                 at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
                 at java.lang.Thread.run(Thread.java:722)
            Caused by: java.io.IOException: Operation not supported
                 at java.io.RandomAccessFile.writeBytes(Native Method)
                 at java.io.RandomAccessFile.write(RandomAccessFile.java:499)
                 at javax.imageio.stream.FileImageOutputStream.write(FileImageOutputStream.java:124)
                 at javax.imageio.stream.ImageOutputStreamImpl.writeInt(ImageOutputStreamImpl.java:91)
                 at com.sun.imageio.plugins.png.ChunkStream.finish(PNGImageWriter.java:136)
                 at com.sun.imageio.plugins.png.PNGImageWriter.write_IHDR(PNGImageWriter.java:401)
                 at com.sun.imageio.plugins.png.PNGImageWriter.write(PNGImageWriter.java:1135)
                 ... 9 more