本地的字符串路径,写的可以传递多个但是我试了多个会报错,最好只用一个。
Thumbnails.of("C:\Users\86152\Desktop\1\原图.png").scale(1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

将字符串抽象成File也可以。
Thumbnails.of(new File("C:\Users\86152\Desktop\1\原图.png")).scale(1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

从输入流读取源文件。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

从网络流读取源文件。
Thumbnails.of(new URL("https://images0.cnblogs.com/blog/651487/201410/191023445446341.png")).scale(1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

将图片输出到本地,接收字符串路径。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

将图片输出到文件。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).toFile(new File("C:\Users\86152\Desktop\1\1.jpg"));

将图片输出到输出流。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).toOutputStream(new FileOutputStream("C:\Users\86152\Desktop\1\1.jpg"));

size(int width,int height)会对图片进行缩放,会改变图片的分辨率随之图片的大小也会改变,但它并不会严格按照我们输入的width,height进行缩放,因为这样图片的比例可能不对称,最终的结果会稍微的微调。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).size(500, 500).toFile("C:\Users\86152\Desktop\1\1.jpg");

width(int width)和size(int width,int height)的区别是它允许你单独指定width然后它会自动根据width自适应的对height进行缩放。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).width(100).toFile("C:\Users\86152\Desktop\1\1.jpg");

height(int height)和width(int width)的区别很明显,它指定height,width会自动根据height自适应的缩放。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).height(100).toFile("C:\Users\86152\Desktop\1\1.jpg");

forceSize(int width, int height)跟size(int width,int height)的区别是它会严格按照参数width,height对图片进行缩放。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).forceSize(111,222).toFile("C:\Users\86152\Desktop\1\1.jpg");

scale(double scaleWidth, double scaleHeight)根据比例对图片进行缩放,它是严格按照参数比例的,如果是1则是原图,如果大于1则分辨率也会增大。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(0.1d, 0.5).toFile("C:\Users\86152\Desktop\1\1.jpg");

scale(double scale)和scale(double scaleWidth, double scaleHeight)的别是,指定一个参数就可以代表width和height。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(0.1d).toFile("C:\Users\86152\Desktop\1\1.jpg");

sourceRegion(int x, int y, int width, int height)四个参数分别是指定从原图片的X轴,Y轴,向右取width的宽,height的高开始裁剪,裁剪的区域就是新的图片。
出现以下错误,缺少size,也就是说当指定了需要裁剪的图片区域后,要先变成一个新的图片必须找到一个容器存放,可以选择缩放里的几个函数。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(0, 0, 1920, 1080).toFile("C:\Users\86152\Desktop\1\1.jpg");

从下图看缩略图原图和这个图片其实差不多的包括的它们的分辨率,但它的大小其实是改变了。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(0, 0, 1920, 1080).size(1920, 1080).toFile("C:\Users\86152\Desktop\1\1.jpg");

这次我们将输出的width,height改成100,100。图片的分辨率和大小都改变了。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(0, 0, 1920, 1080).size(100, 100).toFile("C:\Users\86152\Desktop\1\1.jpg");


换成forceSize后整体的效果就出来了,因为他严格按照指定的width和height改变输出图片。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(0, 0, 1920, 1080).forceSize(100, 100).toFile("C:\Users\86152\Desktop\1\1.jpg");

sourceRegion(Position position, int width, int height)是sourceRegion(int x, int y, int width, int height)重载,Position将X,Y封装成了对象,Position是一个接口它的唯一实现类是Positions。TOP_LEFT代表的是X(0),Y(0)。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(Positions.TOP_LEFT, 1920, 1080).forceSize(1920,1080).toFile("C:\Users\86152\Desktop\1\1.jpg");

