Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone Image?

I have an Image. I need to make a exactly copy of it and save it to BufferedImage, but there is no Image.clone(). The thing should be inside a calculating loop and so it should be really fast, no pixel-by-pixel copying. What's the best in perfomance method to do this?

like image 646
Cenius Avatar asked Jan 14 '12 18:01

Cenius


2 Answers

You can draw to a buffered image, so make a blank bufferedImage, create a graphics context from it, and draw your original image to it.

BufferedImage copyOfImage = 
   new BufferedImage(widthOfImage, heightOfImage, BufferedImage.TYPE_INT_RGB);
Graphics g = copyOfImage.createGraphics();
g.drawImage(originalImage, 0, 0, null);
like image 65
Levster Avatar answered Nov 19 '22 09:11

Levster


There is another way:

BufferedImage copyOfImage = image.getSubimage(0, 0, image.getWidth, image.getHeight);
like image 1
Angelo Alvisi Avatar answered Nov 19 '22 10:11

Angelo Alvisi