首页 > 编程语言 > 详细

java实现图片压缩

时间:2020-12-19 13:41:16      阅读:46      评论:0      收藏:0      [点我收藏+]

简介

我们在项目中经常会遇到图片上传的需求,如商品图片,但图片太大的话,在客户端加载太慢影响用户体验,所有一般会将图片进行压缩。

实现

原图
技术分享图片

添加依赖

<dependency>
  <groupId>net.coobird</groupId>
  <artifactId>thumbnailator</artifactId>
  <version>0.4.8</version>
</dependency>

按质量压缩

import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;

public class Client {

  public static void main(String[] args) throws Exception {
    Thumbnails.of(new File("D:/showqrcode.jpg"))
        .scale(1f) //图片大小(长宽)压缩比例 从0-1,1表示原图
        .outputQuality(0.5f) //图片质量压缩比例 从0-1,越接近1质量越好
        .toOutputStream(new FileOutputStream("D:/showqrcode_50.jpg"));
  }

}

压缩后图片
技术分享图片

图片大小从665KB压缩到了77KB。

按比例缩放

import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;

public class Client2 {

  public static void main(String[] args) throws Exception {
    Thumbnails.of(new File("D:/showqrcode.jpg"))
        .scale(0.5f) //图片大小(长宽)压缩 从0按照
        .outputQuality(0.5f) //图片质量压缩比例 从0-1,越接近1质量越好
        .toOutputStream(new FileOutputStream("D:/showqrcode_50%.jpg"));
  }

}

技术分享图片

按大小和比例缩放

import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;

public class Client21 {

  public static void main(String[] args) throws Exception {
    Thumbnails.of(new File("D:/showqrcode.jpg"))
        .size(500, 300) // 图片比例不变
        .toOutputStream(new FileOutputStream("D:/showqrcode_500_300.jpg"));
  }

}

技术分享图片

按大小缩放

import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;

public class Client22 {

  public static void main(String[] args) throws Exception {
    Thumbnails.of(new File("D:/showqrcode.jpg"))
        .forceSize(500, 300) //不保持图片比例
        .toOutputStream(new FileOutputStream("D:/showqrcode_500_300.jpg"));
  }

}

技术分享图片

旋转

import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;

public class Client3 {

  public static void main(String[] args) throws Exception {
    Thumbnails.of(new File("D:/showqrcode.jpg"))
        .forceSize(500, 300)
        .rotate(90f) //向右旋转
        .toOutputStream(new FileOutputStream("D:/showqrcode+90.jpg"));
  }

}

技术分享图片

加水印

水印图片
技术分享图片

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class Client4 {

  public static void main(String[] args) throws IOException {
    Thumbnails.of("D:/showqrcode.jpg")
        .size(1280, 1024)
        .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("D:/watermark.jpg")),
            0.5f) //位置,水印来源,透明度
        .outputQuality(0.8f)
        .toFile("D:/showqrcode_watermark_bottom_right.jpg");
  }

}

技术分享图片

裁剪

import java.io.IOException;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class Client5 {

  public static void main(String[] args) throws IOException {
    Thumbnails.of("D:/showqrcode.jpg")
        .sourceRegion(Positions.CENTER, 800, 600) //位置,宽,高
        .size(800, 600)
        .keepAspectRatio(false)
        .toFile("D:/showqrcode_region_center.jpg");
  }

}

技术分享图片

更多用法请参考官方文档

参考

java使用google开源工具实现图片压缩

java实现图片压缩

原文:https://www.cnblogs.com/strongmore/p/14158639.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!