【问题标题】:Flip Image with Graphics2D使用 Graphics2D 翻转图像
【发布时间】:2012-03-22 11:20:11
【问题描述】:

我一直想弄清楚如何翻转图像,但还没有弄清楚。

我正在使用Graphics2D

绘制Image
g2d.drawImage(image, x, y, null)

我只需要一种在水平或垂直轴上翻转图像的方法。

如果您愿意,可以查看full source on github

【问题讨论】:

  • 不要让我们查看您的整个源代码,而是发送SSCCE
  • 如果你用谷歌搜索“Graphics2D 旋转图像”,你会发现很多教程

标签: java image swing graphics2d


【解决方案1】:

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

【讨论】:

【解决方案2】:

翻转图像的最简单方法是对图像进行负缩放。 示例:

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

这会水平翻转它。这将垂直翻转它:

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

【讨论】:

  • 这几乎是好的。它应该像 g2.drawImage(image, x+height, y, width, -height, null);这样做的原因是因为负比例会使图像向左移动,所以你必须补偿这种移动。
  • 是的 +1 @T.G 来平衡消极情绪。
  • 我相信这比使用 AffineTransform 更有效。如果我错了,任何人都可以纠正我。 (+1)
【解决方案3】:

您可以在您的Graphics 上使用转换,这应该可以很好地旋转图像。下面是一个示例代码,您可以使用它来实现这一点:

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

【讨论】:

    【解决方案4】:

    将图像垂直旋转 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));
    

    【讨论】:

      【解决方案5】:

      您需要知道图片的宽度和高度,以确保正确缩放:

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

      反正我就是这么干的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 2018-11-14
        • 2011-08-02
        • 2021-02-08
        • 1970-01-01
        相关资源
        最近更新 更多