Converting a BufferedImage to another type
Asked Answered
H

1

22

The most convenient method to read an image from a source (Files, InputStreams, URLs) is:

BufferedImage myImage = ImageIO.read( source );

But then, how to convert myImage to a BufferedImage.TYPE_USHORT_565_RGB format?

Hob answered 19/11, 2011 at 12:32 Comment(2)
@user172825 Welcome to StackOverflow! I recommend you to change your username.Residual
This is very helpful! +1Misfit
S
39

You can create a new BufferedImage of the required type and then draw the original image on it, something like:

    BufferedImage bufImg = ImageIO.read( imageURL );
    BufferedImage convertedImg = new BufferedImage(bufImg.getWidth(), bufImg.getHeight(), BufferedImage.TYPE_USHORT_565_RGB);
    convertedImg.getGraphics().drawImage(bufImg, 0, 0, null);
Scrofula answered 19/11, 2011 at 13:1 Comment(2)
This is an old question, but once should not forget to use dispose() after using drawImage().Henriettahenriette
I know this is a common method, but it is also super slow. Is there anything faster?Teal

© 2022 - 2024 — McMap. All rights reserved.