Java.Utils:圖像處理工具類

package com.bood.common.utils;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.DecimalFormat;

/**
 * 圖像處理工具類(本類完全可以使用,但是更加推薦ImageMagick +Jmagick </br>
 * 採用C++實現的一個類庫,提供了Java的Api,非常強大和高效)</br>
 * 而且要爲JVM分配較大的堆內存,否則內存溢出 </br>
 *
 * @author:bood
 * @since:2020/2/7
 */
public class ImageUtils {

    public ImageUtils() {
    }

    /**
     * 縮放圖片
     *
     * @param src   源文件
     * @param dest  目標文件
     * @param ratio 縮放比例,如 0.1,0.8,1.2,2.4
     * @throws IOException
     */
    public static void zoom(String src, String dest, double ratio) throws IOException {
        //獲取文件擴展名
        String suffix = src.substring(src.lastIndexOf(".") + 1);
        //讀入文件
        BufferedImage bi = ImageIO.read(new File(src));
        //計算目標文件寬度
        int targetWidth = Integer.parseInt(new DecimalFormat("0").format(bi.getWidth() * ratio));
        //計算目標文件高度
        int targetHeight = Integer.parseInt(new DecimalFormat("0").format(bi.getHeight() * ratio));
        //獲取BufferedImage讀入的圖片的一個縮放的版本
        Image image = bi.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
        //創建一張空白的緩存圖片
        BufferedImage target = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        //返回一張2D圖像
        Graphics g = target.createGraphics();
        //將BufferedImage讀入的圖片畫到上一步創建的對象上
        g.drawImage(image, 0, 0, null);
        //釋放
        g.dispose();
        //圖像寫入文件
        ImageIO.write(target, suffix, new File(dest));
    }

    /**
     * 切圖
     *
     * @param src    源文件
     * @param dest   目標文件
     * @param startX 起點x座標
     * @param startY 起點y座標
     * @param endX   結束點x座標
     * @param endY   結束點y座標
     * @throws IOException
     */
    public static void cut(String src, String dest, int startX, int startY, int endX, int endY) throws IOException {
        //獲取文件擴展名
        String suffix = src.substring(src.lastIndexOf(".") + 1);
        //讀入文件
        BufferedImage bi = ImageIO.read(new File(src));
        //計算寬度
        int width = Math.abs(startX - endX);
        //計算高度
        int height = Math.abs(startY - endY);
        BufferedImage target = bi.getSubimage(startX, startY, width, height);
        ImageIO.write(target, suffix, new File(dest));
    }

    /**
     * 旋轉圖片
     *
     * @param src     源文件
     * @param dest    目標文件
     * @param degree  旋轉角度
     * @param bgcolor 背景色,無背景色爲null
     * @throws IOException
     */
    public static void rotate(String src, String dest, int degree, Color bgcolor) throws IOException {
        BufferedImage image = ImageIO.read(new File(src));
        int iw = image.getWidth();// 原始圖象的寬度
        int ih = image.getHeight();// 原始圖象的高度
        int w = 0;
        int h = 0;
        int x = 0;
        int y = 0;
        degree = degree % 360;
        if (degree < 0)
            degree = 360 + degree;// 將角度轉換到0-360度之間
        double ang = Math.toRadians(degree);// 將角度轉爲弧度

        /**
         * 確定旋轉後的圖象的高度和寬度
         */

        if (degree == 180 || degree == 0 || degree == 360) {
            w = iw;
            h = ih;
        } else if (degree == 90 || degree == 270) {
            w = ih;
            h = iw;
        } else {
            double cosVal = Math.abs(Math.cos(ang));
            double sinVal = Math.abs(Math.sin(ang));
            w = (int) (sinVal * ih) + (int) (cosVal * iw);
            h = (int) (sinVal * iw) + (int) (cosVal * ih);
        }

        x = (w / 2) - (iw / 2);// 確定原點座標
        y = (h / 2) - (ih / 2);
        BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());
        Graphics2D gs = (Graphics2D) rotatedImage.getGraphics();
        if (bgcolor == null) {
            rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
        } else {
            gs.setColor(bgcolor);
            gs.fillRect(0, 0, w, h);// 以給定顏色繪製旋轉後圖片的背景
        }

        AffineTransform at = new AffineTransform();
        at.rotate(ang, w / 2, h / 2);// 旋轉圖象
        at.translate(x, y);
        AffineTransformOp op = new AffineTransformOp(at,
                AffineTransformOp.TYPE_BICUBIC);
        op.filter(image, rotatedImage);
        image = rotatedImage;

        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ImageOutputStream iamgeOut = ImageIO.createImageOutputStream(byteOut);

        ImageIO.write(image, "png", iamgeOut);
        InputStream is = new ByteArrayInputStream(byteOut.toByteArray());

        OutputStream os = new FileOutputStream(new File(dest));

        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        os.close();
        is.close();
        byteOut.close();
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章