加载并显示文件夹中的所有图像 [英] Load and display all the images from a folder
问题描述
我想使用Java读取文件夹中的所有图像。
I want to read all the images in a folder using Java.
何时:我按下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.
如何继续?
我有代码阅读图像以及文件夹中的所有图像,但我上面讲的内容是如何完成的?
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!
推荐答案
未经测试,因为没有安装JDK的机器,所以忍受我,那是所有输入按原样,但应该让你开始(预计会有大量的downvotes ......)
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
}
}
}
}
}
使用的API
这样做和使用相对简单只有标准的JDK打包类:
APIs Used
This is relatively simple to do and uses only standard JDK-packaged classes:
File
FilenameFilter
BufferedImage
ImageIO
Java教程也可以为您提供帮助:
These sessions of the Java Tutorial might help you as well:
- Reading/Loading an Image
- How to Use Icons
- How to Use File Choosers
- 使用Apache Commons
FilenameUtils
提取文件的扩展名 - 根据实际的mime类型或内容检测文件,而不是基于扩展名
- 我将UI代码留给您。由于我不知道这是否是家庭作业,我不想提供完整的解决方案。但要继续:
- 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.
- 异常处理
- 带有邪恶忍耐的文件夹(假设你有一个文件夹TryMeIAmEvil.png)
综合以上所有内容,这很容易做到。
By combining all of the above, it's pretty easy to do.
这篇关于加载并显示文件夹中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Look at a
- Use Apache Commons