java圖片處理工具類

package com.he.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtil {
    public static void main(String[] args) {
        String file = "C:\\apache-tomcat-7.0.54\\webapps\\xxt\\media\\20150414\\244\\y10Q8x_IeyZi7Q3oio2tQaNcs-g9fWFzqTXk49c5e068sfYMC8k6Pgc5s72s6yuS.jpg";
        try {
            ImageUtil.cutImage(file, 200);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static String addZoomStr(String str) {
        String fileName = str.substring(str.lastIndexOf('/') + 1);
        str = str.replace(fileName, "zoom" + fileName);
        return str;
    }

    /**
     * 圖片失真壓縮(按給定寬度、高度不變)
     * @param srcPath 圖片源
     * @param w 縮放後的寬
     * @return
     * @throws IOException
     */
    public static String zoomByWidth(String srcPath, int w)
            throws Exception {
        return zoom(srcPath, w, -1);
    }

    /**
     * 圖片失真壓縮(按給定高度、寬度不變)
     * @param srcPath 圖片源
     * @param h 縮放後的高
     * @return
     * @throws IOException
     */
    public static String zoomByHeight(String srcPath, int h)
            throws Exception {
        return zoom(srcPath, -1, h);
    }

    /**
     * 圖片失真壓縮
     * @param srcPath 原圖路徑
     * @param w 縮放後的寬
     * @param h 縮放後的高
     * @return
     * @throws IOException
     */
    public static String zoom(String srcPath, int w, int h) throws Exception {
        String end = srcPath.substring(srcPath.lastIndexOf(".") + 1);
        File file = new File(srcPath);// 構建原文件流
        // 創建新圖路徑
        String newPath = srcPath.substring(0, srcPath.lastIndexOf("."))
                + "zoom." + end;
        Image image = ImageIO.read(file);// 讀取圖片文件
        if (image.getWidth(null) <= w && image.getHeight(null) <= h) {
            return srcPath;
        }
        if (w < 0) {
            w = image.getWidth(null);
        }
        if (h < 0) {
            h = image.getHeight(null);
        }
        BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        /*
         * Image.SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優先級比速度高 生成的圖片質量比較好 但速度慢
         */
        tag.getGraphics().drawImage(
                image.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, null);
        ImageIO.write(tag, end, new File(newPath));// 輸出到文件流
        return newPath;
    }

    /**
     * 圖片按長邊等比例壓縮
     * @param srcPath 源文件
     * @param size 壓縮後長邊長度
     * @return
     * @throws Exception
     */
    public static String zoom(String srcPath, int size) throws Exception {
        int newWidth;
        int newHeight;
        String end = srcPath.substring(srcPath.lastIndexOf(".") + 1);
        File file = new File(srcPath);// 構建原文件流
        // 創建新圖路徑
        String newPath = srcPath.substring(0, srcPath.lastIndexOf("."))
                + "zoom." + end;
        Image image = ImageIO.read(file);// 讀取圖片文件
        if (image.getWidth(null) <= size && image.getHeight(null) <= size) {
            return srcPath;
        }
        // 爲等比縮放計算輸出的圖片寬度及高度
        double rate1 = ((double) image.getWidth(null)) / (double) size + 0.1;
        double rate2 = ((double) image.getHeight(null)) / (double) size + 0.1;
        // 根據縮放比率大的進行縮放控制
        double rate = rate1 > rate2 ? rate1 : rate2;
        newWidth = (int) (((double) image.getWidth(null)) / rate);
        newHeight = (int) (((double) image.getHeight(null)) / rate);
        BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight,
                BufferedImage.TYPE_INT_RGB);
        /*
         * Image.SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優先級比速度高 生成的圖片質量比較好 但速度慢
         */
        tag.getGraphics()
                .drawImage(
                        image.getScaledInstance(newWidth, newHeight,
                                Image.SCALE_SMOOTH), 0, 0, null);
        ImageIO.write(tag, end, new File(newPath));// 輸出到文件流
        return newPath;
    }

    /**
     * 原圖正方形壓縮(若爲非正方形圖片用白色背景填充圖片後壓縮)
     * @param srcPath 原圖路徑
     * @return
     * @throws Exception
     */
    public static String zoom(String srcPath, int size, String header)
            throws Exception {
        String end = srcPath.substring(srcPath.lastIndexOf(".") + 1);
        File file = new File(srcPath);// 構建原文件流
        // 創建新圖路徑
        String newPath = srcPath.substring(0, srcPath.lastIndexOf("."))
                + "zoom." + end;
        Image image = ImageIO.read(file);// 讀取圖片文件

        // 構造Image對象
        int old_w = image.getWidth(null); // 得到源圖寬
        int old_h = image.getHeight(null);
        // 圖片跟據長寬留白,成一個正方形圖。
        BufferedImage oldpic;
        if (old_w > old_h) {
            oldpic = new BufferedImage(old_w, old_w, BufferedImage.TYPE_INT_RGB);
        } else {
            if (old_w < old_h) {
                oldpic = new BufferedImage(old_h, old_h,
                        BufferedImage.TYPE_INT_RGB);
            } else {
                oldpic = new BufferedImage(old_w, old_h,
                        BufferedImage.TYPE_INT_RGB);
            }
        }
        // 白色背景填充圖片
        Graphics g = oldpic.createGraphics();
        g.setColor(Color.white);
        if (old_w > old_h) {
            g.fillRect(0, 0, old_w, old_w);
            g.drawImage(image, 0, (old_w - old_h) / 2, old_w, old_h,
                    Color.white, null);
        } else {
            if (old_w < old_h) {
                g.fillRect(0, 0, old_h, old_h);
                g.drawImage(image, (old_h - old_w) / 2, 0, old_w, old_h,
                        Color.white, null);
            } else {
                g.drawImage(image.getScaledInstance(old_w, old_h,
                        Image.SCALE_SMOOTH), 0, 0, null);
            }
        }
        g.dispose();
        image = oldpic;
        BufferedImage tag = new BufferedImage(size, size,
                BufferedImage.TYPE_INT_RGB);
        // 繪製縮小後的圖
        tag.getGraphics().drawImage(
                image.getScaledInstance(size, size, Image.SCALE_SMOOTH), 0, 0,
                null);
        ImageIO.write(tag, end, new File(newPath));// 輸出到文件流
        return newPath;
    }

    /**
     * 裁圖
     * @param srcPath 源文件
     * @param x X座標
     * @param y y座標
     * @param w 截圖的寬度
     * @param h 截圖的長度
     * @return 截取後的文件
     * @throws IOException
     */
    public static String cutImage(String srcPath, int x, int y, int w, int h)
            throws IOException {
        String end = srcPath.substring(srcPath.lastIndexOf(".") + 1);
        File file = new File(srcPath);// 構建原文件流
        // 創建新圖路徑
        String newPath = srcPath.substring(0, srcPath.lastIndexOf("."))
                + "small." + end;
        BufferedImage image = ImageIO.read(file);
        int maxW = image.getWidth();
        int maxH = image.getHeight();
        // 寬,高 越界處理,超過就以邊界爲止
        if ((y + h) > maxH) {
            h = maxH;
        }
        if ((x + w) > maxW) {
            w = maxW;
        }
        BufferedImage small = image.getSubimage(x, y, w, h);
        small.flush();
        ImageIO.write(small, end, new File(newPath));
        return newPath;
    }

    /**
     * 截取正方形圖片
     * @param srcPath 原圖路徑
     * @param size
     * @return
     * @throws Exception
     */
    public static String cutImage(String srcPath, int size) throws Exception {
        int new_w;
        int new_h;
        String end = srcPath.substring(srcPath.lastIndexOf(".") + 1);
        File file = new File(srcPath);// 構建原文件流
        // 創建新圖路徑
        String newPath = srcPath.substring(0, srcPath.lastIndexOf("."))
                + "_thumb." + end;
        Image image = ImageIO.read(file);// 讀取圖片文件
        int old_w = image.getWidth(null);
        int old_h = image.getHeight(null);
        BufferedImage tag = null;
        if (old_w > size || old_h > size) {
            // 爲等比縮放計算輸出的圖片寬度及高度
            double rate1 = ((double) old_w) / (double) size + 0.1;
            double rate2 = ((double) old_h) / (double) size + 0.1;
            // 根據縮放比率大的進行縮放控制
            double rate = rate1 > rate2 ? rate1 : rate2;
            new_w = (int) (((double) old_w) / rate);
            new_h = (int) (((double) old_h) / rate);
            tag = new BufferedImage((int) new_w, (int) new_h,
                    BufferedImage.TYPE_INT_RGB);
            /*
             * Image.SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優先級比速度高 生成的圖片質量比較好 但速度慢
             */
            tag.getGraphics().drawImage(
                    image.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH),
                    0, 0, null);
        } else {
            new_w = image.getWidth(null);
            new_h = image.getHeight(null);
            tag = new BufferedImage((int) new_w, (int) new_h,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(
                    image.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH),
                    0, 0, null);
        }
        int cut_x = 0, cut_y = 0, cut_w = 0, cut_h = 0;
        if (new_w > new_h) {
            cut_x = (new_w - new_h) / 2;
            cut_y = 0;
            cut_w = cut_h = new_h;
        } else {
            if (new_w < new_h) {
                cut_x = 0;
                cut_y = (new_h - new_w) / 2;
                cut_w = cut_h = new_w;
            } else {
                cut_x = 0;
                cut_y = 0;
                cut_w = cut_h = new_w;
                //return srcPath;
            }
        }
        BufferedImage small = tag.getSubimage(cut_x, cut_y, cut_w, cut_h);
        small.flush();
        ImageIO.write(small, end, new File(newPath));
        return newPath;
    }

    /**
     * 圖片壓縮
     * 1、若原圖尺寸小於給定尺寸則用白色填充
     * 2、若寬度 > 高度,按寬度壓縮圖片後,截取w:h的圖片,否則反之。
     * @param srcPath 原圖路徑
     * @param w 壓縮寬度
     * @param h 壓縮高度
     * @return
     * @throws Exception
     */
    public static String cutImage(String srcPath, int w, int h)
            throws Exception {
        int new_w;
        int new_h;
        String end = srcPath.substring(srcPath.lastIndexOf(".") + 1);
        File file = new File(srcPath);// 構建原文件流
        // 創建新圖路徑
        String newPath = srcPath.substring(0, srcPath.lastIndexOf("."))
                + "small." + end;
        Image image = ImageIO.read(file);// 讀取圖片文件
        int old_w = image.getWidth(null);
        int old_h = image.getHeight(null);
        double old_p = (double) old_w / (double) old_h;
        double new_p = (double) w / (double) h;
        BufferedImage tag = null;
        if (old_p >= new_p) {
            // 爲等比縮放計算輸出的圖片寬度及高度
            double rate = ((double) old_h) / (double) h;
            // 根據縮放比率大的進行縮放控制
            new_w = (int) (((double) old_w) / rate);
            new_h = (int) (((double) old_h) / rate);
        } else {
            // 爲等比縮放計算輸出的圖片寬度及高度
            double rate = ((double) old_w) / (double) w;
            // 根據縮放比率大的進行縮放控制
            new_w = (int) (((double) old_w) / rate);
            new_h = (int) (((double) old_h) / rate);
        }
        tag = new BufferedImage((int) new_w, (int) new_h,
                BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(
                image.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0,
                0, null);
        // 剪切
        int cut_x = 0, cut_y = 0, cut_w = 0, cut_h = 0;
        if (new_w >= w) {
            cut_x = (new_w - w) / 2;
        }
        if (new_h >= h) {
            cut_y = (new_h - h) / 2;
        }
        BufferedImage small = tag.getSubimage(cut_x, cut_y, w, h);
        small.flush();
        ImageIO.write(small, end, new File(newPath));
        return newPath;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章