Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>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...)</em></p> <h1>Loading all the Images from a Folder</h1> <pre><code>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 } } } } } </code></pre> <h1>APIs Used</h1> <p>This is relatively simple to do and uses only standard JDK-packaged classes:</p> <ul> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/io/File.html"><code>File</code></a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html"><code>FilenameFilter</code></a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html"><code>BufferedImage</code></a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html"><code>ImageIO</code></a></li> </ul> <p>These sessions of the <a href="http://docs.oracle.com/javase/tutorial">Java Tutorial</a> might help you as well:</p> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html">Reading/Loading an Image</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html">How to Use File Choosers</a></li> </ul> <h1>Possible Enhancements</h1> <ul> <li>Use Apache Commons <code>FilenameUtils</code> to extract files' extensions</li> <li>Detect files based on actual mime-types or content, not based on extensions</li> <li>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: <ul> <li>Look at a <code>FileChooser</code> to select the folder.</li> <li>I assume you already know how to make frames/windows/dialogs.</li> <li>Read the <a href="http://docs.oracle.com/javase/tutorial">Java Tutorial</a> <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons</a> sections, which teaches you how to display and label them.</li> </ul></li> <li>I left out some issues to be dealt with: <ul> <li>Exception handling</li> <li>Folders with evil endigs (say you have a folder "TryMeIAmEvil.png")</li> </ul></li> </ul> <p>By combining all of the above, it's pretty easy to do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload