我一直想弄清楚如何翻转图像有一段时间了,但还没弄清楚。我在用 Graphics2D 画 Image 与
Graphics2D
Image
g2d.drawImage(image, x, y, null)
我只需要一个方法来翻转图像的水平或垂直轴。如果你愿意,你可以在github上查看完整的源代码。
tzcvj98z1#
将图像垂直旋转180度
File file = new File(file_Name); FileInputStream fis = new FileInputStream(file); BufferedImage bufferedImage = ImageIO.read(fis); //reading the image file AffineTransform tx = AffineTransform.getScaleInstance(-1, -1); tx.translate(-bufferedImage.getWidth(null), -bufferedImage.getHeight(null)); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); bufferedImage = op.filter(bufferedImage, null); ImageIO.write(bufferedImage, "jpg", new File(file_Name));
wkftcu5l2#
翻转图像最简单的方法是对其进行负缩放。例子:
g2.drawImage(image, x + width, y, -width, height, null);
它会水平翻转。这将使其垂直翻转:
g2.drawImage(image, x, y + height, width, -height, null);
64jmpszr3#
你可以在你的电脑上使用变换 Graphics ,这样可以很好地旋转图像。下面是一个示例代码,您可以使用它来实现这一点:
Graphics
AffineTransform affineTransform = new AffineTransform(); //rotate the image by 45 degrees affineTransform.rotate(Math.toRadians(45), x, y); g2d.drawImage(image, m_affineTransform, null);
ql3eal8s4#
您需要知道图像的宽度和高度,以确保其正确缩放:
int width = image.getWidth(); int height = image.getHeight();
然后,你需要画出来:
//Flip the image both horizontally and vertically g2d.drawImage(image, x+(width/2), y+(height/2), -width, -height, null); //Flip the image horizontally g2d.drawImage(image, x+(width/2), y-(height/2), -width, height, null); //Flip the image vertically g2d.drawImage(image, x-(width/2), y+(height/2), width, -height, null);
不管怎样,我就是这样做的。
g2ieeal75#
从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);
5条答案
按热度按时间tzcvj98z1#
将图像垂直旋转180度
wkftcu5l2#
翻转图像最简单的方法是对其进行负缩放。例子:
它会水平翻转。这将使其垂直翻转:
64jmpszr3#
你可以在你的电脑上使用变换
Graphics
,这样可以很好地旋转图像。下面是一个示例代码,您可以使用它来实现这一点:ql3eal8s4#
您需要知道图像的宽度和高度,以确保其正确缩放:
然后,你需要画出来:
不管怎样,我就是这样做的。
g2ieeal75#
从http://examples.javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image: