SoFunction
Updated on 2025-04-06

Java obtains the size, width, and height of the image

Java gets the size, width, and height of the image

File object (the object is an image)

Code:

public class Test {
    public static void main(String[] args) throws Exception {
        // File object        File file = new File("C:\\test\\20220619\\");
        // File size; where () gets bytes, divide by 1024 to get the file size in kb        long size = () / 1024;
        // Image object        BufferedImage bufferedImage = (new FileInputStream(file));
        // Width        int width = ();
        // high        int height = ();
        // Print information        ("Image size: %skb; Image width: %s pixel; Image height: %s pixel", size, width, height);
    }
}

result:

Image size: 4424kb; Image width: 7360 pixels; Image height: 4912 pixels

MultipartFile object (the object is an image)

Code:

public class Test {
    public static void main(String[] args) throws Exception {
        // File object        MultipartFile file = Suppose this is the object transmitted from the front end;
        // File size; where () gets bytes, divide by 1024 to get the file size in kb        long size = () / 1024;
        // Image object        BufferedImage bufferedImage = (());
        // Width        int width = ();
        // high        int height = ();
        // Print information        ("Image size: %skb; Image width: %s pixel; Image height: %s pixel", size, width, height);
    }
}

result:

Image size: 4424kb; Image width: 7360 pixels; Image height: 4912 pixels

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.