sourceRegion(Position position, Size size)是sourceRegion(int x, int y, int width, int height)重载它将X,Y封装成了Position,将width,height封装成了Size,Size是一个接口它的唯一实现类是RelativeSize这个类的构造器接收一个参数,没错一个参数就是百分比,而且是width,height的百分比就像scale(double scale)。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(Positions.TOP_LEFT, new RelativeSize(1)).forceSize(1920, 1080)
.toFile("C:\Users\86152\Desktop\1\1.jpg");
sourceRegion(Region sourceRegion)其实就是上边三个重载形式最终组成的对象,Region的构造器是Position和Size。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(new Region(Positions.TOP_LEFT, new RelativeSize(1))).forceSize(1920, 1080).toFile("C:\Users\86152\Desktop\1\1.jpg");
sourceRegion(Rectangle region)参数Rectangle跟Region差不多,不过它有无参构造,如果使用无参构造Rectangle()相当于sourceRegion(0, 0, 0, 0)如果用这个会报错,因为width,height为0。同样你也可以直接指定它的四个参数。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).sourceRegion(new Rectangle(0, 0, 1920, 1080)).forceSize(1920, 1080).toFile("C:\Users\86152\Desktop\1\1.jpg");
crop(Position position)这个方法就有点意思了,相比sourceRegion(int x, int y, int width, int height)少了width,height那它是如何指定X,Y的横向纵向距离呢?谜底是它使用size(int width, int height)。也就是输出的width,height既是裁剪的width,height。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).crop(Positions.TOP_LEFT).size(500, 500).toFile("C:\Users\86152\Desktop\1\1.jpg");

是否允许覆盖源文件,如果要输出的图片已经存在(路径和文件名相同),默认是覆盖掉源文件,使用此方法传值false则可以避免文件覆盖。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).crop(Positions.TOP_LEFT).size(500, 400).allowOverwrite(false).toFile("C:\Users\86152\Desktop\1\1.jpg");

outputFormat(String format)这个方法用于更改输出文件的类型,如果指定了输出类型,那输出文件名就不要带文件类型了。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).outputFormat("png").toFile("C:\Users\86152\Desktop\1\1");

outputQuality(float quality)设置输出图片的质量,0.1-1,1表示最大质量。下面看三种情况下的输出对比。
1 缩略模式,width,height保持原图1:1。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

2 缩略模式,width,height保持原图1:1,outputQuality(1f)输出全量质量。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).outputQuality(1f).toFile("C:\Users\86152\Desktop\1\2.jpg");

3 缩略模式,width,height保持原图1:1,outputQuality(0.5f)输出一半质量。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).outputQuality(0.5f).toFile("C:\Users\86152\Desktop\1\3.jpg");

watermark(BufferedImage image)给图片打水印,BufferedImage是水印图片的抽象表示。使用ImageIO.read(InputStream in)构造一个水印的图片的表示类。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg"))).toFile("C:\Users\86152\Desktop\1\1.jpg");

watermark(BufferedImage image, float opacity)和watermark(BufferedImage image)的区别是第二个参数它表示水印的不透明度从0.1-1,不指定则是0.5,下图设置不透明度为0.1
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg")), 0.1f).toFile("C:\Users\86152\Desktop\1\2.jpg");

下图设置不透明度为0.1。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg")), 1f).toFile("C:\Users\86152\Desktop\1\3.jpg");

watermark(Position position, BufferedImage image, float opacity)是上边两个的完整体,默认的水印位置在输出图片的中间,使用Position指定水印输出位置。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg")), 1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

如果需要打上多个水印,也可以连续调用watermark。下图在输出图底部中间和底部右边打上水印。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg")), 1f).watermark(Positions.BOTTOM_CENTER,ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg")), 1f).toFile("C:\Users\86152\Desktop\1\1.jpg");

watermark(Watermark w)其实就是把Position,BufferedImage,opacity封装成了Watermark对象,可以使水印共同使用。
Watermark watermark1 = new Watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg")), 1f);
Watermark watermark2 = new Watermark(Positions.BOTTOM_LEFT,ImageIO.read(new FileInputStream("C:\Users\86152\Desktop\1\水印.jpg")), 1f);
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).watermark(watermark1).watermark(watermark2).toFile("C:\Users\86152\Desktop\1\1.jpg");

rotate(double angle)对输出图片做旋转操作。
Thumbnails.of(new FileInputStream("C:\Users\86152\Desktop\1\原图.png")).scale(1f).rotate(180).toFile("C:\Users\86152\Desktop\1\1.jpg");

登录查看全部
参与评论
手机查看
返回顶部