简体   繁体   English

如何从Java中的文件夹目录中获取png图像名称,路径,高度和宽度?

[英]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. 我正在尝试编写一个程序,该程序通过GUI提示用户一个充满其他文件夹,png图像或其他类型文件的文件夹。 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. 我想先用唯一的图像名称,路径,高度和宽度(按此顺序)填充jTable,然后再继续执行我现在不会涉及的其他比较操作。

I tried using jFileChooser to find the folder the user wants. 我尝试使用jFileChooser查找用户想要的文件夹。 How the thing is going to find and filter for .png files eludes me at this time. 目前,我不知道该如何查找和过滤.png文件。 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. 我尝试使用for每个循环从jFileChooser.getSelectedFiles()循环遍历给定的文件数组,但是我无法通过理解将文件区分为png图像。

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. 我以为以后需要使用文件数据来填充高度和宽度列表,因此我创建了一个File对象数组来容纳这些对象。 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. 我还想要一个具有.png扩展名的String文件名的过滤列表,但是事实证明,实现这一点对我来说很难理解。

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? 您将如何查找指定目录中的所有png图像文件(不需要其他文件类型),获取它们的名称,路径,高度和宽度,最后用这些数据点填充jTable? (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... 有关更多详细信息,请参见File#listFilesFileFilter

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... 有关更多详细信息,请参见读取/加载图像BufferedImage

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. 从这里,我将创建一个POJO(普通的Java旧对象),该对象将保留对原始PNG File的引用,并存储其宽度和高度以使其更易于查找。

You would then add these objects to a TableModel of some kind and apply it to a JTable 然后,您可以将这些对象添加到某种TableModel并将其应用于JTable

Beware, it can take time to generate these properties 注意,生成这些属性可能需要一些时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 中从具有透明度的 PNG 创建图像蒙版? - How do I create an image mask from a PNG with transparency in Java? 如何获取包含图像或视频的文件夹的路径 - How to get paths of folders that has image or video 如何使用jfilechooser从Java中的文件名数组中获取多个文件选择的绝对路径 - How do you get the absolute paths for multiple file selections using jfilechooser from an array of file names in java 我如何获取Java目录中所有文件和文件夹的列表 - How would i get a list of all files and folders in a directory in java 如何使用jframe制作一些特定的宽度和高度以及一些自动-Java - how to make some specific widths and heights and some auto with jframes - Java 如何使用java在现有的png图像上绘制矩形 - How do I draw a rectangle on an existing png image using java 如何使用Java在png图像上创建网格图? - How do i create a grid map on a png image with java? Java,Dropbox V2:仅获取文件夹的名称和路径,而不获取文件。 - Java, Dropbox V2 : Get only names and paths of folders, not files. 如何从不同文件夹的FileDialog中获取绝对路径 - How to get absolute paths from FileDialog from different folders 如何从Java中的图像网格中获取一个图像? - How do i get one image from a grid of images in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM