zoukankan      html  css  js  c++  java
  • 图片压缩

    /**
    * ��������ͼ��
    */
    package utils;

    /**
    * @author advance
    *
    */
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.image.BufferedImage;
    import java.io.File;

    public class ThumbnailGenerator {
    public static void main(String[] args) throws Exception {
    transform("C:\Users\Administrator\vspider\files\20190111\image\jpg\0a08179283e02baefc529affd2dd30fe.jpg","C:\Users\Administrator\Desktop\新建文件夹 (2)\aaa.jpg",200,300,1000);
    }

    public static void transform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight, int quality) throws Exception
    {
    Image image = javax.imageio.ImageIO.read(new File(originalFile));

    double thumbRatio = (double)thumbWidth / (double)thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double)imageWidth / (double)imageHeight;
    if (thumbRatio < imageRatio)
    {
    thumbHeight = (int)(thumbWidth / imageRatio);
    }
    else
    {
    thumbWidth = (int)(thumbHeight * imageRatio);
    }

    if(imageWidth < thumbWidth && imageHeight < thumbHeight)
    {
    thumbWidth = imageWidth;
    thumbHeight = imageHeight;
    }
    else if(imageWidth < thumbWidth)
    thumbWidth = imageWidth;
    else if(imageHeight < thumbHeight)
    thumbHeight = imageHeight;

    BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setBackground(Color.WHITE);
    graphics2D.setPaint(Color.WHITE);
    graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    javax.imageio.ImageIO.write(thumbImage, "JPG", new File(thumbnailFile));
    }
    }

  • 相关阅读:
    css代码中position的定位,baidu+总结
    ibatis_HelloWorld
    v7系统,任务栏的开始图标和其他图标重合问题
    递归方法:输入一个多位整数,计算出从0到该数1出现的个数。
    解决JS:window.close()在Firefox下的不能关闭的问题
    Programming in the MidFuture(转)
    修改blog问题
    面向数据库的高级语言
    F#试用感受
    基于.net的数学编程语言
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10255537.html
Copyright © 2011-2022 走看看