栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java图片压缩工具类集

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java图片压缩工具类集

package com.boot.security.server.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;


public class PicUtils{
    private static float QUALITY = 0.8f;

    
    public static byte[] compressOfSize(File file, int width, int height)
            throws Exception {
        byte[] bs = null;
        InputStream input = null;
        try {
            input = new FileInputStream(file);
            bs = compressOfSize(input, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bs;
    }

    
    public static byte[] compressOfSize(InputStream input, int width, int height)
            throws Exception {
        ByteArrayOutputStream output = null;
        try {
            output = new ByteArrayOutputStream();
            Thumbnails.of(input).size(width, height).toOutputStream(output);
            return output.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    
    public static byte[] compressOfPercent(InputStream input, float percent)
            throws Exception {
        ByteArrayOutputStream output = null;
        try {
            output = new ByteArrayOutputStream();
            Thumbnails.of(input).scale(percent).toOutputStream(output);
            return output.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    
    public static byte[] compressOfPercent(File file, float percent)
            throws Exception {
        byte[] bs = null;
        InputStream input = null;
        try {
            input = new FileInputStream(file);
            bs = compressOfPercent(input, percent);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bs;
    }

    
    public static byte[] compressOfQuality(File file, float quality)
            throws Exception {
        byte[] bs = null;
        InputStream input = null;
        try {
            input = new FileInputStream(file);
            bs = compressOfQuality(input, quality);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bs;
    }

    
    public static byte[] compressOfQuality(InputStream input, float quality)
            throws Exception {
        ByteArrayOutputStream output = null;
        try {
            output = new ByteArrayOutputStream();
            if(quality == 0){
                Thumbnails.of(input).scale(1f).outputQuality(QUALITY)
                        .toOutputStream(output);
            } else {
                Thumbnails.of(input).scale(1f).outputQuality(quality)
                        .toOutputStream(output);
            }
            return output.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    
    public static byte[] waterMark(byte[] fromPic, InputStream markPic,
                                   float transparent) throws Exception {
        InputStream finput = null;
        ByteArrayOutputStream output = null;
        try {
            finput = new ByteArrayInputStream(fromPic);
            output = new ByteArrayOutputStream();
            Thumbnails
                    .of(finput)
                    .scale(1f)
                    .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(markPic),
                            transparent).toOutputStream(output);
            return output.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (finput != null)
                    finput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    
    public static byte[] transferPicFormat(byte[] fromPic, String picFormat)
            throws Exception {
        ByteArrayInputStream finput = null;
        ByteArrayOutputStream output = null;
        try {
            finput = new ByteArrayInputStream(fromPic);
            output = new ByteArrayOutputStream();
            Thumbnails.of(finput).outputFormat(picFormat)
                    .toOutputStream(output);
            return output.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (finput != null)
                    finput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }




    public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
        if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
            return imageBytes;
        }
        long srcSize = imageBytes.length;
        double accuracy = getAccuracy(srcSize / 1024);
        try {
            while (imageBytes.length > desFileSize * 1024) {
                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
                Thumbnails.of(inputStream)
                        .scale(accuracy)
                        .outputQuality(accuracy)
                        .toOutputStream(outputStream);
                imageBytes = outputStream.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imageBytes;
    }
 
    
    private static double getAccuracy(long size) {
        double accuracy;
        if (size < 900) {
            accuracy = 0.85;
        } else if (size < 2047) {
            accuracy = 0.6;
        } else if (size < 3275) {
            accuracy = 0.44;
        } else {
            accuracy = 0.4;
        }
        return accuracy;
    }


 
}

记录一下.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571642.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号