使用Graphics2D翻转图像 [英] Flip Image with Graphics2D
本文介绍了使用Graphics2D翻转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直想弄清楚如何翻转图像一段时间,但还没想出来。
I've been trying to figure out how to flip an image for a while, but haven't figured out yet.
我正在使用 Graphics2D
使用
g2d.drawImage(image, x, y, null)
我只需要一种方法在水平轴或垂直轴上翻转图像。
I just need a way to flip the image on the horizontal or vertical axis.
如果您需要,可以查看 github上的完整源代码。
If you want you can have a look at the full source on github.
推荐答案
来自 http:// examples。 javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image :
// Flip the image vertically
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
// Flip the image horizontally
tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-image.getWidth(null), 0);
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
// Flip the image vertically and horizontally; equivalent to rotating the image 180 degrees
tx = AffineTransform.getScaleInstance(-1, -1);
tx.translate(-image.getWidth(null), -image.getHeight(null));
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
这篇关于使用Graphics2D翻转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文