How do you clone a BufferedImage

I have an object which has many bufferedimages in it, I want to create a new object copying all the bufferedimages into the new object, but these new images may be altered and i don't want the original object images to be altered by altering the new objects images.

is that clear?

Is this possible to do and can anyone suggest a good way to do it please? I have thought of getSubImage but read somewhere that any changes to the subimage are relected back to the parent image.

I just want to be able to get a fresh entirely separate copy or clone of a BufferedImage

113
задан Cœur 26 March 2017 в 04:28
поделиться

2 ответа

Что-то вроде этого?

static BufferedImage deepCopy(BufferedImage bi) {
 ColorModel cm = bi.getColorModel();
 boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
 WritableRaster raster = bi.copyData(null);
 return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
167
ответ дан 24 November 2019 в 02:41
поделиться

Класс BufferedImage не реализует интерфейс Cloneable. Таким образом, метод клонирования не отменяется. Вот альтернатива технике глубокого копирования: Совет по Java 76: альтернатива технике глубокого копирования

4
ответ дан 24 November 2019 в 02:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: