Solved

Buffered Image reuse

Posted on 2009-05-17
13
650 Views
Last Modified: 2013-12-22
I have a function like

while(...){
...
BufferedImage  = new BufferedImage(...)
Graphics2D g = (Graphics2D)b.createGraphics();
g.drawImage(...);
}

I mean, i draw an image to different bufferedimages. But i want to use this bufferedimage severel times. (1000 times)
Else, i create buffered image 1000 times, and it is a serious memeory leak.
How to reuse this buuferedimage (change the content)? The next code isnot working properly;

BufferedImage  b = new BufferedImage(...)
while(...){
...
Graphics2D g = (Graphics2D)b.createGraphics();
g.drawImage(...);
}
0
Comment
Question by:btocakci
  • 7
  • 6
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24408606
just 'clear' it each time with a call to fillRect()
0
 

Author Comment

by:btocakci
ID: 24408644
thanks object. I tried it, but now, every time when draw sth to its graphics object i see they are drawn incrementally;
while(...){
Graphics og = (Graphics)outImage.createGraphics();
            og.fillRect(0, 0, outImage.getWidth(), outImage.getHeight());
            og.drawImage(stopImage, 0, 0, size.width, size.height, null);
         og.drawRect(...);
}

I see all the rectangles in images. and they are still drawn the same image.

0
 
LVL 92

Expert Comment

by:objects
ID: 24408651
what are you doing with the images?

0
 

Author Comment

by:btocakci
ID: 24408659

I get frames from a video with jmf. Write it into bufferedimage. Process the image. And draw rectangles into proper positions.

// get frames from video

BufferToImage stopBuffer = new BufferToImage((VideoFormat) frame.getFormat());             

Image stopImage = stopBuffer.createImage(frame);
 

// draw the frame into bufferedimage

outImage = new ProcessableBufImg(size.width, size.height, BufferedImage.TYPE_INT_RGB);
 

while(...){

Graphics og = (Graphics)outImage.getGraphics();

            og.fillRect(0, 0, outImage.getWidth(), outImage.getHeight());

            og.drawImage(stopImage, 0, 0, size.width, size.height, null);
 

// here process the bfferedimage and then draw rectangles
 

og.drawRect(...);

}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24408689
what do you do once you have painted each image?

0
 

Author Comment

by:btocakci
ID: 24408720

// this is my buffer 

static ArrayList<ProcessableBufImg> images = new ArrayList<ProcessableBufImg>();
 

// after painting them i put them to this vector

images.add(outimage);
 

//then  i write this vector's all elements to file
 

ImageIO.write(images.elementAt(i), "jpg", new File(GlobalOpt.outputDirectory+"map\\map"+i+".jpg"));

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:btocakci
ID: 24408729
and i want to change it. once i process, i directly want to write it into file.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 24408743
if you're adding them to a list then you need to create a new ProcessableBufImg for every loop iteration.

0
 

Author Comment

by:btocakci
ID: 24408749
aahh, sorry. they must stay in this arraylist.

just after drawing, i try

ImageIO.write(outimage, "jpg", new File(GlobalOpt.outputDirectory+"map\\map"+i+".jpg"));

and it worked as i want. But after put the image into array list and write to file from beginning of the list to the end, it didnot work.
0
 

Author Comment

by:btocakci
ID: 24408753
>> if you're adding them to a list then you need to create a new ProcessableBufImg for every loop iteration.

that means, i must create 1000 of them?
0
 
LVL 92

Expert Comment

by:objects
ID: 24408757
yes, because you create a list. Otherwise you are adding the same image to your list 1000 times

0
 

Author Comment

by:btocakci
ID: 24408765
ooh, that is what i was scared of.
so, there is no way to decrease the memory leakage?
0
 
LVL 92

Expert Comment

by:objects
ID: 24408782
not if you need all 1000 images
you would need to work out a way so you didn't need all the images in a list

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 39
eclipse package explorer vs project explorer view 2 84
Java SE 8u111  Lot of stuff broke 11 54
oracle 11g 23 50
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now