首页 > 其他分享 >简单易用的图片缩略图处理库

简单易用的图片缩略图处理库

时间:2023-03-04 21:22:15浏览次数:44  
标签:srcImage targetHeight 缩略图 int targetWidth 易用 图片

哈喽,大家好,我是源小北

 

最近给大家送IDEA激活码。先到先得 具体操作步骤文章

image.png

 

最近在工作中,刚好有遇到对图片进行缩放处理的功能。于是,便拿起很久以前使用过的Java原生工具类处理图片,不过在开发过程中也发现了一些问题。

在Java中,原生的处理图片缩略图,我们可以使用java.awt.Graphics2D和Image.getScaledInstance两种工具类。我们先来看看他们的使用方式。

首先是java.awt.Graphics2D

很多人可能对Graphics2D 不是很熟悉,它是Java平台提供的可以渲染二维形状、文本、图像的基础类。

下面我们来看看使用Graphics2D进行图片大小调整的一个例子。

/**
* 图片缩放
*
* @param srcImagePath 图片路径
* @param targetWidth 目标宽度
* @return
* @throws IOException
*/
public static BufferedImage resizeImage(String srcImagePath, int targetWidth) throws IOException {
Image srcImage = ImageIO.read(new File(srcImagePath));
int targetHeight = getTargetHeight(targetWidth, srcImage);
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = resizedImage.createGraphics();
graphics2D.drawImage(srcImage, 0, 0, targetWidth, targetHeight, null);
graphics2D.dispose();
return resizedImage;
}

/**
* 根据指定宽度,计算等比例高度
*
* @param targetWidth 目标宽度
* @param srcImage 图片信息
* @return
*/
private static int getTargetHeight(int targetWidth, Image srcImage) {
int targetHeight = srcImage.getHeight(null);
if (targetWidth < srcImage.getWidth(null)) {
targetHeight = Math.round((float)targetHeight / ((float)srcImage.getWidth(null) / (float)targetWidth));
}
return targetHeight;
}

先简单的看看,我们再来看看另一工具类的实现代码例子:

Image.getScaledInstance

/**
* 图片缩放
*
* @param srcImagePath 图片路径
* @param targetWidth 目标宽度
* @return
* @throws IOException
*/
public static BufferedImage resizeImage2(String srcImagePath, int targetWidth) throws IOException {
Image srcImage = ImageIO.read(new File(srcImagePath));
int targetHeight = getTargetHeight(targetWidth, srcImage);
Image image = srcImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(image, 0, 0, null);
return bufferedImage;
}

/**
* 根据指定宽度,计算等比例高度
*
* @param targetWidth 目标宽度
* @param srcImage 图片信息
* @return
*/
private static int getTargetHeight(int targetWidth, Image srcImage) {
int targetHeight = srcImage.getHeight(null);
if (targetWidth < srcImage.getWidth(null)) {
targetHeight = Math.round((float)targetHeight / ((float)srcImage.getWidth(null) / (float)targetWidth));
}
return targetHeight;
}

不过我们要注意,使用Image.getScaledInstance虽然相比java.awt.Graphics2D调整图片更大小简单方便,生成的图片质量也不错,但是这种方式的效率并不高。

但是指背君觉得,这两种Java的原生工具类,进行图片缩略图处理还是有很多的弊端:代码非常可读性不高,且不够简练,需要经过一番折腾才能得到最终的效果。

那么有没有其他优雅、简练且易用的方式处理图片缩略图呢?当然有啦,源小北经过一番苦寻,终于在开源界找到啦一个简单好用的开源Java工具类。

重头戏:Thumbnailator

Thumbnailator是什么呢?它是一个专门处理缩略图的Java代码库。它可以通过一个简单的步骤执行相当复杂的缩略图处理任务。

GitHub上的开源地址:https://github.com/coobird/thumbnailator

那么,到底有多件单呢?我们看看下面的例子:

假如,我们需要在一个目录中创建图像文件的JPEG缩略图,在保留原始图像的纵横比的同时,全部调整为600像素x400像素的最大尺寸,可以通过以下方式执行:

首先项目中导入对应的Java包:

<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>[0.4, 0.5)</version>
</dependency>

然后只需按下面方式编码:

Thumbnails.of(new File("path/to/directory").listFiles())
.size(600, 400)
.outputFormat("jpg")
.toFiles(Rename.PREFIX_DOT_THUMBNAIL);

哈哈,你没有看错,就上面的一行代码就完成了图片缩略图的处理,是不是比Java原生的处理方式方面多了。

而且代码整体看来,简单、易读更容易使用。有了这个类库,大家处理图片缩略图可以说是轻而易举啦!

小北有话说

开源指北,立志做最好的开源分享平台,分享有趣实用的开源项目。

以上就是本次推荐的全部内容,我是源小北,感谢各位的观看。

标签:srcImage,targetHeight,缩略图,int,targetWidth,易用,图片
From: https://www.cnblogs.com/kyzb-yxb/p/17179190.html

相关文章