Reputation: 11
I'm still a novice Java programmer. I have downloaded some projects on the internet but I have some issue running one of the project. That project is about face recognition. It can successfully be compiled but when I want to load an image, the file pictures don't show in the JFileChooser.
I think the problem is at this part:
File folder = fc.getSelectedFile();
//System.out.println("1 "+folder);
FileFilter dirFilter = new FileFilter() {
public boolean accept(File pathname) {
return pathname.exists() && pathname.isDirectory();
}
};
FileFilter jpgFilter = new FileFilter() {
public boolean accept(File pathname) {
String filename = pathname.getName();
boolean jpgFile = (filename.toUpperCase().endsWith("JPG")
|| filename.toUpperCase().endsWith("JPEG"));
return pathname.exists() && pathname.isFile() && jpgFile;
}
};
File[] folders = folder.listFiles(dirFilter);
//System.out.println("2 "+folders);
trainingSet.clear();
faceBrowser.empty();
for (int i = 0; i < folders.length; i++) { //For each folder in the training set directory
File[] files = folders[i].listFiles(jpgFilter);
System.out.println("3 " + files);
for (int j = 0; j < files.length; j++) {
trainingSet.add(files[j]);
}
}
File[] files = trainingSet.toArray(new File[1]);
jlist.setListData(files);
//there is no image files in the folderwai
//System.out.println(files);
for (int i = 0; i < files.length; i++) {
//System.out.println(files[0]);
Face f = new Face(files[i]);
f.description = "Face image in database.";
f.classification = files[i].getParentFile().getName();
faceBrowser.addFace(f);
faces.add(f);
}
jlStatus.setIndeterminate(false);
jlStatus.setString(files.length + " files loaded from " + folders.length + " folders.");
jlStatus.paintImmediately(jlStatus.getVisibleRect());
jspFaceBrowser.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
main.invalidate();
jbTrain.setEnabled(true);
jbCropImage.setEnabled(true);
}
Upvotes: 0
Views: 144
Reputation: 385
This only supports files that end with the extensions .jpg
or .jpeg
. If the file you are loading ends with a .png
or some other extension, you'll have to convert it to .jpg
using a converter, probably an online one like png2jpg.com. Once you've converted it, it should show up in the JFileChooser
.
This behavior is declared on this line:
boolean jpgFile = (filename.toUpperCase().endsWith("JPG") || filename.toUpperCase().endsWith("JPEG"));
You could change "JPG"
to something else, but, just to be sure, I'd leave it alone until you become a little more at-home with messing around in programming.
Upvotes: 1