12

我有一个尺寸为 800x800 的图像,其大小为 170 kb。我想将此图像调整为 600x600。调整大小后,我希望减小图像大小。我怎样才能做到这一点?

4

3 回答 3

20

您不需要一个完整的图像处理库来简单地调整图像大小。

推荐的方法是使用渐进式双线性缩放,就像这样(随意使用此方法,就像在您的代码中一样):

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是缓冲图像。

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是 a ByteArrayOutputStream,您可以对其进行 Base64 编码。

String base64 = Base64.encode(os.toByteArray());
于 2012-10-14T06:13:51.660 回答
6

您可以使用 100% Java、Apache2 开源imgscalr 库(单个静态类)并执行以下操作:

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(...)

此外,如果写出的缩放图像的质量看起来不够好(尽管它会很快被写出),您可以对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>
于 2012-10-17T16:45:24.467 回答
3

使用一些通过Java支持图像处理的好框架,例如IimageJ

Java 中还可以通过一些类(例如MemoryImageSourcePixelGrabber )获得一些基本支持。

于 2012-10-14T05:24:47.320 回答