• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on image assignment  RSS feed

 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a buffered image:

BufferedImage origImg;
origImg = someImage.jpg

and I say:

BufferedImage workingImg;
workingImg = origImg;

Is workingImg an independent copy of someImage.jpg or does it simply point to the memory location of origImg?

To make an independent copy, do I have to say:

workingImg = new BufferedImage(origImg.getWidth(), origImg.getHeight(), origImg.getType());
workingImg = origImg;

or do I have to go back to the original source of the image file on the drive?

Thanks for any explanations.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, you can't get a BufferedImage from a filename, so maybe you should start here: http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

To make an independent copy, you can either read the image again from source, or create a new BufferedImage of the same size and draw the already loaded image onto it.
 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Although I did not go into it in my post, I am assigning the image to origImg as:

origImg = ImageIO.read(new File(fn));

My issue is that I need another instance of this image and following your response, I think I will get it by reading the file from disk a second time as:

workingImg = ImageIO.read(new File(fn));

I was concerned that this would be slow (which it isn't) or that there was a more professional way of doing it.

If there are any more comments or suggestions, I am all ears. Otherwise I will mark this topic as closed.

By the way, what does the statement workingImg = origImg actually do?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Sale wrote:what does the statement workingImg = origImg actually do?

It stores the same reference to the image in another variable. So now you have two references to the same image data. Since you said you wanted an independent copy of the image -which I understand to mean that you could change one without changing the other- that is not what you want.
 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. This clarifies a lot for me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!