如何缩放缓冲图像在javadocs之后,我尝试将BufferedImage没有成功,这是我的代码:BufferedImage image = MatrixToImageWriter.getBufferedImage(encoded);Graphics2D grph = image.createGraphics();grph.scale(2.0, 2.0);grph.dispose();我不明白它为什么不起作用,有什么帮助吗?
3 回答
倚天杖
TA贡献1828条经验 获得超3个赞
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);
墨色风雨
TA贡献1853条经验 获得超6个赞
不幸的是,getScaledInstance()的性能很差,如果没有问题的话。
另一种方法是创建一个新的BufferedImage,并在新的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,newHL.8表示新的BufferedImage大小,并且必须正确计算。在因素比例方面:
int newWidth = new Double(original.getWidth() * widthFactor).intValue();int newHeight = new Double(original.getHeight() * heightFactor).intValue();
添加回答
举报
0/150
提交
取消