【问题标题】:Rotating buffered image in Java在Java中旋转缓冲图像
【发布时间】:2017-12-23 14:21:31
【问题描述】:

我在 Java 中编写了一个图像旋转方法(允许旋转 90、180 和 270 度),但它似乎无法正常工作。我显然做错了什么,但我完全无法弄清楚是什么。输出的问题是图像确实被旋转了,但是图像中有黑色部分,就像图像不在正确的位置一样。

我的第一次尝试是不使用我用作目标的结果变量,而是这样做:

return affineTransformOp.filter(bufferedImage, null);

而且旋转很好,图像没有黑色部分,但是颜色很奇怪,就像如果改变了一些颜色,肤色就变成了红色。所以当我看到其他人有同样的问题时,我就改变了它。

这是我目前拥有的:

private BufferedImage rotateImage(ImageData imageData, BufferedImage bufferedImage) {
    AffineTransform affineTransform = new AffineTransform();

    affineTransform.rotate(imageData.getRotation().getRotationAngle(), bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);

    AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR);

    BufferedImage result = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(), bufferedImage.getType());

    affineTransformOp.filter(bufferedImage, result);

    return result;
}

我也尝试过翻译图片,但效果不大。

我非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: java rotation bufferedimage affinetransform


    【解决方案1】:

    如果以后有人遇到同样的问题,找到答案。

    这是解决了我的问题的修改后的 Java 方法:

    private BufferedImage rotateImage(ImageRotation imageRotation, BufferedImage bufferedImage) {
        AffineTransform affineTransform = new AffineTransform();
    
        if (ImageRotation.ROTATION_90.equals(imageRotation) || ImageRotation.ROTATION_270.equals(imageRotation)) {
            affineTransform.translate(bufferedImage.getHeight() / 2, bufferedImage.getWidth() / 2);
            affineTransform.rotate(imageRotation.getRotationAngle());
            affineTransform.translate(-bufferedImage.getWidth() / 2, -bufferedImage.getHeight() / 2);
    
        } else if (ImageRotation.ROTATION_180.equals(imageRotation)) {
            affineTransform.translate(bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);
            affineTransform.rotate(imageRotation.getRotationAngle());
            affineTransform.translate(-bufferedImage.getWidth() / 2, -bufferedImage.getHeight() / 2);
    
        } else {
            affineTransform.rotate(imageRotation.getRotationAngle());
        }
    
        AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR);
    
        BufferedImage result;
    
        if (ImageRotation.ROTATION_90.equals(imageRotation) || ImageRotation.ROTATION_270.equals(imageRotation)) {
            result = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(), bufferedImage.getType());
    
        } else {
            result = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType());
        }
    
        affineTransformOp.filter(bufferedImage, result);
    
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      相关资源
      最近更新 更多