为什么我得到的图片大小是0字节

cqcjcc123 2007-08-23 02:34:56
import java.io.*;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;

public class FileOperate {

public FileOperate() {

}
public static String getImageSize(String filePath,int width,int height)
throws Exception{

double Ratio=1.0;
File F = new File(filePath);
if (!F.isFile())
throw new Exception(F+" is not image file error in CreateThumbnail!");

File ThF = new File(filePath.substring(0,filePath.lastIndexOf("/")+1),filePath.substring(filePath.lastIndexOf("/")+1)+"_"+width);

BufferedImage Bi = ImageIO.read(F);
Image Itemp = Bi.getScaledInstance (width,height,Bi.SCALE_SMOOTH);


if ((Bi.getHeight()>height) || (Bi.getWidth()>width)){
double a = width;
double b = height;
double c = Bi.getHeight();
double d = Bi.getWidth();
if (a != 0 && b != 0 && (c/b)>(d/a))
Ratio = b/c;
else
Ratio = a/d;
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage)Itemp, filePath.substring(filePath.lastIndexOf("/")+1), ThF);
}catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage());
}
return (filePath + "_" + width);
}

public static void main(String[] args){

try{
String str = getImageSize("d:/cache/test.jpg",100,100);
System.out.println(str);
}catch(Exception e){
e.printStackTrace();
}
}

}
...全文
530 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdjray 2007-08-23
  • 打赏
  • 举报
回复
你的代码应该会抛错的
filePath.substring(filePath.lastIndexOf("/")+1)+"_"+width
为什么最后要加上"_" + width???
要是加上了为什么你的文件名最后没有"_"+width???

然后不知道为什么你的getImageSize方法是返回一个莫名其妙的字符串?
而且返回的总是传进去的100?
你真的是在获取图片的大小吗?
cqcjcc123 2007-08-23
  • 打赏
  • 举报
回复
有人能帮我看看吗,谢谢了

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