54

遵循javadocs,我尝试扩展aBufferedImage但没有成功,这是我的代码:

BufferedImage image = MatrixToImageWriter.getBufferedImage(encoded);
Graphics2D grph = image.createGraphics();
grph.scale(2.0, 2.0);
grph.dispose();

我不明白为什么它不起作用,有什么帮助吗?

4

7 回答 7

76

AffineTransformOp提供了选择插值类型的额外灵活性。

BufferedImage before = getBufferedImage(encoded);
int w = before.getWidth();
int h = before.getHeight();
BufferedImage after = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
AffineTransform at = new AffineTransform();
at.scale(2.0, 2.0);
AffineTransformOp scaleOp = 
   new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
after = scaleOp.filter(before, after);

显示的片段说明了重新采样,而不是裁剪;这个相关的答案解决了这个问题此处检查了一些相关示例。

于 2010-11-18T15:43:26.853 回答
39

不幸的是,即使没有问题,getScaledInstance() 的性能也很差。

另一种方法是创建一个新的 BufferedImage 并在新图像上绘制原始图像的缩放版本。

BufferedImage resized = new BufferedImage(newWidth, newHeight, original.getType());
Graphics2D g = resized.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(original, 0, 0, newWidth, newHeight, 0, 0, original.getWidth(),
    original.getHeight(), null);
g.dispose();

newWidth,newHeight 表示新的 BufferedImage 大小,必须正确计算。在因子缩放的情况下:

int newWidth = new Double(original.getWidth() * widthFactor).intValue();
int newHeight = new Double(original.getHeight() * heightFactor).intValue();

编辑:找到说明性能问题的文章:Image.getScaledInstance() 的危险

于 2010-11-18T15:15:31.740 回答
13

使用 imgscalr – Java 图像缩放库:

BufferedImage image =
     Scalr.resize(originalImage, Scalr.Method.BALANCED, newWidth, newHeight);

这对我来说已经足够快了。

于 2014-02-23T21:52:14.697 回答
10

正如@Bozho 所说,您可能想要使用getScaledInstance.

但是,要了解其grph.scale(2.0, 2.0)工作原理,您可以查看以下代码:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


class Main {
    public static void main(String[] args) throws IOException {

        final int SCALE = 2;

        Image img = new ImageIcon("duke.png").getImage();

        BufferedImage bi = new BufferedImage(SCALE * img.getWidth(null),
                                             SCALE * img.getHeight(null),
                                             BufferedImage.TYPE_INT_ARGB);

        Graphics2D grph = (Graphics2D) bi.getGraphics();
        grph.scale(SCALE, SCALE);

        // everything drawn with grph from now on will get scaled.

        grph.drawImage(img, 0, 0, null);
        grph.dispose();

        ImageIO.write(bi, "png", new File("duke_double_size.png"));
    }
}

给定duke.png
在此处输入图像描述

它产生duke_double_size.png
在此处输入图像描述

于 2010-11-18T15:07:18.277 回答
9

要缩放图像,您需要创建一个新图像并在其中绘制。一种方法是使用filter()an 的方法AffineTransferOp,如此处所建议。这允许您选择插值技术。

private static BufferedImage scale1(BufferedImage before, double scale) {
    int w = before.getWidth();
    int h = before.getHeight();
    // Create a new image of the proper size
    int w2 = (int) (w * scale);
    int h2 = (int) (h * scale);
    BufferedImage after = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_ARGB);
    AffineTransform scaleInstance = AffineTransform.getScaleInstance(scale, scale);
    AffineTransformOp scaleOp 
        = new AffineTransformOp(scaleInstance, AffineTransformOp.TYPE_BILINEAR);

    scaleOp.filter(before, after);
    return after;
}

另一种方法是简单地将原始图像绘制到新图像中,使用缩放操作进行缩放。这种方法非常相似,但它也说明了如何在最终图像中绘制任何你想要的东西。(我在这两种方法开始不同的地方添加了一个空白行。)

private static BufferedImage scale2(BufferedImage before, double scale) {
    int w = before.getWidth();
    int h = before.getHeight();
    // Create a new image of the proper size
    int w2 = (int) (w * scale);
    int h2 = (int) (h * scale);
    BufferedImage after = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_ARGB);
    AffineTransform scaleInstance = AffineTransform.getScaleInstance(scale, scale);
    AffineTransformOp scaleOp 
        = new AffineTransformOp(scaleInstance, AffineTransformOp.TYPE_BILINEAR);

    Graphics2D g2 = (Graphics2D) after.getGraphics();
    // Here, you may draw anything you want into the new image, but we're
    // drawing a scaled version of the original image.
    g2.drawImage(before, scaleOp, 0, 0);
    g2.dispose();
    return after;
}

