Example usage for javax.imageio ImageReader getFormatName

List of usage examples for javax.imageio ImageReader getFormatName

Introduction

In this page you can find the example usage for javax.imageio ImageReader getFormatName.

Prototype

public String getFormatName() throws IOException 

Source Link

Document

Returns a String identifying the format of the input source.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    URL[] urls = { new URL("http://yourserver/small.png") };
    for (URL url : urls) {
        ImageInputStream iis = ImageIO.createImageInputStream(url.openStream());
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);

        System.out.println("url= " + url.getPath());
        while (readers.hasNext()) {
            ImageReader read = readers.next();
            System.out.println("format name = " + read.getFormatName());
        }//from w  ww.ja  v a2s . c o  m
        System.out.println();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ImageInputStream imageStream = ImageIO.createImageInputStream(new URL("").openStream());
    Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
    ImageReader reader = null;
    if (!readers.hasNext()) {
        imageStream.close();//from www  . j  a v  a  2  s .  c o m
        return;
    } else {
        reader = readers.next();
    }
    String formatName = reader.getFormatName();
    if (!formatName.equalsIgnoreCase("jpeg") && !formatName.equalsIgnoreCase("png")
            && !formatName.equalsIgnoreCase("gif")) {
        imageStream.close();
        return;
    }
    reader.setInput(imageStream, true, true);
    BufferedImage theImage = reader.read(0);
    reader.dispose();
    imageStream.close();
}

From source file:Main.java

private static String getFormatName(Object o) {
    try {//www .j av  a  2 s .c  o m
        ImageInputStream iis = ImageIO.createImageInputStream(o);
        Iterator iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            return null;
        }
        ImageReader reader = (ImageReader) iter.next();
        iis.close();

        return reader.getFormatName();
    } catch (IOException e) {
    }
    return null;
}

From source file:ImageUtilities.java

/**
 * Returns the format of an image (as string).
 * /*w  ww .  j  a  va2 s  .c o m*/
 * @param stream
 *            image source stream
 * 
 * @return format stream (e.g. "JPEG")
 * 
 * @throws IOException
 *             if an I/O error occured
 */
public static String getFormat(InputStream stream) throws IOException {
    ImageInputStream iis = ImageIO.createImageInputStream(stream);
    Iterator iter = ImageIO.getImageReaders(iis);
    if (!iter.hasNext()) {
        throw new IOException("Unsupported image format!");
    }
    ImageReader reader = (ImageReader) iter.next();
    iis.close();
    return reader.getFormatName();
}

From source file:de.javagl.jgltf.model.io.GltfUtils.java

/**
 * Tries to detect the format of the given image data, and return the
 * corresponding string of the <code>"image/..."</code> MIME type.
 * This may, for example, be <code>"png"</code> or <code>"gif"</code>
 * or <code>"jpeg"</code> (**not** <code>"jpg"</code>!)
 *  // w  w  w .  j  a v a  2 s.c  o  m
 * @param imageData The image data
 * @return The image format string
 * @throws IOException If the image format can not be detected
 */
private static String guessImageMimeTypeString(ByteBuffer imageData) throws IOException {
    ImageReader imageReader = null;
    try {
        imageReader = findImageReader(imageData);
        return imageReader.getFormatName();
    } finally {
        if (imageReader != null) {
            imageReader.dispose();
        }
    }

}

From source file:net.sf.webphotos.tools.Thumbnail.java

private static String getFormatName(Object o) {
    try {//from   w  ww  .  ja  v  a 2 s .c om
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream(o);

        // Find all image readers that recognize the image format
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            // No readers found
            return null;
        }

        // Use the first reader
        ImageReader reader = iter.next();

        // Close stream
        iis.close();

        // Return the format name
        return reader.getFormatName();
    } catch (Exception e) {
        // The image could not be read
        return null;
    }
}

From source file:common.utils.ImageUtils.java

/**
 * closes iis// www.j a  va  2 s . c  o m
 * @param iis
 * @return image from given stream or null if no readers found
 * @throws java.io.IOException
 */
public static BufferedImageHolder readImage(ImageInputStream iis) throws IOException {
    ImageReader reader = getImageReader(iis);
    if (reader == null) {
        iis.close();
        return null;
    }
    reader.setInput(iis, true, true);
    ImageReadParam param = reader.getDefaultReadParam();
    String format_name = reader.getFormatName();
    BufferedImage bi;
    try {
        bi = reader.read(0, param);
    } finally {
        reader.dispose();
        iis.close();
    }
    return new BufferedImageHolder(bi, format_name);
}

From source file:com.lm.lic.manager.util.GenUtil.java

public static String senseImageFormat(byte[] icon) throws Exception {
    try {/*from  w  w w .j  a v  a 2 s  .  c  o  m*/
        ByteArrayInputStream bais = new ByteArrayInputStream(icon);
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream(bais);
        ImageIO.getReaderFormatNames();

        // Find all image readers that recognize the image format
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            return null;
        }
        ImageReader reader = iter.next();
        iis.close();
        return reader.getFormatName();
    } catch (IOException e) {
    }

    return null;
}

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static String getImageFormat(File inputImage) throws IOException {
    // Return a string describing the image format
    String format = null;//from  w  ww .j a va 2s.c o m
    ImageInputStream iis = ImageIO.createImageInputStream(inputImage);
    Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
    if (!iter.hasNext()) {
        throw new RuntimeException("No readers found! I don't think this is an image file");
    }
    ImageReader reader = iter.next();
    format = reader.getFormatName();
    return format;
}

From source file:com.occamlab.te.parsers.ImageParser.java

/**
 * Determines the type of image, or null if not a valid image type
 * /*from  w w  w  . j a  va  2  s. c o  m*/
 * @param imageLoc
 *            the string location of the image (uri syntax expected)
 * @return String the name of the image type/format, or null if not valid
 */
public static String getImageType(String imageLoc) {

    // Get the image as an InputStream
    InputStream is = null;
    try {
        URI imageUri = new URI(imageLoc);
        URL imageUrl = imageUri.toURL();
        is = imageUrl.openStream();
    } catch (Exception e) {
        jlogger.log(Level.SEVERE, "getImageType", e);

        return null;
    }

    // Determine the image type and return it if valid
    try {
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream(is);

        // Find all image readers that recognize the image format
        Iterator iter = ImageIO.getImageReaders(iis);

        // No readers found
        if (!iter.hasNext()) {
            return null;
        }

        // Use the first reader
        ImageReader reader = (ImageReader) iter.next();
        iis.close();

        // Return the format name
        return reader.getFormatName();
    } catch (IOException e) {
        jlogger.log(Level.SEVERE, "getImageType", e);

    }

    // The image could not be read
    return null;
}