Here is the code for extract RGb values from a single image.... and now i need to read set of images from a folder , can anyone pls give me idea for that since im very new to java ,,thanks in advance :)

import java.io.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class GetPixelColor
{
  public static void main(String args[]) throws IOException{
    File file= new File("lena.jpg");
    BufferedImage image = ImageIO.read(file);
int i,j;
int w=800;
int h=800;
int [][] red=new int [h][w];
int [][] green=new int [h][w];
int [][] blue=new int [h][w];

    0 
for( i=0;i<w;i++)
{
for( j=0;j<h;j++)
{    
int clr=  image.getRGB(i,j); 
  red[i][j]   = (clr & 0x00ff0000) >> 16;
    green[i][j] = (clr & 0x0000ff00) >> 8;
    blue [i][j]=  clr & 0x000000ff;
}
}
for(i=0;i<w;i++){
for(j=0;j<h;j++)
{
    System.out.println("Red Color value = "+ red[i][j]);
    System.out.println("Green Color value = "+ green[i][j]);
    System.out.println("Blue Color value = "+ blue[i][j]);
  }
}
}
}

Edited 5 Years Ago by __avd: Added [code] tags.

Pass a folder name (relative or absolute) to your application using the command line arguments/some other means, create a file object for that folder, invoke the "listFiles" method on that File object which returns a list of all the files (and directories) in that directory, traverse these files and do what you have been doing till now.

final File dir = new File("some/dir");
for(final File imgFile : dir.listFies()) {
  // assuming the directory contains only images
  doSomethingWithImgFile(imgFile);
}

If your directory doesn't contain only image files, look into the "listFiles" method which accepts a filter. Read Javadoc for more details.

you can use this for individual files in a folder

final ImageFilter imageFilter = new ImageFilter();
      final File dir = new File("some/dir");
      for(final File imgFile : dir.listFies()) {
          if(imageFilter.accept(imgFile)){
              doSomethingWithImgFile(imgFile);
          }
      }



class ImageFilter{
    String GIF = "gif";
    String PNG = "png";
    String JPG = "jpg";
    String BMP = "bmp";
    String JPEG = "jpeg";

    public boolean accept(File file) {
        if(file != null) {
            if(file.isDirectory())
                return false;
            String extension = getExtension(file);
            if(extension != null && isSupported(extension))
                return true;
        }
        return false;
    }

    private String getExtension(File file) {
        if(file != null) {
            String filename = file.getName();
            int dot = filename.lastIndexOf('.');
            if(dot > 0 && dot < filename.length()-1)
                return filename.substring(dot+1).toLowerCase();
        }
        return null;
    }

    private boolean isSupported(String ext) {
        return ext.equalsIgnoreCase(GIF) || ext.equalsIgnoreCase(PNG) ||
                ext.equalsIgnoreCase(JPG) || ext.equalsIgnoreCase(BMP) ||
                ext.equalsIgnoreCase(JPEG);
    }
}
This article has been dead for over six months. Start a new discussion instead.