如何缩放BufferedImage
image
image-processing
java
30
0

按照javadocs,我尝试缩放BufferedImage失败,这是我的代码:

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

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

参考资料:
Stack Overflow
收藏
评论
共 7 个回答
高赞 时间 活跃

scale(..)工作原理略有不同。您可以使用bufferedImage.getScaledInstance(..)

收藏
评论

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);

显示的片段说明了重采样 ,而不是裁剪 ;这个相关的答案解决了这个问题 ; 这里考察一些相关的例子。

收藏
评论

要缩放图像,您需要创建一个新图像并将其绘制。一种方法是使用AffineTransferOpfilter()方法,如此处所示 。这使您可以选择插值技术。

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()在使用scaleBilinear()低于标准绘制方法更快Graphics2D.drawImage()scale2()同样,BiCubic插值最慢,但是在扩展图像时会提供最佳结果。 (为了提高性能,只能将它与scaleBilinear()scaleNearest().进行比较scaleNearest(). )双线性似乎对于缩小图像更好,尽管这是一个艰难的选择。而NearestNeighbor最快,结果最差。双线性似乎是速度和质量之间的最佳折衷。该Image.getScaledInstance()堪称questionable()方法,进行非常差,并返回相同的品质较低,因为最近邻。 (性能编号仅用于扩展图像。)

在此处输入图片说明

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);
}
收藏
评论

不幸的是,即使没有问题,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()的危险

收藏
评论

正如@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
在此处输入图片说明

收藏
评论

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

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

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

尽管顾名思义,Thumbnailator旨在缩小图像,但它也会在默认缩放器实现中使用双线性插值来完成放大图像的工作。


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

收藏
评论

使用imgscalr – Java图像缩放库

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

这对我来说足够快。

收藏
评论
新手导航
  • 社区规范
  • 提出问题
  • 进行投票
  • 个人资料
  • 优化问题
  • 回答问题