Solved

Buffered Image reuse

Posted on 2009-05-17
13
644 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

20 Experts available now in Live!

Get 1:1 Help Now