简体   繁体   中英

How do I get png image names, paths, heights, and widths from a directory of folders in java?

I'm trying to write a program that prompts the user via GUI for a folder full of other folders, png images, or other types of files. I want to populate a jTable with the only the image names, paths, heights, and widths (in that order) at first before continuing onto other comparison operations I won't get into right now.

I tried using jFileChooser to find the folder the user wants. How the thing is going to find and filter for .png files eludes me at this time. I tried using a for each loop to cycle through a given array of files from jFileChooser.getSelectedFiles(), but I was unable to differentiate the files as png images or otherwise via means of my understanding.

I attempted using the following code within my desired button action listener:

    private void btnPatchSelActionPerformed(java.awt.event.ActionEvent evt)
{                                            
        fchsFolderChooser.showOpenDialog(null);
        File files[] = fchsFolderChooser.getSelectedFiles();
        String fileNames[] = fchsFolderChooser.getSelectedFile().list("*.png");
    }

I thought that I'd need to have the file data available later for populating the list of heights and widths, so I created an array of File objects to hold those. I also wanted a filtered list of String file names that had the .png extension, but implementing this has proven difficult for me to understand.

How would you go about finding all the png image files within a specified directory (no other file types required), getting their names, paths, heights, and widths, and finally populate a jTable with those data points? (in as few lines of code as possible)

Listing Files

File selectedFile = fchsFolderChooser.getSelectedFile();
File pngs[] = selectedFile.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        return pathname.getName().toLowerCase().endsWith(".png");
    }
});

See File#listFiles and FileFilter for more details...

Image Properties

for (File png : pngs) {

    try {
        BufferedImage img = ImageIO.read(png);
        int width = img.getWidth();
        int height = img.getHeight();
        System.out.println(png.getParent() + ", " + png.getName() + " @ " + width + "x" + height);
    } catch (IOException e) {
        System.out.println("Bad image: " + png);
        e.printStackTrace();
    }

}

See Reading/Loading an Image and BufferedImage for more details...

Expansion

From here, I'd create a POJO (Plain Old Java Object) which would maintain a reference to the original PNG File and stored the width and height to make it easier to look up.

You would then add these objects to a TableModel of some kind and apply it to a JTable

Beware, it can take time to generate these properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM