java图像
Today we will look into java resize image program. Recently I had to upload a lot of images to an FTP server but the images size were huge. Since I was going to use them only on web pages, it was wise to resize them to a smaller size and then upload it.
今天,我们将研究java调整大小图像程序。 最近,我不得不将很多图像上传到FTP服务器,但是图像尺寸很大。 由于我只打算在网页上使用它们,因此明智的做法是将它们调整为较小的大小然后上传。
A few days back I provided a list of online tools to reduce image size. But when you have hundreds of images, it’s better to invest some time and write a program that does this work for you. So let’s see how we can resize an image in java.
几天前,我提供了一些在线工具以减小图像大小 。 但是,当您拥有数百张图像时,最好花一些时间并编写一个可以为您工作的程序。 因此,让我们看看如何在Java中调整图像大小。
Java调整大小图像 (Java Resize Image)
We can use java.awt.Graphics2D
class to resize image in java.
我们可以使用java.awt.Graphics2D
类来调整Java中图像的大小。
Below is the program that searches all the files in a directory and resizes them to the given size and saves it to a different directory.
以下是搜索目录中所有文件并将其大小调整为给定大小并将其保存到其他目录的程序。
The sample program here saves images in both PNG and JPG format but you can easily change it to serve your specific requirement.
这里的示例程序将图像保存为PNG和JPG格式,但是您可以轻松地对其进行更改以满足您的特定要求。
Java图像调整程序 (Java Image Resize Program)
package com.journaldev.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;
/**