我有一张说尺寸为800x800的图片,尺寸为170 kb。我想调整此图像的大小说600x600。调整大小后,我希望减小图像大小。我怎么能这样做?
答案 0 :(得分:14)
您不需要一个完整的图像处理库来简单地调整图像大小。
推荐的方法是使用渐进双线性缩放,就像这样(可以在代码中使用此方法):
public BufferedImage scale(BufferedImage img, int targetWidth, int targetHeight) {
int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = img;
BufferedImage scratchImage = null;
Graphics2D g2 = null;
int w = img.getWidth();
int h = img.getHeight();
int prevW = w;
int prevH = h;
do {
if (w > targetWidth) {
w /= 2;
w = (w < targetWidth) ? targetWidth : w;
}
if (h > targetHeight) {
h /= 2;
h = (h < targetHeight) ? targetHeight : h;
}
if (scratchImage == null) {
scratchImage = new BufferedImage(w, h, type);
g2 = scratchImage.createGraphics();
}
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null);
prevW = w;
prevH = h;
ret = scratchImage;
} while (w != targetWidth || h != targetHeight);
if (g2 != null) {
g2.dispose();
}
if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) {
scratchImage = new BufferedImage(targetWidth, targetHeight, type);
g2 = scratchImage.createGraphics();
g2.drawImage(ret, 0, 0, null);
g2.dispose();
ret = scratchImage;
}
return ret;
}
代码已在Filthy Rich Clients处修改并清除原件。
根据您的评论,您可以降低质量并编码JPEG字节,如下所示:
image
是BufferedImage。
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.2f); // Change this, float between 0.0 and 1.0
writer.setOutput(ImageIO.createImageOutputStream(os));
writer.write(null, new IIOImage(image, null, null), param);
writer.dispose();
现在,由于os
是ByteArrayOutputStream
,您可以对其进行Base64编码。
String base64 = Base64.encode(os.toByteArray());
答案 1 :(得分:5)
您可以使用100%Java,Apache2开源imgscalr library(单个静态类)并执行以下操作:
import org.imgscalr.Scalr.*; // static imports are awesome with the lib
... some class code ...
// This is a super-contrived method name, I just wanted to incorporate
// the details from your question accurately.
public static void resizeImageTo600x600(BufferedImage image) {
ImageIO.write(resize(image, 600), "JPG", new File("/path/to/file.jpg"));
}
注意:如果以上内容看起来很奇怪,静态导入允许我直接使用调整大小调用而不指定 Scalr.resize(...)< / em>的
此外,如果写出的缩放图像的质量看起来不够好(虽然它会被快速写出),你可以使用更多参数来调整 resize 方法:
public static void resizeImageTo600x600(BufferedImage image) {
ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600), "JPG", new File("/path/to/file.jpg"));
}
..你甚至可以将一个BufferedImageOp应用于结果以软化它,因为降尺度使图像看起来像锯齿状:
public static void resizeImageTo600x600(BufferedImage image) {
ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600, Scalr.OP_ANTIALIAS), "JPG", new File("/path/to/file.jpg"));
}
只需在Maven POM中添加以下dep条目即可开始使用该库(imgscalr位于Maven中央回购中):
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
答案 2 :(得分:3)
使用一些支持通过Java进行图像处理的好框架,例如: IamageJ
通过MemoryImageSource和PixelGrabber等一些类,Java中也提供了一些基本支持。