分享
 
 
 

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

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有