繁体   English   中英

旋转具有透明背景的BufferedImage

[英]Rotate BufferedImage with transparent background

我有一个透明背景的图像。 我想将此图像旋转到特定角度,并保持生成的图像的透明背景。 为此,我使用以下方法:

public static BufferedImage rotateImage(BufferedImage image, double angle, Color backgroundColor) {
    System.out.println(image.getType());
    double theta = Math.toRadians(angle);
    double sin = Math.abs(Math.sin(theta));
    double cos = Math.abs(Math.cos(theta));
    int w = image.getWidth();
    int h = image.getHeight();
    int newW = (int) Math.floor(w * cos + h * sin);
    int newH = (int) Math.floor(h * cos + w * sin);

    BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
    Graphics2D g2d = tmp.createGraphics();
    if (backgroundColor != null) {
        g2d.setColor(backgroundColor);
        g2d.fillRect(0, 0, newW, newH);
    } 
    g2d.fillRect(0, 0, newW, newH);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.translate((newW - w) / 2, (newH - h) / 2);
    g2d.rotate(theta, w / 2, h / 2);
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    return tmp;
}

我用background = null调用它:

BufferedImage image = ImageIO.read(file);
rotateImage(image, 4, null);
ImageIO.write(bi, "PNG", new File("image.png"));

但是生成的image.png的背景是白色。 我在做错什么以及如何正确保持image.png的透明背景?

我对Graphics.drawImage()的行为有些疑惑。 也许其他人可以对此发表评论。

但是, Graphics2D.drawRenderedImage()可以处理。 它需要一个AffineTransform来控制旋转。 下面的示例很好地工作。 您可能对最终图像尺寸和旋转图像的位置有其他要求。

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageRotation {

    public static void main(String[] args) {
        ImageRotation rotation = new ImageRotation();
        rotation.rotate("input.png", 45, "output.png");
    }

    public void rotate(String inputImageFilename, double angle, String outputImageFilename) {

        try {
            BufferedImage inputImage = ImageIO.read(new File(inputImageFilename));
            BufferedImage outputImage = rotateImage(inputImage, angle);
            ImageIO.write(outputImage, "PNG", new File(outputImageFilename));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private BufferedImage rotateImage(BufferedImage sourceImage, double angle) {
        int width = sourceImage.getWidth();
        int height = sourceImage.getHeight();
        BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = destImage.createGraphics();

        AffineTransform transform = new AffineTransform();
        transform.rotate(angle / 180 * Math.PI, width / 2 , height / 2);
        g2d.drawRenderedImage(sourceImage, transform);

        g2d.dispose();
        return destImage;
    }
}

更新资料

尽管以上代码适用于大多数PNG,但不适用于蝶形动物正在使用的图像 我分析了图像:

  • 它是没有调色板的灰度图像(PNG颜色类型0)。
  • 它使用具有2个字节长的tRNS块的简单透明性。

据我所知这是完全合法的。 但是,ImageIO没有实现此组合。 如果图像没有调色板,则它只会忽略tRNS块,因此会忽略透明度信息。 这很可能是一个错误。

现在,您基本上有两个选择:

  1. 寻找替代库来读取PNG文件。
  2. 阅读PNG文件后,请修复透明度。 这仅在知道图像使用特定问题格式的情况下才有效。

有效的PNG文件的输入和输出

输入图片:

输入图像

输出图像:

输出图像

暂无
暂无

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

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