[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Buffered Image reuse

Posted on 2009-05-17
13
Medium Priority
?
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

650 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