[英]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)
(以尽可能少的代码行)
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#listFiles
和FileFilter
。
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
。
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.