Picture compression

package com.ledong.util;  

import java.io. *;  
import java.util.Date;  
import java.awt.*;  
import java.awt.image.*;  
import javax.imageio.ImageIO;  
import com.sun.image.codec.jpeg.*;  

/**
 * Image compression processing
 */  
public class PictureCompress {
    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());  
        PictureCompress imgCom = new PictureCompress("C:\\temp\\pic123.jpg");  
        //imgCom.resizeFix(150,150);  
        imgCom.resizeFix(720,720);  
        System.out.println("结束:" + new Date().toLocaleString());  
    }  
    
    /**
     * Constructor
     */  
    public PictureCompress(String fileName) throws IOException {  
        File file = new File(fileName);// read the file  
        img = ImageIO.read(file); // Construct an Image object  
        width = img.getWidth(null); // get the width of the source image  
        height = img.getHeight(null); // Get the source image length  
    }  
    
    /**
     * Compress by width or height
     * @param w int max width
     * @param h int max height
     */  
    public void resizeFix(int w, int h) throws IOException {  
        if (width / height > w / h) {  
            resizeByWidth(w);  
        } else {  
            resizeByHeight(h);  
        }  
    }  
    
    /**
     * Based on the width, scale the image proportionally
     * @param w int new width
     */  
    public void resizeByWidth(int w) throws IOException {  
        int h = (int) (height * w / width);  
        resize(w, h);  
    }  
    
    /**
     * Based on height, scale the picture in equal proportion
     * @param h int new height
     */  
    public void resizeByHeight(int h) throws IOException {  
        int w = (int) (width * h / height);  
        resize(w, h);  
    }  
    
    /**
     * Force compress/enlarge image to fixed size
     * @param w int new width
     * @param h int new height
     */  
    public void resize(int w, int h) throws IOException {  
        // The thumbnail algorithm of SCALE_SMOOTH generates the priority of the smoothness of the thumbnail image is higher than the speed. The quality of the generated image is better but the speed is slow  
        BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);   
        image.getGraphics().drawImage(img, 0, 0, w, h, null); // draw the reduced image  
        File destFile = new File("C:\\temp\\456.jpg");  
        FileOutputStream out = new FileOutputStream(destFile); // output to file stream  
        // Can normally achieve bmp, png, gif to jpg  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        encoder.encode(image); // JPEG编码  
            out.close();  
        }  
 }  

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326609913&siteId=291194637