功能:将输入目录类所有的文件按照输入尺寸:长,宽进行筛选,生成3个文件列表:尺寸大于输入,尺寸小于输入,分析失败。
代码如下:
/**
* 2005-05-19
*/
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.TruncatedFileException;
/**
*
* @author LEI
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ImgFilter {
/** Automatically generated javadoc for: INT_100 */
private static final int INT_100 = 100;
/** Automatically generated javadoc for: INT_600 */
private static final int INT_600 = 600;
/** Automatically generated javadoc for: INT_800 */
private static final int INT_800 = 800;
/**
*
*/
private static void error () {
System.out
.println("Too Few Argument./nImgFileter c:/images 800(width) 600(height)");
}
public static void main (String[] args) {
if (args.length < 3) {
error();
return;
}
ImgFilter bot = new ImgFilter();
bot.execute(args);
System.out.println("--------/n");
}
/**
* Check the input image size pixel(s)
*
* @param fp
* @param minWidth
* @param minHeight
* @return
*/
private int checkSize (String fp, int minWidth, int minHeight) {
InputStream in = null;
try {
BufferedImage mImage;
in = new FileInputStream(fp);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
mImage = decoder.decodeAsBufferedImage();
int height = mImage.getHeight();
int width = mImage.getWidth();
long minSqure = minWidth * minHeight;
long squre = width * height;
if (width < minWidth && height < minHeight) {
return 0;
}
if (squre < minSqure) {
return 0;
}
return 1;
} catch (TruncatedFileException ig1) {
// System.err.println(fp + ": " + imgx.toString());
return -1;
} catch (ImageFormatException ig2) {
return -1;
} catch (IOException ex) {
System.err.println(fp + ": " + ex.toString());
return -1;
} finally {
try {
in.close();
} catch (IOException ignored) {
}
}
}
/**
* Main entrance of the ImgFilter Class
*
* @param args
*/
private void execute (String[] args) {
String fd = args[0];
String extend = "jpg";
int width = INT_800, height = INT_600;
try {
width = Integer.parseInt(args[1]);
height = Integer.parseInt(args[2]);
} catch (Exception ignored) {
}
String[] lstFiles = retrieveDirList(fd, extend);
if (lstFiles == null) {
return;
}
int max = lstFiles.length;
List lstOk = new ArrayList();
List lstBad = new ArrayList();
List lstUn = new ArrayList();
for (int i = 0; i < lstFiles.length; i++) {
String fn = lstFiles[i];
String fp = fd + "/" + fn;
int rs = checkSize(fp, width, height);
if (rs == 1) {
lstOk.add(fn);
} else if (rs == 0) {
lstBad.add(fn);
} else {
lstUn.add(fn);
}
if (i % INT_100 == 0) {
System.out.println(i + "/" + max);
}
} // end for
System.out.println(max + "/" + max);
writeFile(fd + "/ok.lst", lstOk);
writeFile(fd + "/fail.lst", lstBad);
moveLstFile(fd + "/fail.lst", "small");
writeFile(fd + "/bad.lst", lstUn);
moveLstFile(fd + "/bad.lst", "bad");
}
/**
* generate move file list
*
* @param fp
* @param dirname
*/
private void moveLstFile (String fp, String dirname) {
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new FileReader(fp));
String line = br.readLine();
while (line != null) {
sb.append("move ").append(line).append(" ").append(dirname)
.append("// /r/n");
line = br.readLine();
}
this.writeFile(fp + ".cmd", sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// ignored
}
}
}
/**
* Retrieve the spec extend name file list
*
* @return
*/
private String[] retrieveDirList (String fd, String ext) {
FilenameFilter filter = new FilenameFilter() {
public boolean accept (File dir, String name) {
return name.endsWith(".jpg");
}
};
File dir = new File(fd);
String[] list = dir.list(filter);
return list;
}
/**
* write input list to file
*
* @param string
* @param lstUn
*/
private void writeFile (String string, List lst) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < lst.size(); i++) {
sb.append(lst.get(i)).append("/r/n");
}
writeFile(string, sb.toString());
}
/**
* Write input content to input fp
*
* @param fp
* @param content
*/
private void writeFile (String fp, String content) {
FileWriter fw = null;
try {
fw = new FileWriter(fp);
fw.write(content);
} catch (IOException e) {
System.err.println("Write:" + fp + "failed:" + e.toString());
} finally {
try {
fw.close();
} catch (IOException e1) {
}
}
}
} // eof
代码如下:
/**
* 2005-05-19
*/
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.TruncatedFileException;
/**
*
* @author LEI
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ImgFilter {
/** Automatically generated javadoc for: INT_100 */
private static final int INT_100 = 100;
/** Automatically generated javadoc for: INT_600 */
private static final int INT_600 = 600;
/** Automatically generated javadoc for: INT_800 */
private static final int INT_800 = 800;
/**
*
*/
private static void error () {
System.out
.println("Too Few Argument./nImgFileter c:/images 800(width) 600(height)");
}
public static void main (String[] args) {
if (args.length < 3) {
error();
return;
}
ImgFilter bot = new ImgFilter();
bot.execute(args);
System.out.println("--------/n");
}
/**
* Check the input image size pixel(s)
*
* @param fp
* @param minWidth
* @param minHeight
* @return
*/
private int checkSize (String fp, int minWidth, int minHeight) {
InputStream in = null;
try {
BufferedImage mImage;
in = new FileInputStream(fp);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
mImage = decoder.decodeAsBufferedImage();
int height = mImage.getHeight();
int width = mImage.getWidth();
long minSqure = minWidth * minHeight;
long squre = width * height;
if (width < minWidth && height < minHeight) {
return 0;
}
if (squre < minSqure) {
return 0;
}
return 1;
} catch (TruncatedFileException ig1) {
// System.err.println(fp + ": " + imgx.toString());
return -1;
} catch (ImageFormatException ig2) {
return -1;
} catch (IOException ex) {
System.err.println(fp + ": " + ex.toString());
return -1;
} finally {
try {
in.close();
} catch (IOException ignored) {
}
}
}
/**
* Main entrance of the ImgFilter Class
*
* @param args
*/
private void execute (String[] args) {
String fd = args[0];
String extend = "jpg";
int width = INT_800, height = INT_600;
try {
width = Integer.parseInt(args[1]);
height = Integer.parseInt(args[2]);
} catch (Exception ignored) {
}
String[] lstFiles = retrieveDirList(fd, extend);
if (lstFiles == null) {
return;
}
int max = lstFiles.length;
List lstOk = new ArrayList();
List lstBad = new ArrayList();
List lstUn = new ArrayList();
for (int i = 0; i < lstFiles.length; i++) {
String fn = lstFiles[i];
String fp = fd + "/" + fn;
int rs = checkSize(fp, width, height);
if (rs == 1) {
lstOk.add(fn);
} else if (rs == 0) {
lstBad.add(fn);
} else {
lstUn.add(fn);
}
if (i % INT_100 == 0) {
System.out.println(i + "/" + max);
}
} // end for
System.out.println(max + "/" + max);
writeFile(fd + "/ok.lst", lstOk);
writeFile(fd + "/fail.lst", lstBad);
moveLstFile(fd + "/fail.lst", "small");
writeFile(fd + "/bad.lst", lstUn);
moveLstFile(fd + "/bad.lst", "bad");
}
/**
* generate move file list
*
* @param fp
* @param dirname
*/
private void moveLstFile (String fp, String dirname) {
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new FileReader(fp));
String line = br.readLine();
while (line != null) {
sb.append("move ").append(line).append(" ").append(dirname)
.append("// /r/n");
line = br.readLine();
}
this.writeFile(fp + ".cmd", sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// ignored
}
}
}
/**
* Retrieve the spec extend name file list
*
* @return
*/
private String[] retrieveDirList (String fd, String ext) {
FilenameFilter filter = new FilenameFilter() {
public boolean accept (File dir, String name) {
return name.endsWith(".jpg");
}
};
File dir = new File(fd);
String[] list = dir.list(filter);
return list;
}
/**
* write input list to file
*
* @param string
* @param lstUn
*/
private void writeFile (String string, List lst) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < lst.size(); i++) {
sb.append(lst.get(i)).append("/r/n");
}
writeFile(string, sb.toString());
}
/**
* Write input content to input fp
*
* @param fp
* @param content
*/
private void writeFile (String fp, String content) {
FileWriter fw = null;
try {
fw = new FileWriter(fp);
fw.write(content);
} catch (IOException e) {
System.err.println("Write:" + fp + "failed:" + e.toString());
} finally {
try {
fw.close();
} catch (IOException e1) {
}
}
}
} // eof