为什么我得到的图片大小是0字节
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();
}
}
}
...全文
相关推荐
基于JavaScript获取base64图片大小
base64原理 Base64编码要求把3个8位字节(38=24)转化为4个6位的字节(46=24),之后在6位的前面补两个0,形成8位一个字节的形式。 如果剩下的字符不足3个字节,则用0填充,输出字符使用’=’,因此编码后输出的文本末尾可能会出现1或2个’=’ 如何获取base64图片大小 通过base64编码原理我们知道,base64的图片字符流中的每8个字符就有两个是用0补充,而且字符流的末尾还可能存在‘=’号,我们可以通过这个原理计算图片的文件流大小。 getImgByteSize(data) { if (data.graphicContents) { // 获取base64
c#检测图片大小,类型
采用递归方式,检测所有文件夹下图片大小。采用文件头二进制方法判断图片类型
Web(前端) input标签 上传图片支持预览,计算图片大小 源码
上传图片支持预览,计算图片大小
纯javascript实现上传图片前图片预览,判断图片大小
纯javascript实现上传图片前图片预览,判断图片大小,兼容ie-7到10 firefox chrome,非常不错,分享给大家。
JS上传图片大小验证.rar
上传图片大小验证方法 非完整控件 如需要可以加入正则表达式来判断上传文件扩展名


Java SE
