4 Replies Latest reply on Apr 12, 2008 4:57 PM by 843799

    JPG to BMP conversion problems...

    843799
      Hi folks, I'm having a bit of trouble. I can't see why BufferedImage img is returning null. Can anyone spot anything?
          private void convertImages() {
              //Read all the files in a given directory in.  If they are images then convert them.
      
              File file;
              JFileChooser fc = new JFileChooser();
              fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
              int returnVal = fc.showOpenDialog(fc);
              if (returnVal == JFileChooser.APPROVE_OPTION) {
                  file = fc.getSelectedFile();      // Get directory to file.
                  File[] files = file.listFiles();  // Get file array
                  outputField.setText("");
                  outputField.append("Converting all images in " + file.getPath() +
                          "to bitmap images.\n  Images will be saved in "
                          + file.getParentFile().getPath() + File.separator + "rawdata.\n");
                  File outputdirectory = null;
      
                  if(new File(file.getParentFile().getPath()+File.separator+"rawdata").mkdir() || new File(file.getParentFile().getPath()+File.separator+"rawdata").exists()){
                      outputdirectory = new File(file.getParentFile().getPath()+File.separator+"rawdata");
                      outputField.append("Created directory: " + outputdirectory.getPath()+"\n");
                  } else {
                      outputField.append("Unable to create a rawdata directory.");
                      return;
                  }
      
                  for(File image_file : files){
                      if(isImage(image_file)){
                          outputField.append("Converting " + image_file.getName() + " to bitmap.\n");
                          try {
                              img = ImageIO.read(image_file);
                              output = new File(outputdirectory.getPath()+File.separator+image_file.getName()+".bmp");
                              ImageIO.write(img, "bmp", output);
                              img.flush();
                          } catch (IOException ex) {
                              outputField.append("Problem when trying to convert file: " + image_file.getName() + "\n");
                          } catch (IllegalArgumentException e){
                              e.printStackTrace();
                              outputField.append("Problem with image file. \n");
                          }
                      } else {
                          outputField.append("Ignored file " + image_file.getName() + " because it is not an image.\n");
                      }
                  }
                  outputField.append("Job done.\n");
              }
      
          }
      I'm getting this output:
      Converting all images in F:\Documents and Settings\Eric\My Documents\testnewto bitmap images.
        Images will be saved in F:\Documents and Settings\Eric\My Documents\rawdata.
      Created directory: F:\Documents and Settings\Eric\My Documents\rawdata
      Ignored file .DS_Store because it is not an image.
      Ignored file ._.DS_Store because it is not an image.
      Converting 1.jpg to bitmap.
      Problem with image file. 
      Converting 2.jpg to bitmap.
      Problem with image file. 
      Converting 3.jpg to bitmap.
      Problem with image file. 
      Converting 4.jpg to bitmap.
      Problem with image file. 
      Converting 5.jpg to bitmap.
      Problem with image file. 
      Ignored file Results because it is not an image.
      Ignored file testnewtrain.txt because it is not an image.
      Ignored file Thumbs.db because it is not an image.
      Ignored file train.txt because it is not an image.
      Job done.
      And an error:
      java.lang.IllegalArgumentException: im == null!
              at javax.imageio.ImageIO.write(ImageIO.java:1457)
              at javax.imageio.ImageIO.write(ImageIO.java:1521)
      Obviously, for whatever reason img isn't initialising, but I can't see why not. It's probably something silly. Any suggestions?
      Eric

      Edited by: escocialite on Apr 9, 2008 2:42 PM
        • 1. Re: JPG to BMP conversion problems...
          darrylburke
          Obviously, for whatever reason img isn't initialising, but I can't see why not.
          I ran your code through a test, and as expected, it performed flawlessly.
          It's probably something silly.
          Can't tell, since it worked for me.
          Any suggestions?
          Run this SSCCE which works flawlessly or me and see how it works for you.
          package graphics;
          
          import java.awt.Graphics2D;
          import java.awt.Image;
          import java.awt.Toolkit;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.IOException;
          import javax.imageio.ImageIO;
          import javax.swing.JFileChooser;
          
          public class LoadAndSaveImage {
             
             public void convertImages() {
                
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.showOpenDialog(null);
                File folder = chooser.getSelectedFile();
                File[] files = folder.listFiles();
                for (File file : files) {
                   String name = file.getName();
                   if (name.endsWith("jpg") || name.endsWith("JPG")) {
                      BufferedImage img = null;
                      File outFile = new File(folder.getPath() + File.separator + name + ".bmp");
                      try {
                         img = ImageIO.read(file);
                         ImageIO.write(img, "bmp", outFile);
                         img.flush();
                      } catch (IOException ex) {
                         ex.printStackTrace();
                      }
                   }
                }
             }
             
             public static void main(String[] args) {
                new LoadAndSaveImage().convertImages();
             }
          }
          luck, db
          • 2. Re: JPG to BMP conversion problems...
            843799
            Okay, so I ran your SSCCE, but unfortunately I got the same error occurring:
            Exception in thread "main" java.lang.IllegalArgumentException: im == null!
                 at javax.imageio.ImageIO.write(Unknown Source)
                 at javax.imageio.ImageIO.write(Unknown Source)
                 at graphics.LoadAndSaveImage.convertImages(LoadAndSaveImage.java:28)
                 at graphics.LoadAndSaveImage.main(LoadAndSaveImage.java:38)
            Any hints?

            Eric
            • 3. Re: JPG to BMP conversion problems...
              800379
              Darryls code works for me. From the api docs of ImageIO.read():
              If no registered ImageReader claims to be able to read the resulting stream, null is returned
              What system and java runtime are you using?
              • 4. Re: JPG to BMP conversion problems...
                843799
                Good news - I solved the problem... or rather, the problem in a known one. ImageIO doesn't work on images that have been altered by Windows Explorer (like using rotation). I presume performing these operations somehow changes the metadata of the image, making it unreadable by ImageIO. This is a known bug, and I read about it somewhere (sorry I can't be more specific). Anybody else who has these issues, try using the original image. Oropen it and save a copy in photoshop or something similar. This should reset the metadata.

                Regards,
                Eric