【问题标题】:How to scan an image folder and add them to an image array如何扫描图像文件夹并将它们添加到图像阵列
【发布时间】:2019-03-21 18:56:58
【问题描述】:

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

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

【问题讨论】:

  • 你忘了问一个具体的问题。
  • 一个 Q. 你想怎么扫描它?有手机吗?
  • 不,不是通过电话,类似于使用扫描器类扫描文件中的句子行,但不是扫描句子,而是扫描图像。更像是说“将读取的图像保存到 Image 实例。
  • 您需要实际的像素数据,还是只想扫描图像文件,然后处理这些(即,您想对图像做什么?之后)?如果您同时将任意数量的图像读入内存,您可能会在某个时候耗尽内存。一个更具可扩展性的解决方案可能是一次处理一个(或另一个固定数量的)图像,然后移动到下一个。

标签: java image arraylist file-io javax.imageio


【解决方案1】:

好的,这是我创建的:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多