Java 后台实现图片压缩的2种方法

1.使用Graphics重新绘制图片+JPEGImageEncoder文件格式转换实现图片压缩

直接帖java源码,也可以下载整个Java源文件

public class CompressPic {
    public static void main(String[] args) {
        CompressPic compressPic=new CompressPic();
        compressPic.compressPic("D:\\BaiduYunDownload\\1.jpg", "D:\\BaiduYunDownload\\1_min.jpg");
    }
    /*******************************************************************************
     * 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法
     * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
     */
    private File file = null; // 文件对象
    private String inputFileName; // 输入图文件名
    private String outputFileName; // 输出图文件名
    public CompressPic() { // 初始化变量
        inputFileName = "";
        outputFileName = "";
    }


    public void setInputFileName(String inputFileName) {
        this.inputFileName = inputFileName;
    }

    public void setOutputFileName(String outputFileName) {
        this.outputFileName = outputFileName;
    }


    /*
     * 获得图片大小 传入参数 String path :图片路径
     */
    public long getPicSize(String path) {
        file = new File(path);
        return file.length();
    }

    // 图片处理
    public String compressPic() {
        try {
            // 获得源文件
            file = new File(inputFileName);
            //System.out.println(inputDir + inputFileName);
            if (!file.exists()) {
                System.out.println("文件不存在!");
            }
            Image img = ImageIO.read(file);
            // 判断图片格式是否正确
            if (img.getWidth(null) == -1) {
                System.out.println(" can't read,retry!" + "<BR>");
                return "no";
            } else {
                int newWidth=236;
                int newHeight=(int)((double) img.getHeight(null)/(((double) img.getWidth(null))/236));
//                // 判断是否是等比缩放
//                if (this.proportion == true) {
//                    // 为等比缩放计算输出的图片宽度及高度
//                    double rate1 = ((double) img.getWidth(null))
//                            / (double) outputWidth + 0.1;
//                    double rate2 = ((double) img.getHeight(null))
//                            / (double) outputHeight + 0.1;
//                    // 根据缩放比率大的进行缩放控制
//                    double rate = rate1 > rate2 ? rate1 : rate2;
//                    newWidth = (int) (((double) img.getWidth(null)) / rate);
//                    newHeight = (int) (((double) img.getHeight(null)) / rate);
//                } else {
//                    newWidth = outputWidth; // 输出的图片宽度
//                    newHeight = outputHeight; // 输出的图片高度
//                }
                BufferedImage tag = new BufferedImage((int) newWidth,
                        (int) newHeight, BufferedImage.TYPE_INT_RGB);

                /*
                 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
                 */
                tag.getGraphics().drawImage(
                        img.getScaledInstance(newWidth, newHeight,
                                Image.SCALE_SMOOTH), 0, 0, null);
                FileOutputStream out = new FileOutputStream(outputFileName);
                // JPEGImageEncoder可适用于其他图片类型的转换
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(tag);
                out.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return "ok";
    }

    public String compressPic(
            String inputFileName, String outputFileName) {
        // 输入图文件名
        this.inputFileName = inputFileName;
        // 输出图文件名
        this.outputFileName = outputFileName;
        return compressPic();
    }
    
}

下载java源码 

2.thumbnailator插件进行图片压缩

首先要下载 thumbnailator-0.4.3.jar jar包 
下载地址:http://www.java2s.com/Code/Jar/t/Downloadthumbnailator042alljar.htm

Thumbnails.of(fromPic)
        .size(200, 200)
        .keepAspectRatio(false)
        .allowOverwrite(true)
        .toFile(toPic);
 

详情请看 thumbnailator官网 有更多的压缩方式 
优点:代码简单,只用一行代码就可搞定,压缩方式多,可以压缩大小,压缩质量,旋转,加水印等 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值