Solved

Buffered Image reuse

Posted on 2009-05-17
13
663 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 41
MySQL  on Tomcat 8 70
throw exception 21 58
jsp login check 12 43
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 …

828 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