Java调整大小图像
今天,我们将研究java调整大小图像程序。
最近,我不得不将很多图像上传到FTP服务器,但是图像尺寸很大。
由于我只打算在网页上使用它们,因此明智的做法是将它们调整为较小的尺寸然后上传。
几天前,我提供了一些在线工具以减小图像大小。
但是,当您拥有数百张图片时,最好花一些时间并编写一个程序来为您工作。
因此,让我们看看如何在Java中调整图片大小。
Java调整大小图像
我们可以使用java.awt.Graphics2D类来调整Java中图像的大小。
以下是搜索目录中所有文件并将其大小调整为给定大小并将其保存到其他目录的程序。
这里的示例程序将图像保存为PNG和JPG格式,但是您可以轻松地对其进行更改以满足您的特定要求。
Java图像调整程序
package com.theitroad.util;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* This class will resize all the images in a given folder
* @author hyman
*
*/
public class JavaImageResizer {
public static void main(String[] args) throws IOException {
File folder = new File("/Users/hyman/Desktop/images");
File[] listOfFiles = folder.listFiles();
System.out.println("Total No of Files:"+listOfFiles.length);
Image img = null;
BufferedImage tempPNG = null;
BufferedImage tempJPG = null;
File newFilePNG = null;
File newFileJPG = null;
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
img = ImageIO.read(new File("/Users/hyman/Desktop/images/"+listOfFiles[i].getName()));
tempPNG = resizeImage(img, 100, 100);
tempJPG = resizeImage(img, 100, 100);
newFilePNG = new File("/Users/hyman/Desktop/images/resize/"+listOfFiles[i].getName()+"_New.png");
newFileJPG = new File("/Users/hyman/Desktop/images/resize/"+listOfFiles[i].getName()+"_New.jpg");
ImageIO.write(tempPNG, "png", newFilePNG);
ImageIO.write(tempJPG, "jpg", newFileJPG);
}
}
System.out.println("DONE");
}
/**
* This function resize the image file and returns the BufferedImage object that can be saved to file system.
*/
public static BufferedImage resizeImage(final Image image, int width, int height) {
final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
//below three lines are for RenderingHints for better image quality at cost of higher processing time
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(image, 0, 0, width, height, null);
graphics2D.dispose();
return bufferedImage;
}
}
前几行使用Java IO从目录中检索文件列表,然后在for循环中处理每个文件。
" resizeImage"是其中" Graphics2D"用于调整图像大小并将其作为" BufferedImage"对象返回的方法。
同样,我们使用" ImageIO"以PNG和JPG格式将其写入文件系统。
Java调整图像大小要点
您可以使用
RenderingHints来获得高质量的图像,但会浪费处理时间。
如果您不打算将图像缩小到非常小的尺寸,则可能要避免使用它们以加快处理速度。
根据您的情况,您需要快速处理还是需要更高的质量。即使存在不是图像文件的任何文件(例如文本,doc或者pdf文件),该方法也不会引发任何异常。
在这种情况下,它将调整其大小,您将获得黑色图像。
您可以通过过滤名称来轻松地扩展代码以跳过这些文件。在相同分辨率下,PNG图片尺寸大于JPG图片尺寸,这非常重要。
因此,如果要调整网页图像的大小,则可能要使用JPG格式来节省页面加载时间。
Java使用宽高比调整图像大小
在正常情况下,您要保持图像的宽高比,否则它将从一侧拉伸。
这是可帮助您保持宽高比的代码段。
double aspectRatio = (double) img.getWidth(null)/(double) img.getHeight(null); tempPNG = resizeImage(img, 100, (int) (100/aspectRatio));
请注意,在这种情况下,您必须确保目录中只有图像,否则它将引发异常。

