13 Replies Latest reply: Jun 12, 2008 9:17 AM by 807591 RSS

    loading large image files

    807591
      Hi,
      I use this code to load images:
       Toolkit.getDefaultToolkit().getImage(location); 
       
      this code works ok for medium size images .but when i tried to get Image with about 3 MB or higher , nothing happens !

      how can i solve this issue ?

      thanks
        • 1. Re: loading large image files
          807591
          The standard way to load images is:
          BufferedImage image = ImageIO.read(file_or_url_or_etc);
          See the Java@D tutorial: [http://java.sun.com/docs/books/tutorial/2d/images/index.html]

          A couple megs is not a big image, but remember that's the compressed file size. The size in memory will be larger. You may want to run java with more memory. See the documentation for flags -Xmx and -Xms:

          [http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html]

          ps. If image reading is taking a long time and delaying your GUI, you may want to do it in another thread, for example using a SwingWorker:

          [http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html]
          • 2. Re: loading large image files
            800456
            Use the utilities in the javax.imageio package
            • 3. Re: loading large image files
              807591
              organic_othman wrote:
              Hi,
              I use this code to load images:
              Toolkit.getDefaultToolkit().getImage(location); 
              this code works ok for medium size images .but when i tried to get Image with about 3 MB or higher , nothing happens !

              how can i solve this issue ?

              thanks
              If you want to load images this way, then you have to be sure your image is loaded before you try to do anything else with it. The way Java has provided to do this is with MediaTracker. Look in the API under MediaTracker and it supplies an example of how to use it.
              • 4. Re: loading large image files
                807591
                morgalr wrote:
                organic_othman wrote:
                Hi,
                I use this code to load images:
                Toolkit.getDefaultToolkit().getImage(location); 
                this code works ok for medium size images .but when i tried to get Image with about 3 MB or higher , nothing happens !

                how can i solve this issue ?

                thanks
                If you want to load images this way, then you have to be sure your image is loaded before you try to do anything else with it. The way Java has provided to do this is with MediaTracker. Look in the API under MediaTracker and it supplies an example of how to use it.
                ImageIO.read is simpler, since it doesn't load asynchronously, and so there is no need to use MediaTracker.
                • 5. Re: loading large image files
                  807591
                  ImageIO.read is simpler, since it doesn't load asynchronously, and so there is no need to use MediaTracker.
                  I know that, but depending on the OP's comfort levels it may not be simpler for him/her and both ways achieve the same result--the correct loading of an image.
                  • 6. Re: loading large image files
                    807591
                    morgalr wrote:
                    ImageIO.read is simpler, since it doesn't load asynchronously, and so there is no need to use MediaTracker.
                    I know that, but depending on the OP's comfort levels it may not be simpler for him/her and both ways achieve the same result--the correct loading of an image.
                    I see what you mean. On the other hand, one can tempt the OP to use imageio because he can do things like use IIOWriteProgressListener to get feedback (40% loaded!)
                    • 7. Re: loading large image files
                      807591
                      I see what you mean. On the other hand, one can tempt the OP to use imageio because he can do things like use IIOWriteProgressListener to get feedback (40% loaded!)
                      Indeed.
                      • 8. Re: loading large image files
                        807591
                        I need your help guys to load some photo images taken with a digital camera

                        you can find one sample photo jpeg image here : http://www.myjavaserver.com/~othmanelmoulat/IMGP0259.JPG

                        what i need is to load these photos , resize them and display them in JLabel using JLabel(new ImageIcon())

                             
                        i tried all solutions you suggested in this post without luck !
                        i also tried using jimi and imagero (imgero a robust commercial java imaging library) to load the photos ; but nothing worked !!
                        there is something strange with these photos ! i don't know what is it.
                        i even don't get any debug info eventhough i added many debug lines to see if i reach the code for loading image and resizing..
                        this is a mystery i need to figure out .
                        do you have any idea how i can read these photos , resize them into small thumbnails and display them inside a JLabel ?

                        i would appreciate a lot any help .
                        • 9. Re: loading large image files
                          Darryl Burke
                          I saved your image to e:/java/test/jpg and this code worked fine for me.
                          import java.awt.Image;
                          import java.io.File;
                          import java.io.IOException;
                          import javax.imageio.ImageIO;
                          import javax.swing.ImageIcon;
                          import javax.swing.JFrame;
                          import javax.swing.JLabel;
                          import javax.swing.SwingUtilities;
                          
                          public class TestImageIO {
                             
                             
                             void makeUI() {
                                File file = new File("e:/java/test.jpg");
                                Image thumb = null;
                                try {
                                   thumb = ImageIO.read(file)
                                         .getScaledInstance(128, 96, Image.SCALE_SMOOTH);
                                } catch (IOException ex) {
                                   ex.printStackTrace();
                                }
                                
                                JLabel label = new JLabel(new ImageIcon(thumb));
                                JFrame frame = new JFrame("TestImageIO");
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                frame.setSize(300, 300);
                                frame.add(label);
                                frame.setLocationRelativeTo(null);
                                frame.setVisible(true);
                             }
                             
                             public static void main(String[] args) {
                                SwingUtilities.invokeLater(new Runnable() {
                                   public void run() {
                                      new TestImageIO().makeUI();
                                   }
                                });
                             }
                          }
                          db
                          • 10. Re: loading large image files
                            807591
                            Ok i solved the problem guys !
                            it doesn't have anything to do with resizing or loading the images.
                            i only had to make sure my image file ends with (.jpg) (the images extension were .JPG in capital) so they were not detected by my image filter.
                            i turned image file name to lowercase and checked the extension and that's it !
                            all works fine !
                            thank you all
                            • 11. Re: loading large image files
                              807591
                              organic_othman wrote:
                              Ok i solved the problem guys !
                              it doesn't have anything to do with resizing or loading the images.
                              i only had to make sure my image file ends with (.jpg) (the images extension were .JPG in capital) so they were not detected by my image filter.
                              i turned image file name to lowercase and checked the extension and that's it !
                              all works fine !
                              thank you all
                              Woo-hoo! Java FTW!
                              • 12. Re: loading large image files
                                807591
                                DrLaszloJamf wrote:

                                Woo-hoo! Java FTW!
                                thank you . I'm curious to know what "FTW" means ;)
                                sorry if this seems a silly question!
                                • 13. Re: loading large image files
                                  807591
                                  For The Win