Java:复制不仅仅是一个新的BufferedImage



我正在尝试在三个新的bufferedimage中复制相同的bufferedimage内容,这是我的代码:

    ColorModel cm = image.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = image.copyData(null);
    BufferedImage[] images = {
        new BufferedImage(cm, raster, isAlphaPremultiplied, null),
        new BufferedImage(cm, raster, isAlphaPremultiplied, null),
        new BufferedImage(cm, raster, isAlphaPremultiplied, null)
    };

即使我以不同的方式编辑这些图像,结果也相同。我敢肯定这一切都可以,因为如果我只有一个副本,则代码可以正常工作,但不超过一个。

如何管理这样的事情?

我得到了它。即使我的bufferedimages不同,colormodel和writableraster也仅引用相同的对象。

如果有人有我的问题,请尝试这样的事情:

    private BufferedImage copyImage() {
        ColorModel cm = image.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = image.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    }
    public BufferedImage[] copyUpdatedImage() {
        BufferedImage[] images = {
            copyImage(),
            copyImage(),
            copyImage()
        };
    }

最新更新