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();
}
}}
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();
}
}}
filePath.substring(filePath.lastIndexOf("/")+1)+"_"+width
为什么最后要加上"_" + width???
要是加上了为什么你的文件名最后没有"_"+width???然后不知道为什么你的getImageSize方法是返回一个莫名其妙的字符串?
而且返回的总是传进去的100?
你真的是在获取图片的大小吗?