有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的旋转缓冲图像

我已经用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;
}

我也试过翻译这张图片,但没有多大帮助

我真的很感激任何帮助。多谢各位


共 (1) 个答案

  1. # 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;
    }