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

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

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

最佳答案

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

所示片段说明了resampling,而不是cropping;这个相关的answer解决了issue;检查了一些相关示例here

关于java - 如何缩放BufferedImage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4216123/

10-09 04:09