在JAVA中,当我们调整图片的大小或比例时,我们可以按照以下的步棸:
- 通过调用的ImageIO类的read(File)方法 创建用于输入图片BufferedImage对象。
- 按所需输出高度输出的BufferedImage 对象。
- 从输出图片的BufferedImage对象获取一个Graphics2D对象。
- 画出输入图片的BufferedImage对象到输出图片的Graphics2D对象。
- 用ImageIO这个类,write(File)方法将图片保存到硬盘。
这是一个图片操作的工具类。他实现了两个用于调整图片大小的方法。
package net.codejava.graphic;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 操作图片的代码.
*/
public class ImageResizer {
/**
* Resizes an image to a absolute width and height (the image may not be
* proportional)
* @param inputImagePath Path of the original image
* @param outputImagePath Path to save the resized image
* @param scaledWidth absolute width in pixels
* @param scaledHeight absolute height in pixels
* @throws IOException
*/
public static void resize(String inputImagePath,
String outputImagePath, int scaledWidth, int scaledHeight)
throws IOException {
// reads input image
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth,
scaledHeight, inputImage.getType());
// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// extracts extension of output file
String formatName = outputImagePath.substring(outputImagePath
.lastIndexOf(".") + 1);
// writes to output file
ImageIO.write(outputImage, formatName, new File(outputImagePath));
}
/**
* Resizes an image by a percentage of original size (proportional).
* @param inputImagePath Path of the original image
* @param outputImagePath Path to save the resized image
* @param percent a double number specifies percentage of the output image
* over the input image.
* @throws IOException
*/
public static void resize(String inputImagePath,
String outputImagePath, double percent) throws IOException {
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
int scaledWidth = (int) (inputImage.getWidth() * percent);
int scaledHeight = (int) (inputImage.getHeight() * percent);
resize(inputImagePath, outputImagePath, scaledWidth, scaledHeight);
}
/**
* Test resizing images
*/
public static void main(String[] args) {
String inputImagePath = "D:/Photo/Puppy.jpg";
String outputImagePath1 = "D:/Photo/Puppy_Fixed.jpg";
String outputImagePath2 = "D:/Photo/Puppy_Smaller.jpg";
String outputImagePath3 = "D:/Photo/Puppy_Bigger.jpg";
try {
// resize to a fixed width (not proportional)
int scaledWidth = 1024;
int scaledHeight = 768;
ImageResizer.resize(inputImagePath, outputImagePath1, scaledWidth, scaledHeight);
// resize smaller by 50%
double percent = 0.5;
ImageResizer.resize(inputImagePath, outputImagePath2, percent);
// resize bigger by 50%
percent = 1.5;
ImageResizer.resize(inputImagePath, outputImagePath3, percent);
} catch (IOException ex) {
System.out.println("Error resizing the image.");
ex.printStackTrace();
}
}
}
[warning]
注意 这样操作图片可能会影响图片的质量[/warning]