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

cqcjcc123 cqcjcc123 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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();
}
}

}
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
cqcjcc123
Java SE 62,620 成员 307,251 内容