Make a BufferedImage use less RAM?

后端 未结 6 1336
挽巷
挽巷 2020-12-25 08:02

I have java program that reads a jpegfile from the harddrive and uses it as the background image for various other things. The image itself is stored in a BufferImage

6条回答
  •  情歌与酒
    2020-12-25 08:51

    Use imgscalr:

    http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/

    Why?

    1. Follows best practices
    2. Stupid simple
    3. Interpolation, Anti-aliasing support
    4. So you aren't rolling your own scaling library

    Code:

    BufferedImage thumbnail = Scalr.resize(image, 150);
    
    or
    
    BufferedImage thumbnail = Scalr.resize(image, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH, 150, 100, Scalr.OP_ANTIALIAS);
    

    Also, use image.flush() on your larger image after conversion to help with the memory utilization.

提交回复
热议问题