简体   繁体   English

使用Graphics2D翻转图像

[英]Flip Image with Graphics2D

I've been trying to figure out how to flip an image for a while, but haven't figured out yet. 我一直想弄清楚如何翻转图像一段时间,但还没想到。

I'm using Graphics2D to draw an Image with 我正在使用Graphics2D来绘制Image

g2d.drawImage(image, x, y, null)

I just need a way to flip the image on the horizontal or vertical axis. 我只需要一种在水平轴或垂直轴上翻转图像的方法。

If you want you can have a look at the full source on github . 如果你想要,你可以看看github上的完整源代码

From http://examples.javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image : 来自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);

The easiest way to flip the image is by negative scaling it. 翻转图像的最简单方法是对其进行负缩放。 Example: 例:

g2.drawImage(image, x + width, y, -width, height, null);

That will flip it horizontally. 那会水平翻转它。 This will flip it vertically: 这将垂直翻转:

g2.drawImage(image, x, y + height, width, -height, null);

You can use a transform on your Graphics , that should rotate the image just fine. 您可以在Graphics上使用变换,这应该可以很好地旋转图像。 Below is a sample code that you can use to acheive this: 下面是一个示例代码,您可以使用它来实现此目的:

AffineTransform affineTransform = new AffineTransform(); 
//rotate the image by 45 degrees 
affineTransform.rotate(Math.toRadians(45), x, y); 
g2d.drawImage(image, m_affineTransform, null); 

Rotate Image Vertical 180 Degrees 将图像垂直旋转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));

You need to know the width and height of the image to make sure it's properly scaled: 您需要知道图像的宽度和高度,以确保它正确缩放:

int width = image.getWidth(); int height = image.getHeight();

Then, you need to draw it: 然后,你需要绘制它:

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

This is how I do it, anyways. 无论如何,我就是这样做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM