Java圖片處理工具類

jopen 10年前發布 | 3K 次閱讀 Java

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

/** 
 * 圖片壓縮處理 
 *  
 * @author kuang hj 
 */  
public class ImgCompressUtils  
{  
    // 圖片對象  
    private Image img;  

    // 原圖的寬度  
    private int width;  

    // 原圖的高度  
    private int height;  

    private static BufferedImage image;  

    @SuppressWarnings("deprecation")  
    public static void main(String[] args)  
        throws Exception  
    {  
        System.out.println("開始:" + new Date().toLocaleString());  
        ImgCompressUtils imgCom = new ImgCompressUtils("D:\\jjj.jpg");  
        // 處理圖片得到 image  
        imgCom.resizeFix(80, 80);  
        // 輸出結果文件  
        outputNewFile("D:\\result.jpg", image);  
        System.out.println("結束:" + new Date().toLocaleString());  
    }  

    /** 
     * 構造函數 取到需要處理的圖片,得到寬 和 高 
     */  
    public ImgCompressUtils(String fileName)  
        throws IOException  
    {  
        File file = new File(fileName);// 讀入文件  
        img = ImageIO.read(file); // 構造Image對象  
        width = img.getWidth(null); // 得到源圖寬  
        height = img.getHeight(null); // 得到源圖長  
    }  

    /** 
     * 按照寬度還是高度進行壓縮 
     *  
     * @param destwidth int 最大寬度 
     * @param destheight int 最大高度 
     */  
    public void resizeFix(int destwidth, int destheight)  
        throws IOException  
    {  
        if (width / height > destwidth / destheight)  
        {  
            resizeByWidth(destwidth);  
        }  
        else  
        {  
            resizeByHeight(destheight);  
        }  
    }  

    /** 
     * 以寬度為基準,等比例放縮圖片 
     *  
     * @param destwidth int 新寬度 
     */  
    public void resizeByWidth(int destwidth)  
        throws IOException  
    {  
        int tmph = (int)(height * destwidth / width);  
        resize(destwidth, tmph);  
    }  

    /** 
     * 以高度為基準,等比例縮放圖片 
     *  
     * @param destheight int 新高度 
     */  
    public void resizeByHeight(int destheight)  
        throws IOException  
    {  
        int tmpw = (int)(width * destheight / height);  
        resize(tmpw, destheight);  
    }  

    /** 
     * 強制壓縮/放大圖片到固定的大小 
     *  
     * @param w int 新寬度 
     * @param h int 新高度 
     */  
    public BufferedImage resize(int destwidth, int destheight)  
    {  
        // SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優先級比速度高 生成的圖片質量比較好 但速度慢  
        image = new BufferedImage(destwidth, destheight, BufferedImage.TYPE_INT_RGB);  

        // 繪制縮小后的圖  
        image.getGraphics().drawImage(img, 0, 0, destwidth, destheight, null);  

        return image;  
    }  

    /** 
     * 新生成的壓縮文件 <一句話功能簡述> <功能詳細描述> 
     *  
     * @param path 
     * @param image 
     * @see [類、類#方法、類#成員] 
     */  
    public static void outputNewFile(String path, BufferedImage image)  
    {  
        try  
        {  
            File destFile = new File(path);  
            FileOutputStream out = new FileOutputStream(destFile); // 輸出到文件流  
            // 可以正常實現bmp、png、gif轉jpg  
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
            encoder.encode(image); // JPEG編碼  
            out.close();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
}  </pre> 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!