How to calculate java BufferedImage filesize

后端 未结 6 1465
闹比i
闹比i 2021-01-04 04:41

I have a servlet based application that is serving images from files stored locally. I have added logic that will allow the application to load the image file to a Buffered

6条回答
  •  伪装坚强ぢ
    2021-01-04 05:06

    You can calculate the size of a BufferedImage in memory very easily. This is because it is a wrapper for a WritableRaster that uses a DataBuffer for it's backing. If you want to calculate it's size in memory you can get a copy of the image's raster using getData() and then measuring the size of the data buffer in the raster.

    DataBuffer dataBuffer = bufImg.getData().getDataBuffer();
    
    // Each bank element in the data buffer is a 32-bit integer
    long sizeBytes = ((long) dataBuffer.getSize()) * 4l;
    long sizeMB = sizeBytes / (1024l * 1024l);`
    

提交回复
热议问题