wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Image in memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image in memory?" Watch "Image in memory?" New topic
Author

Image in memory?

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I am having trouble with my code to rotate an image.
The code loads a file and then rotates it clockwise or anticlockwise by 90 degrees.

The problem is that the first time rotation after the image is loaded, the rotation works fine, any time after that, the image looks like it just rotates itself and makes itself half the size and smaller.

I made the code show me outputs and it is almost as if the code holds the image in memory from one call to the method to the next call even though I'm telling it not to.

Here's the code

public ImageIcon rotateImage(boolean clockwise){

File uncompressedFile = new File (FolderLocations.UncompressedImageLOCATION);
Image awtImage = new ImageIcon(uncompressedFile.getPath()).getImage();

RenderedImage ri = JAI.create( "awtImage", awtImage );

ParameterBlock pb1 = new ParameterBlock();
pb1.addSource(ri);

pb1.add(0.5f);
pb1.add(0.5f);

if(clockwise) pb1.add(new Double(Math.toRadians(90)).floatValue());
else if(!clockwise) pb1.add(new Double(Math.toRadians(-90)).floatValue());

pb1.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
RenderedOp op = JAI.create("rotate",pb1);

PlanarImage myPlanar = op.createInstance();
BufferedImage buff = myPlanar.getAsBufferedImage();
ImageIcon myIcon = new ImageIcon(buff);

try{
boolean found = ImageIO.write(buff, "jpg", uncompressedFile);
}catch(IOException ioe){}


ri = null;
pb1 = null;
awtImage = null;
uncompressedFile = null;
System.gc();

return myIcon;

}

Any suggestions would be greately appreciated.

Thanks
Rachel
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
I've not looked at JAI at all, so I may be in the dark a bit.

The problem you have is that when you call this method more than once, you end up with an image thats smaller and rotated?

Have you tried hardcoding your source and then calling the method a few times?

You can also use the ImageIcon constructor with a string for the file name, then you won't have to create a file.

Regards,
Aaron R>
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I tried to hardcode the source, I also tried making the method static and declaring all variables to null when I was done. I even tried making a new instance of the class everytime I wanted to use the method. None seem to work.

Cheers,
Rachel
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I found the answer. It's kind of long, so if there's someone reading this now or in future, you can email me for the solution.

Cheers,
rachel
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
Go ahead and post the gist of it, then everyone will get to learn some more!

Regards,
Aaron R>
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
OK, here's the condensed version.

Cheers,
Rachel

public ImageIcon rotateImage(boolean clockwise){

File uncompressedMyFile = new File(FolderLocations.UncompressedImageLOCATION);

PlanarImage im0 = (PlanarImage)JAI.create("fileload", FolderLocations.UncompressedImageLOCATION);
//this here above was the answer to the program holding the reference to the file. Doing it this way fixed the problem.

ParameterBlock parameterBlock = new ParameterBlock();
parameterBlock.addSource(im0);

if(clockwise) parameterBlock.add(TransposeDescriptor.ROTATE_90);
else if(!clockwise) parameterBlock.add(TransposeDescriptor.ROTATE_270);

PlanarImage newImage = JAI.create("transpose", parameterBlock).createSnapshot();



BufferedImage buff = newImage.getAsBufferedImage();
ImageIcon myIcon = new ImageIcon(buff);


try{
boolean found = ImageIO.write(buff, "jpg", uncompressedMyFile);
}catch(IOException ioe){ }

uncompressedFile = new File(FolderLocations.UncompressedImageLOCATION);

return myIcon;

}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Image in memory?
 
Similar Threads
Rotating a buffered image
Rotating an image
JAI rotate leaves 1px black border - why?
Code to print , rotate, zoom an image from an applet
Image Rotation