根据图片尺寸分类的小程序

2008-02-23 09:42:09来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

功能:将输入目录类所有的文件按照输入尺寸:长,宽进行筛选,生成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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:如何利用 Eclipse 工具来诊断客户问题

下一篇:基础扫盲篇 - J2EE的13种核心技术