圖片上傳壓縮處理

今天遇到一個問題,就是圖片壓縮須要進行處理。java

 

我呢,在網上直接找了一個組件:git

thumbnailator

 

okgithub

 

直接上工具類:工具

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.List;

/**
 * @author hmb
 * @des 圖片工具處理類
 */
@Slf4j
public class ImageUtil {

    /**
     * 定義容許上傳的圖片格式
     */
    private static List<String> imgTypes = Lists.newArrayList("gif", "jpeg", "jpg", "png", "bmp");

    /**
     * 檢查格式是否合法
     *
     * @param imageType
     * @return
     */
    public static boolean checkType(String imageType) {
        boolean flag = false;
        if (imgTypes.contains(imageType.toLowerCase())) {
            flag = true;
        }
        return flag;
    }

    /**
     * 根據圖片對象獲取對應InputStream
     *
     * @param image
     * @param readImageFormat
     * @return
     * @throws IOException
     */
    public static InputStream getInputStream(BufferedImage image, String readImageFormat) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, readImageFormat, os);
        InputStream is = new ByteArrayInputStream(os.toByteArray());
        os.close();
        return is;
    }

    /**
     * 指定大小 壓縮圖片
     * 默認輸出50%質量圖片
     *
     * @param in      輸出流
     * @param width   圖片長度
     * @param height  圖片寬度
     * @param quality 圖片壓縮質量 範圍:0.0~1.0,1爲最高質量
     * @param scale  按照比例進行縮放 0.0~1.0
     * @return
     * @throws IOException
     */
    public static InputStream compress(InputStream in, Integer width, Integer height, Float quality,Double scale) throws IOException {
        if (in == null) {
            return null;
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Thumbnails.Builder builder=Thumbnails.of(in);
        if(width!=null && height!=null){
            builder.size(width.intValue(),height.intValue());
        }

        if(scale!=null){
            builder.scale(scale.doubleValue());
        }
        if(quality!=null){
            builder.outputQuality(quality.floatValue());
        }
        builder.toOutputStream(os);
     builder.keepAspectRatio(false);//嚴格按寬高輸出  InputStream is
= new ByteArrayInputStream(os.toByteArray()); in.close(); os.close(); return is; } public static void inputstreamToFile(InputStream ins,File file) throws IOException{ OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } /** * 指定大小進行縮放 若圖片橫比width小,高比height小,不變 若圖片橫比width小,高比height大,高縮小到height,圖片比例不變 * 若圖片橫比width大,高比height小,橫縮小到width,圖片比例不變 * 若圖片橫比width大,高比height大,圖片按比例縮小,橫爲width或高爲height * * @param source 輸入源 * @param output 輸出源 * @param width 寬 * @param height 高 * @throws IOException */ public static void imgThumb(String source, String output, int width, int height, float quality) throws IOException { Thumbnails.of(source).size(width, height).outputQuality(quality).outputFormat("jpg").toFile(output); } /** * 指定大小進行縮放 * * @param source 輸入源 * @param output 輸出源 * @param width 寬 * @param height 高 * @throws IOException */ public static void imgThumb(File source, String output, int width, int height, float quality) throws IOException { Thumbnails.of(source).size(width, height).outputQuality(quality).outputFormat("jpg").toFile(output); } /** * 按照比例進行縮放 * * @param source 輸入源 * @param output 輸出源 * @throws IOException */ public static void imgScale(String source, String output, double scale) throws IOException { Thumbnails.of(source).scale(scale).outputFormat("jpg").toFile(output); } public static void imgScale(File source, String output, double scale) throws IOException { Thumbnails.of(source).scale(scale).outputFormat("jpg").toFile(output); } /** * 不按照比例,指定大小進行縮放 * * @param source 輸入源 * @param output 輸出源 * @param width 寬 * @param height 高 * @param keepAspectRatio 默認是按照比例縮放的,值爲false 時不按比例縮放 */ public static void imgNoScale(String source, String output, int width, int height, boolean keepAspectRatio) throws IOException { Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).outputFormat("jpg").toFile(output); } public static void imgNoScale(File source, String output, int width, int height, boolean keepAspectRatio) throws IOException { Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).outputFormat("jpg").toFile(output); } /** * 水印 * * @param source 輸入源 * @param output 輸入源 * @param width 寬 * @param height 高 * @param position 水印位置 Positions.BOTTOM_RIGHT o.5f * @param watermark 水印圖片地址 * @param transparency 透明度 0.5f * @param quality 圖片質量 0.8f */ public static void imgWatermark(String source, String output, int width, int height, Position position, String watermark, float transparency, float quality) throws IOException { Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency) .outputQuality(0.8f).outputFormat("jpg").toFile(output); } public static void imgWatermark(File source, String output, int width, int height, Position position, String watermark, float transparency, float quality) throws IOException { Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency) .outputQuality(0.8f).outputFormat("jpg").toFile(output); } /** * 裁剪圖片 * * @param source 輸入源 * @param output 輸出源 * @param position 裁剪位置 * @param x 裁剪區域x * @param y 裁剪區域y * @param width 寬 * @param height 高 * @param keepAspectRatio 默認是按照比例縮放的,值爲false 時不按比例縮放 */ public static void imgSourceRegion(String source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) throws IOException { Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio) .outputFormat("jpg").toFile(output); } public static void imgSourceRegion(File source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) throws IOException { Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio) .outputFormat("jpg").toFile(output); } /** * 按座標裁剪 * * @param source 輸入源 * @param output 輸出源 * @param x 起始x座標 * @param y 起始y座標 * @param x1 結束x座標 * @param y1 結束y座標 * @param width 寬 * @param height 高 * @param keepAspectRatio 默認是按照比例縮放的,值爲false 時不按比例縮放 */ public static void imgSourceRegion(String source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) throws IOException { Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio) .toFile(output); } public static void imgSourceRegion(File source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) throws IOException { Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio) .outputFormat("jpg").toFile(output); } /** * 轉化圖像格式 * * @param source 輸入源 * @param output 輸出源 * @param width 寬 * @param height 高 * @param format 圖片類型,gif、png、jpg */ public static void imgFormat(String source, String output, int width, int height, String format) throws IOException { Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output); } public static void imgFormat(File source, String output, int width, int height, String format) throws IOException { Thumbnails.of(source).size(width, height).outputFormat(format).outputFormat("jpg").toFile(output); } /** * 輸出到OutputStream * * @param source 輸入源 * @param output 輸出源 * @param width 寬 * @param height 高 * @return toOutputStream(流對象) */ public static OutputStream imgOutputStream(String source, String output, int width, int height) throws IOException { OutputStream os = new FileOutputStream(output); Thumbnails.of(source).size(width, height).outputFormat("jpg").toOutputStream(os); return os; } public static OutputStream imgOutputStream(File source, String output, int width, int height) throws IOException { OutputStream os = new FileOutputStream(output); Thumbnails.of(source).size(width, height).outputFormat("jpg").toOutputStream(os); return os; } /** * 輸出到BufferedImage * * @param source 輸入源 * @param output 輸出源 * @param width 寬 * @param height 高 * @param format 圖片類型,gif、png、jpg * @return BufferedImage */ public static BufferedImage imgBufferedImage(String source, String output, int width, int height, String format) throws IOException { BufferedImage buf = Thumbnails.of(source).size(width, height).outputFormat("jpg").asBufferedImage(); ImageIO.write(buf, format, new File(output)); return buf; } public static BufferedImage imgBufferedImage(File source, String output, int width, int height, String format) throws IOException { BufferedImage buf = Thumbnails.of(source).size(width, height).outputFormat("jpg").asBufferedImage(); ImageIO.write(buf, format, new File(output)); return buf; } }

 

這工具類,可將圖片裁剪,壓縮,等比縮放。。。還能夠哦ui

相關文章
相關標籤/搜索