Solved

Buffered Image reuse

Posted on 2009-05-17
13
678 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
Independent Software Vendors: 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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