How to get the format(ex:jpen,png,gif) of image file (BufferedImage) in java
Asked Answered
I

2

10

i want to get the buffered image format or type of image using java,

 InputStream stream = request.getResponseStream();   
 BufferedImage image=  ImageIO.read(stream);
 LOGGER.info(image);
Injector answered 11/7, 2012 at 4:23 Comment(1)
You want to be able to check the format? (e.g. jpeg png bmp)Frolic
G
15

Refer the javax.imageio.ImageIO.read() code

This code originally implemented in groovy but translated into java so It may have a syntax error.

public String read(InputStream input) throws IOException {
    ImageInputStream stream = ImageIO.createImageInputStream(input);

    Iterator iter = ImageIO.getImageReaders(stream);
    if (!iter.hasNext()) {
        return null;
    }
    ImageReader reader = (ImageReader) iter.next();
    ImageReadParam param = reader.getDefaultReadParam();
    reader.setInput(stream, true, true);
    BufferedImage bi;
    try {
        bi = reader.read(0, param);
        return reader.getFormatName();
    } finally {
        reader.dispose();
        stream.close();
    }
}
Glennaglennie answered 20/7, 2015 at 9:59 Comment(0)
L
-2

This code might help you to get the extension of image

public static String getFormate(String ImageName) {

    return (ImageName.substring(ImageName.indexOf('.'),ImageName.length()));
}

In the above code first it searches dot(.) and with help of substring() it will return the extension

Linear answered 22/3, 2016 at 13:52 Comment(1)
You've got many downvotes because no explaination how to get ImageName from InputStream was provided. Practically there is no solutionHydrology

© 2022 - 2024 — McMap. All rights reserved.