附录:结果

为了说明差异,我比较了以下五种方法的结果。以下是放大和缩小的结果以及性能数据的样子。(性能从一次运行到下一次运行不同,因此仅将这些数字作为粗略的指导。)顶部图像是原始图像。我将其缩放为两倍大小和一半大小。

如您所见,AffineTransformOp.filter()使用 inscaleBilinear()比 in 的标准绘图方法Graphics2D.drawImage()更快scale2()。BiCubic 插值也是最慢的,但在扩展图像时效果最好。(对于性能,它应该只与scaleBilinear()和进行比较scaleNearest().)双线性似乎更适合缩小图像,尽管这是一个艰难的决定。NearestNeighbor 是最快的,结果最差。双线性似乎是速度和质量之间的最佳折衷。Image.getScaledInstance()方法中调用的,questionable()性能非常差,并且返回的质量与 NearestNeighbor 相同。(性能数字仅用于扩展图像。)

在此处输入图像描述

public static BufferedImage scaleBilinear(BufferedImage before, double scale) {
    final int interpolation = AffineTransformOp.TYPE_BILINEAR;
    return scale(before, scale, interpolation);
}

public static BufferedImage scaleBicubic(BufferedImage before, double scale) {
    final int interpolation = AffineTransformOp.TYPE_BICUBIC;
    return scale(before, scale, interpolation);
}

public static BufferedImage scaleNearest(BufferedImage before, double scale) {
    final int interpolation = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
    return scale(before, scale, interpolation);
}

@NotNull
private static 
BufferedImage scale(final BufferedImage before, final double scale, final int type) {
    int w = before.getWidth();
    int h = before.getHeight();
    int w2 = (int) (w * scale);
    int h2 = (int) (h * scale);
    BufferedImage after = new BufferedImage(w2, h2, before.getType());
    AffineTransform scaleInstance = AffineTransform.getScaleInstance(scale, scale);
    AffineTransformOp scaleOp = new AffineTransformOp(scaleInstance, type);
    scaleOp.filter(before, after);
    return after;
}

/**
 * This is a more generic solution. It produces the same result, but it shows how you 
 * can draw anything you want into the newly created image. It's slower 
 * than scaleBilinear().
 * @param before The original image
 * @param scale The scale factor
 * @return A scaled version of the original image
 */
private static BufferedImage scale2(BufferedImage before, double scale) {
    int w = before.getWidth();
    int h = before.getHeight();
    // Create a new image of the proper size
    int w2 = (int) (w * scale);
    int h2 = (int) (h * scale);
    BufferedImage after = new BufferedImage(w2, h2, before.getType());
    AffineTransform scaleInstance = AffineTransform.getScaleInstance(scale, scale);
    AffineTransformOp scaleOp
            = new AffineTransformOp(scaleInstance, AffineTransformOp.TYPE_BILINEAR);

    Graphics2D g2 = (Graphics2D) after.getGraphics();
    // Here, you may draw anything you want into the new image, but we're just drawing
    // a scaled version of the original image. This is slower than 
    // calling scaleOp.filter().
    g2.drawImage(before, scaleOp, 0, 0);
    g2.dispose();
    return after;
}

/**
 * I call this one "questionable" because it uses the questionable getScaledImage() 
 * method. This method is no longer favored because it's slow, as my tests confirm.
 * @param before The original image
 * @param scale The scale factor
 * @return The scaled image.
 */
private static Image questionable(final BufferedImage before, double scale) {
    int w2 = (int) (before.getWidth() * scale);
    int h2 = (int) (before.getHeight() * scale);
    return before.getScaledInstance(w2, h2, Image.SCALE_FAST);
}
于 2017-09-14T06:19:22.250 回答
8

如果您不介意使用外部库,Thumbnailator可以执行BufferedImages 的缩放。

Thumbnailator 将负责处理Java 2D处理(例如使用Graphics2D和设置适当的渲染提示),以便可以使用简单的 fluent API 调用来调整图像大小:

BufferedImage image = Thumbnails.of(originalImage).scale(2.0).asBufferedImage();

尽管Thumbnailator,顾名思义,是为缩小图像而设计的,但它也可以很好地放大图像,在其默认的缩放器实现中使用双线性插值。


免责声明:我是Thumbnailator库的维护者。

于 2014-11-16T10:35:05.470 回答
3

scale(..)工作方式有点不同。您可以使用bufferedImage.getScaledInstance(..)

于 2010-11-18T15:02:45.853 回答