0

I am working on a project and one of the requirements is to scan a folder containing different Images. I need to find a way to scan the folder and add each image to an array of Images. Here is what I want to do

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
4
  • 2
    You forgot to ask a specific question. Commented Mar 21, 2019 at 18:59
  • One Q. How do you want to scan it? With phone? Commented Mar 21, 2019 at 19:11
  • No not by phone, similar to using a scanner class to scan lines of sentences in a file but instead of scanning sentences I am scanning images. More like saying "save read image to an Image instance. Commented Mar 21, 2019 at 19:14
  • Do you need the actual pixel data, or do you just want to scan for image files, and work with those (ie. what do you want to do with the images later)? If you read an arbitrary number of images into memory at the same time, you are likely to run out of memory at some point. A more scalable solution would probably be to process one (or another fixed number) image at a time, then move onto the next. Commented Mar 22, 2019 at 9:00

1 Answer 1

1

Okay here is what I created:

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;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.