19

I want to read all the images in a folder using Java.

When: I press a button in the Java application,
It should:

  • ask for the directory's path in a popup,
  • then load all the images from this directory,
  • then display their names, dimension types and size.

How to proceed?

I have the code for read the image and also for all image in the folder but how the things i told above can be done?

Any suggestion or help is welcome! Please provide reference links!

3
  • 12
    show us what you have, and tell us where you are getting stuck exactly if you expect help on this. How to Ask
    – Adam
    Commented Jul 2, 2012 at 20:43
  • 1
    Since your question is pretty generic, I can but guess that this is you're after: docs.oracle.com/javase/tutorial/uiswing/components/… This is one way of doing it.
    – biziclop
    Commented Jul 2, 2012 at 21:02
  • 1
    @kundanraj: if this is homework, please tag is as such.
    – haylem
    Commented Jul 2, 2012 at 21:10

3 Answers 3

51

Untested because not on a machine with a JDK installed, so bear with me, that's all typed-in "as-is", but should get you started (expect a rush of downvotes...)

Loading all the Images from a Folder

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Test {

    // File representing the folder that you select using a FileChooser
    static final File dir = new File("PATH_TO_YOUR_DIRECTORY");

    // array of supported extensions (use a List if you prefer)
    static final String[] EXTENSIONS = new String[]{
        "gif", "png", "bmp" // and other formats you need
    };
    // filter to identify images based on their extensions
    static final FilenameFilter IMAGE_FILTER = new FilenameFilter() {

        @Override
        public boolean accept(final File dir, final String name) {
            for (final String ext : EXTENSIONS) {
                if (name.endsWith("." + ext)) {
                    return (true);
                }
            }
            return (false);
        }
    };

    public static void main(String[] args) {

        if (dir.isDirectory()) { // make sure it's a directory
            for (final File f : dir.listFiles(IMAGE_FILTER)) {
                BufferedImage img = null;

                try {
                    img = ImageIO.read(f);

                    // you probably want something more involved here
                    // to display in your UI
                    System.out.println("image: " + f.getName());
                    System.out.println(" width : " + img.getWidth());
                    System.out.println(" height: " + img.getHeight());
                    System.out.println(" size  : " + f.length());
                } catch (final IOException e) {
                    // handle errors here
                }
            }
        }
    }
}

APIs Used

This is relatively simple to do and uses only standard JDK-packaged classes:

These sessions of the Java Tutorial might help you as well:

Possible Enhancements

  • Use Apache Commons FilenameUtils to extract files' extensions
  • Detect files based on actual mime-types or content, not based on extensions
  • I leave UI code up to you. As I'm unaware if this is homework or not, I don't want to provide a full solution. But to continue:
    • Look at a FileChooser to select the folder.
    • I assume you already know how to make frames/windows/dialogs.
    • Read the Java Tutorial How to Use Icons sections, which teaches you how to display and label them.
  • I left out some issues to be dealt with:
    • Exception handling
    • Folders with evil endigs (say you have a folder "TryMeIAmEvil.png")

By combining all of the above, it's pretty easy to do.

7
  • 1
    everything went better than expected? ;)
    – Adam
    Commented Jul 2, 2012 at 21:23
  • @codesparkle: I don't, maybe that code actually works? :) Usually when you post something that fails, you get quickly trashed for submitted crappy code. Or maybe it's just not rush-hour yet. Plus as I went I realized the OP was probably asking for homework so I didn't keep on going.
    – haylem
    Commented Jul 2, 2012 at 21:24
  • @DavidKroukamp: it's not too hard for relatively simple things once you know the APIs, and the Javadoc and Java Tutorial are online to help refresh memories and put pieces back together. Remembering imports is usually the tricky part (as I almost never type any now that IDEs do it for me...)
    – haylem
    Commented Jul 2, 2012 at 21:27
  • Lol yes imports are fatherless :). I have corrected your code where neccessary but it wasnt much so my +1 still stands great job Commented Jul 2, 2012 at 21:33
  • @DavidKroukamp: Thanks. Not sure your edits make it clearer though. I had purposefully omitted the class declaration (and thus not made the variable static final, I was just using local final vars, old habit to always make them like that). Usually I tend to just write the essential. Probably better for the OP though.
    – haylem
    Commented Jul 2, 2012 at 21:35
1

javaxt.io.Directory directory = new javaxt.io.Directory("C:\Users\Public\Pictures\Sample Pictures"); directory.getFiles(); javaxt.io.File[] files;

    java.io.FileFilter filter = file -> !file.isHidden() && (file.isDirectory() || (file.getName().endsWith(".jpg")));
    files = directory.getFiles(filter, true);
    System.out.println(Arrays.toString(files));
-1
step 1=first of all make a folder out of webapps 
step2= write code to uploading a image in ur folder
step3=write a code to display a image in ur respective jsp,html,jframe what u want

this is folder=(images)
reading image for folder'
Image image = null;
        try {
            File sourceimage = new File("D:\\images\\slide4.jpg");
              image = ImageIO.read(sourceimage);

        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.