<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.12</version>
</dependency>
package com.taotao.test;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.apache.poi.hpsf.Thumbnail;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author aping
* @time 2020/9/6 14:14
* 压缩图片
*/
public class ThumbnailatorTest {
public static void main(String[] args) throws IOException {
// zddx();
test8();
}
/**
* 指定大小进行压缩
*/
public static void zddx()throws IOException{
/***
* sixze(width,height) 若图片横比200小,高比300小,不变
* 若图片横比200小,高比300大 高缩小到300 ,图片比例不变,若图片横比200大,高比300小,横缩小到200图片比例不变
* 若图片横比200大,高比300大,图片按比例缩小,横为200高为300
*/
Thumbnails.of("C:\\js\\1.jpg").size(334,222).toFile("C:\\js\\zddx.jpg");
}
/**
* 按照比例进行压缩
*/
public static void test2()throws IOException{
/**
* scale(比例)
*/
Thumbnails.of("C:\\js\\1.jpg").scale(0.25f).toFile("C:\\js\\2.jpg");
}
/**
* 不按照比例,指定大小进行压缩
*/
public static void dxys() throws IOException{
/**
* keepAspectRatio(false)默认按照比例进行压缩
*/
Thumbnails.of("C:\\js\\1.jpg").size(334,222).keepAspectRatio(false).toFile("C:\\js\\image_120x120.jpg");
}
/**
* 旋转
*/
public static void xuanzhuan()throws IOException{
/**
* roate(角度),正数,顺时针,负数逆时针
*/
Thumbnails.of("C:\\js\\1.jpg").size(334,222).rotate(90).toFile("C:\\js\\image_+90.jpg");
}
/**
* 水印
*/
public static void shuiyin() throws IOException{
/**
* watermark(位置,水印图,透明度)
*/
Thumbnails.of("C:\\js\\1.jpg").size(334,222).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(
"C:\\kaifa\\gz\\VideoTools\\watermark\\12.jpg" )),0.5f).outputQuality(0.8f).toFile("C:\\js\\image_watermark_bottom_right.jpg");
}
/**
* 裁剪
*/
public static void caijian()throws IOException{
/**
* 图片中心400*400的区域
*/
Thumbnails.of("C:\\js\\1.jpg").sourceRegion(Positions.CENTER, 400, 400).size(334,222).keepAspectRatio(false)
.toFile("C:\\js\\image_region_center.jpg");
/**
* 图片右下400*400的区域
*/
Thumbnails.of("C:\\js\\1.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(334,222).keepAspectRatio(false)
.toFile("C:\\js\\image_region_bootom_right.jpg");
/**
* 指定坐标
*/
Thumbnails.of("C:\\js\\1.jpg").sourceRegion(600, 500, 400, 400).size(334,222).keepAspectRatio(false).toFile("C:\\js\\image_region_coord.jpg");
}
/**
* 转化图像格式
*
* @throws IOException
*/
public static void test7() throws IOException {
/**
* outputFormat(图像格式)
*/
Thumbnails.of("C:\\js\\1.jpg").size(334,222).outputFormat("png").toFile("C:\\js\\mage_1280x1024.png");
Thumbnails.of("C:\\js\\1.jpg").size(334,222).outputFormat("gif").toFile("C:\\js\\image_1280x1024.gif");
}
/**
* 输出到OutputStream
*
* @throws IOException
*/
public static void test8() throws IOException {
/**
* toOutputStream(流对象)
*/
OutputStream os = new FileOutputStream("C:\\js\\image_1280x1024_OutputStream.png");
Thumbnails.of("C:\\js\\1.jpg").size(334,222).toOutputStream(os);
}
/**
* 输出到BufferedImage
*
* @throws IOException
*/
public static void test9() throws IOException {
/**
* asBufferedImage() 返回BufferedImage
*/
BufferedImage thumbnail = Thumbnails.of("C:\\js\\1.jpg").size(334,222).asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File("C:\\js\\image_1280x1024_BufferedImage.jpg"));
}
}
网友评论