Java图片压缩质量和压缩尺寸(支持jdk1.6,1.7不支持)

阅读更多
package com.xwtec.iocp.test;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date; 
import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; 

public class ImgCompress {

private Image img; 
    private int width; 
    private int height; 
    @SuppressWarnings("deprecation") 
    public static void main(String[] args) throws Exception { 
        System.out.println("开始:" + new Date().toLocaleString()); 
//        ImgCompress imgCom = new ImgCompress("C:\\temp\\123.jpg"); 
//        imgCom.resizeFix(300, 300); 
        saveMinPhoto("C:\\temp\\123.jpg", "C:\\temp\\123.jpg", 1000, 0.9d);
        System.out.println("结束:" + new Date().toLocaleString());
     
    } 
    /**
     * 构造函数
     */ 
    public ImgCompress(String fileName) throws IOException { 
        File file = new File(fileName);// 读入文件 
        img = ImageIO.read(file);      // 构造Image对象 
        width = img.getWidth(null);    // 得到源图宽 
        height = img.getHeight(null);  // 得到源图长 
    } 
    /**
     * 按照宽度还是高度进行压缩
     * @param w int 最大宽度
     * @param h int 最大高度
     */ 
    public void resizeFix(int w, int h) throws IOException { 
        if (width / height > w / h) { 
            resizeByWidth(w); 
        } else { 
            resizeByHeight(h); 
        } 
    } 
    /**
     * 以宽度为基准,等比例放缩图片
     * @param w int 新宽度
     */ 
    public void resizeByWidth(int w) throws IOException { 
        int h = (int) (height * w / width); 
        resize(w, h); 
    } 
    /**
     * 以高度为基准,等比例缩放图片
     * @param h int 新高度
     */ 
    public void resizeByHeight(int h) throws IOException { 
        int w = (int) (width * h / height); 
        resize(w, h); 
    } 
    /**
     * 强制压缩/放大图片到固定的大小
     * @param w int 新宽度
     * @param h int 新高度
     */ 
    public void resize(int w, int h) throws IOException { 
        // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 
        BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );  
        image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图 
        File destFile = new File("C:\\temp\\456.jpg"); 
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流 
        // 可以正常实现bmp、png、gif转jpg 
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
        encoder.encode(image); // JPEG编码 
        out.close(); 
    }
   
    /**
     * 等比例压缩算法:
     * 算法思想:根据压缩基数和压缩比来压缩原图,生产一张图片效果最接近原图的缩略图
     * @param srcURL 原图地址
     * @param deskURL 缩略图地址
     * @param comBase 压缩基数
     * @param scale 压缩限制(宽/高)比例  一般用1:
     * 当scale>=1,缩略图height=comBase,width按原图宽高比例;若scale<1,缩略图width=comBase,height按原图宽高比例
     * @throws Exception
     * @author shenbin
     * @createTime 2014-12-16
     * @lastModifyTime 2014-12-16
     */
    public static void saveMinPhoto(String srcURL, String deskURL, double comBase,
            double scale) throws Exception {
        File srcFile = new java.io.File(srcURL);
        Image src = ImageIO.read(srcFile);
        int srcHeight = src.getHeight(null);
        int srcWidth = src.getWidth(null);
        int deskHeight = 0;// 缩略图高
        int deskWidth = 0;// 缩略图宽
        double srcScale = (double) srcHeight / srcWidth;
        /**缩略图宽高算法*/
        if ((double) srcHeight > comBase || (double) srcWidth > comBase) {
            if (srcScale >= scale || 1 / srcScale > scale) {
                if (srcScale >= scale) {
                    deskHeight = (int) comBase;
                    deskWidth = srcWidth * deskHeight / srcHeight;
                } else {
                    deskWidth = (int) comBase;
                    deskHeight = srcHeight * deskWidth / srcWidth;
                }
            } else {
                if ((double) srcHeight > comBase) {
                    deskHeight = (int) comBase;
                    deskWidth = srcWidth * deskHeight / srcHeight;
                } else {
                    deskWidth = (int) comBase;
                    deskHeight = srcHeight * deskWidth / srcWidth;
                }
            }
        } else {
            deskHeight = srcHeight;
            deskWidth = srcWidth;
        }
        BufferedImage tag = new BufferedImage(deskWidth, deskHeight, BufferedImage.TYPE_3BYTE_BGR);
        tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null); //绘制缩小后的图
        FileOutputStream deskImage = new FileOutputStream(deskURL); //输出到文件流
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);
        encoder.encode(tag); //近JPEG编码
        deskImage.close();
    }

}
  • ImgCompress.zip (1.3 KB)
  • 下载次数: 0

你可能感兴趣的:(java)