import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class pic
{
public static void main(String[] args) throws IOException
{
File file = new File("d:/temp/3.jpg");
String newurl="d:/temp/4.jpg"; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize = 100;
int old_w = src.getWidth(null); //得到源图宽
int old_h = src.getHeight(null);
int new_w = 0;
int new_h = 0; //得到源图长
int tempsize;
float tempdouble;
if(old_w>old_h)
{
tempdouble=old_w/tagsize;
}
else
{
tempdouble=old_h/tagsize;
}
new_w = Math.round(old_w/tempdouble);
new_h = Math.round(old_h/tempdouble); //计算新图长宽
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
FileOutputStream newimage = new FileOutputStream(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。