package com.lingtu.model;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Vector;
import javax.imageio.ImageIO;
//http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4826548
public class IconResp {
public static String[] iconArray = new String[500];
public static Vector iconvec = new Vector();
public static void main(String[] args) {
// TODO Auto-generated method stub
new IconResp().getAllIcon();
}
public Vector getAllIcon(){
if (iconvec.size()==0) {
setIconVec();
}
for (int i = 0; i < iconvec.size(); i++) {
System.out.println(iconvec.get(i).toString());
}
return iconvec;
}
public void resetIconResp(){
iconvec.clear();
setIconVec();
}
public String[] getAllIconOnly1Level(){
if (iconArray[0]==null){
setIconArrayOnly1Level();
}
for (int i = 0; i < iconArray.length; i++) {
System.out.println(i+":"+iconArray[i]);
}
return iconArray;
}
public void setIconArrayOnly1Level(){
File f = new File(Const.ICON_RESP_PATH);
if (f.exists()&&f.isDirectory()) {
for (int i = 0; i < f.listFiles().length; i++) {
if (f.listFiles()[i].isFile()) {
String lowerExtName = f.listFiles()[i].getName();
lowerExtName=lowerExtName.substring(lowerExtName.length()-4, lowerExtName.length())
.toLowerCase();
if(".jpg.gif.png".indexOf(lowerExtName)!=-1)
iconArray[i] = f.listFiles()[i].getName() + "," + getWH(Const.ICON_RESP_PATH + File.separator + f.listFiles()[i].getName());
}
}
}
}
public void setIconVec(){
File f = new File(Const.ICON_RESP_PATH);
if (f.exists()&&f.isDirectory()) {
for (int i = 0; i < f.listFiles().length; i++) {
if (f.listFiles()[i].isFile()) {
String lowerExtName = f.listFiles()[i].getName();
lowerExtName=lowerExtName.substring(lowerExtName.length()-4, lowerExtName.length())
.toLowerCase();
if(Const.ICON_IMAGE_TYPE.indexOf(lowerExtName)!=-1)
iconvec.add(Const.ICON_ROOT_NAME + "," + "," + f.listFiles()[i].getName() + "," + getWH(Const.ICON_RESP_PATH + File.separator + f.listFiles()[i].getName()));
}else if(f.listFiles()[i].isDirectory()){
File f2 = f.listFiles()[i];
for (int j = 0; j < f2.listFiles().length; j++) {
if (f2.listFiles()[j].isFile()) {
String lowerExtName = f2.listFiles()[j].getName();
lowerExtName=lowerExtName.substring(lowerExtName.length()-4, lowerExtName.length())
.toLowerCase();
if(Const.ICON_IMAGE_TYPE.indexOf(lowerExtName)!=-1)
iconvec.add(f2.getName()+","+ f2.getName() + "," + f2.listFiles()[j].getName() + "," + getWH(Const.ICON_RESP_PATH + File.separator + f2.getName()+ File.separator + f2.listFiles()[j].getName()));
}
}
}
}
}
}
//得到形如 "width,height" 的字符串
public String getWH ( String strFileName){
//System.out.println(strFileName);
int iPicWidth = 0;
int iPicHeight = 0;
try {
strFileName = strFileName.replaceAll("\\\\", "\\\\\\\\");
File fPic = new File( strFileName);
InputStream in = new FileInputStream(fPic);
BufferedImage sourceImg = null;
if(in != null){
try {
sourceImg = ImageIO.read(in);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if(sourceImg != null){
iPicWidth = sourceImg.getWidth();
iPicHeight = sourceImg.getHeight();
return iPicWidth + "," + iPicHeight;
}else{
System.out.println(strFileName+ "sourceImg is null!");
}
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在!" + strFileName);
} catch (Exception e) {
return "1,1";
}
return "1,1";
}
}
java bug about ImageIo
最新推荐文章于 2021-11-21 21:32:19 发布