根据图片尺寸分类的小程序
2008-02-23 09:42:09来源:互联网 阅读 ()
代码如下:
/**
* 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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash