Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Image to BufferedImage with no init?

I am wondering is there a way to convert Image to BufferedImage without code like a

new BufferedImage(...)

because every new init makes app run slower , moreover, if it is in paint() method :(

Please advise the most optimal conversion way.

Thanks

like image 861
user592704 Avatar asked Dec 09 '22 09:12

user592704


1 Answers

No. Not unless the original Image happens to be a BufferedImage already. Then you can just do a cast:

BufferedImage bufImg = null;
if (origImage instanceof BufferedImage) {
    bufImg = (BufferedImage) origImage;
else {
    bugImg = new BufferedImage(...);
    // proper initialization
}

If it's not a BufferedImage it may very well be for instance a VolatileImage (the other concrete subclass in the API).

From the docs on volatile image:

VolatileImage is an image which can lose its contents at any time due to circumstances beyond the control of the application (e.g., situations caused by the operating system or by other applications).

As you may understand, such image can not provide the same interface as a BufferedImage, thus the only way to get hold of a BufferedImage is to create one, and draw the original image on top of it.

like image 117
aioobe Avatar answered Dec 13 '22 23:12

aioobe