上传图片压缩工具类

来源:互联网 发布:阿里云80端口是电信 编辑:程序博客网 时间:2024/06/07 18:42
package util;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImageUtils {public static double DEFAULT_SMALL_IMG_WIDTH = 167.0D;  public static double DEFAULT_BIG_IMG_WIDTH = 1000.0D;  public static double SCALE = 1.0D;  public static void main(String[] args)    throws Exception  {    File file = new File("E://94.JPG");    InputStream stream = new FileInputStream(file);  }  public static void drawMiniImage(InputStream stream, String deskURL, double comHeight, double comWidth)    throws Exception  {    Image src = ImageIO.read(stream);    int srcHeight = src.getHeight(null);    int srcWidth = src.getWidth(null);    int deskHeight = 0;    int deskWidth = 0;    double srcScale = srcHeight / srcWidth;    double comScale = comHeight / comWidth;    if ((comHeight <= 0.0D) || (comWidth <= 0.0D)) {      deskHeight = srcHeight;      deskWidth = srcWidth;    } else if (srcScale > comScale) {      deskHeight = (int)comHeight;      deskWidth = srcWidth * deskHeight / srcHeight;    } else {      deskWidth = (int)comWidth;      deskHeight = srcHeight * deskWidth / srcWidth;      if (deskHeight > comHeight) {        deskHeight = (int)comHeight;        deskWidth = srcWidth * deskHeight / srcHeight;      }    }    BufferedImage tag = new BufferedImage(deskWidth, deskHeight,       5);    tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null);    FileOutputStream deskImage = new FileOutputStream(deskURL);    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);    encoder.encode(tag);    deskImage.close();    stream.close();  }}

0 0
原创粉丝点击