如何扫描图像文件夹并将它们添加到图像数组

How to scan an image folder and add them to an image array

我正在处理一个项目,其中一项要求是扫描包含不同图像的文件夹。我需要找到一种方法来扫描文件夹并将每个图像添加到图像数组中。这是我想做的

public class ImageScan {
   private ArrayList<Image> images = new ArrayList<>(); 

   public void loadImages() {

   ArrayList<Image> image_Array = new ArrayList<>();

   File file = new File("data/images");         
   BufferedImage image = ImageIO.read(file);

      while(image.hasNextImage()) {//hasnextImage() is  not a valid method, 
      //just to express my idea.

         Image image = //save read Image to an image instance
         //here all I want to do is add each image I obtain into an 
         //arrayList of images      

         image_Array.add(image);        
      }//end of while       

   this.setImages(image_Array);// i set image_Array using getter method 
   }//end of loadData method

}//end of class

好的,这是我创建的:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class ImageScan {

    private List<Image> images = new ArrayList<Image>();

    public void loadImages() {

        List<Image> imageArray = new ArrayList<Image>();

        File file = new File("data/images");

        File[] imageFiles = file.listFiles(); // This gets all of the files inside 
'file', if 'file' is a folder
        for (File f : imageFiles) {
            try {
                BufferedImage image = ImageIO.read(f);
                imageArray.add(image);
            } catch (Exception e) {
                // This makes sure only the images in the folder are used, not any 
file.
            }
        }

        this.setImages(imageArray);
    }

    public void setImages(List<Image> imageArray) {
        images = imageArray;
    }

    public List<Image> getImages() {
        return images;
    }

}