Java Code Examples for javax.imageio.ImageIO#getImageReadersBySuffix()

The following examples show how to use javax.imageio.ImageIO#getImageReadersBySuffix() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: youkefu   File: FileImageSink.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String preferredOutputFormatName()
{
	String fileExtension = getExtension(destinationFile);

	if (fileExtension != null)
	{
		Iterator<ImageReader> rIter = ImageIO.getImageReadersBySuffix(fileExtension);
		
		if (rIter.hasNext())
		{
			try
			{
				return rIter.next().getFormatName();
			}
			catch (IOException e)
			{
				return ThumbnailParameter.ORIGINAL_FORMAT;
			}
		}
	}
	
	return outputFormat;
}
 
Example 2
Source Project: java-bot-sdk   File: ImageUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Dimension getImageDimension(File imgFile) throws IOException {
    int pos = imgFile.getName().lastIndexOf(".");
    if (pos == -1)
        throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
    String suffix = imgFile.getName().substring(pos + 1);
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    while(iter.hasNext()) {
        ImageReader reader = iter.next();
        try {
            ImageInputStream stream = new FileImageInputStream(imgFile);
            reader.setInput(stream);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            return new Dimension(width, height);
        } catch (IOException e) {
            System.out.println("Error reading: " + imgFile.getAbsolutePath() + " " + e);
        } finally {
            reader.dispose();
        }
    }

    throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
 
Example 3
/**
 * Gets image dimensions for given file
 * 
 * @param imgFile image file
 * @return dimensions of image
 * @throws IOException if the file is not a known image
 */
public static Dimension getImageDimension(File imgFile) throws IOException {
	int pos = imgFile.getName().lastIndexOf(".");
	if (pos == -1)
		throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
	String suffix = imgFile.getName().substring(pos + 1);
	Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
	while (iter.hasNext()) {
		ImageReader reader = iter.next();
		try {
			ImageInputStream stream = new FileImageInputStream(imgFile);
			reader.setInput(stream);
			int width = reader.getWidth(reader.getMinIndex());
			int height = reader.getHeight(reader.getMinIndex());
			return new Dimension(width, height);
		} catch (IOException e) {
			log.warn("Error reading: " + imgFile.getAbsolutePath(), e);
		} finally {
			reader.dispose();
		}
	}

	throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
 
Example 4
Source Project: density-converter   File: ImageUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets image dimensions for given file
 *
 * @param imgFile image file
 * @return dimensions of image
 * @throws IOException if the file is not a known image
 */
public static Dimension getImageDimension(File imgFile) throws IOException {
    int pos = imgFile.getName().lastIndexOf(".");
    if (pos == -1)
        throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
    String suffix = imgFile.getName().substring(pos + 1);
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    if (iter.hasNext()) {
        ImageReader reader = iter.next();
        try {
            ImageInputStream stream = new FileImageInputStream(imgFile);
            reader.setInput(stream);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            return new Dimension(width, height);
        } finally {
            reader.dispose();
        }
    }

    throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
 
Example 5
Source Project: skin-composer   File: Utils.java    License: MIT License 6 votes vote down vote up
public static boolean doesImageFitBox(FileHandle fileHandle, float width, float height) {
    boolean result = false;
    String suffix = fileHandle.extension();
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    if (iter.hasNext()) {
        ImageReader reader = iter.next();
        try (var stream = new FileImageInputStream(fileHandle.file())) {
            reader.setInput(stream);
            int imageWidth = reader.getWidth(reader.getMinIndex());
            int imageHeight = reader.getHeight(reader.getMinIndex());
            result = imageWidth < width && imageHeight < height;
        } catch (IOException e) {
            Gdx.app.error(Utils.class.getName(), "error checking image dimensions", e);
        } finally {
            reader.dispose();
        }
    } else {
        Gdx.app.error(Utils.class.getName(), "No reader available to check image dimensions");
    }
    return result;
}
 
Example 6
private static Dimension getImageDimension(File imgFile) throws IOException {
    int pos = imgFile.getName().lastIndexOf(".");
    if (pos == -1)
        throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
    String suffix = imgFile.getName().substring(pos + 1);
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    while (iter.hasNext()) {
        ImageReader reader = iter.next();
        try {
            ImageInputStream stream = new FileImageInputStream(imgFile);
            reader.setInput(stream);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            return new Dimension(width, height);
        } catch (IOException e) {
            LOG.log(Level.WARNING, "Error reading: " + imgFile.getAbsolutePath(), e);
        } finally {
            reader.dispose();
        }
    }

    throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
 
Example 7
@Override
public Object load(AssetInfo info) throws IOException {
    if (ImageIO.getImageReadersBySuffix(info.getKey().getExtension()) != null){
        boolean flip = ((TextureKey) info.getKey()).isFlipY();
        InputStream in = null;
        try {
            in = info.openStream();
            Image img = load(in, flip);
            if (img == null){
                throw new AssetLoadException("The given image cannot be loaded " + info.getKey());
            }
            return img;
        } finally {
            if (in != null){
                in.close();
            }
        }
    }else{
        throw new AssetLoadException("The extension " + info.getKey().getExtension() + " is not supported");
    }
}
 
Example 8
private java.awt.Dimension getImageSize(File image) throws IOException {
    String filename = image.getName();
    int p = filename.lastIndexOf('.');
    if (p == -1) {
        return null;
    }
    String suffix = filename.substring(p + 1).toLowerCase();
    Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix(suffix);
    if (readers.hasNext()) {
        ImageReader reader = readers.next();
        try {
            try (ImageInputStream in = new FileImageInputStream(image)) {
                reader.setInput(in);
                int width = reader.getWidth(reader.getMinIndex());
                int height = reader.getHeight(reader.getMinIndex());
                return new java.awt.Dimension(width, height);
            }
        } finally {
            reader.dispose();
        }
    } else {
        return null;
    }
}
 
Example 9
private static void testGetReaderFileSuffixes() {
    String[] suffixes = ImageIO.getReaderFileSuffixes();
    for (String s : suffixes) {
        Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(s);
        if (!it.hasNext()) {
            throw new RuntimeException("getReaderFileSuffixes returned " +
                                       "an unknown suffix: " + s);
        }
    }
}
 
Example 10
public static void main(String args[]) throws Exception {
    Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix("simp");
    ImageReader simpReader = null;
    if (readers.hasNext()) {
        simpReader = readers.next();
        System.out.println("reader="+simpReader);
    }
    if (simpReader == null) {
        throw new RuntimeException("Reader not found.");
    }

    ImageReaderSpi spi = simpReader.getOriginatingProvider();
    IIOMetadataFormat spiFmt =
        spi.getImageMetadataFormat("simp_metadata_1.0");
    System.out.println("fmt from SPI=" + spiFmt);

    ByteArrayInputStream bais = new ByteArrayInputStream(simpData);
    ImageInputStream iis = new MemoryCacheImageInputStream(bais);
    simpReader.setInput(iis);
    BufferedImage bi = simpReader.read(0);
    System.out.println(bi);
    IIOMetadata metadata = simpReader.getImageMetadata(0);
    System.out.println("Image metadata="+metadata);
    IIOMetadataFormat format =
        metadata.getMetadataFormat("simp_metadata_1.0");
    System.out.println("Image metadata format="+format);
    if (format == null) {
        throw new RuntimeException("MetadataFormat not found.");
    }
}
 
Example 11
Source Project: pumpernickel   File: JPEGMetaDataDemo.java    License: MIT License 5 votes vote down vote up
@Override
protected int[] getCollectDataParamLimits() {
	if (readers == null) {
		readers = new ArrayList<>();
		Iterator<ImageReader> iterator = ImageIO
				.getImageReadersBySuffix("jpeg");
		while (iterator.hasNext()) {
			ImageReader reader = iterator.next();
			readers.add(reader);
		}
	}
	return new int[] { SAMPLE_COUNT, 1 + 2 * readers.size() };
}
 
Example 12
private static void testGetReaderFileSuffixes() {
    String[] suffixes = ImageIO.getReaderFileSuffixes();
    for (String s : suffixes) {
        Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(s);
        if (!it.hasNext()) {
            throw new RuntimeException("getReaderFileSuffixes returned " +
                                       "an unknown suffix: " + s);
        }
    }
}
 
Example 13
Source Project: Azzet   File: GifFormat.java    License: Open Software License 3.0 5 votes vote down vote up
@Override
public BufferedImage[] loadAsset( InputStream input, AssetInfo assetInfo ) throws Exception
{
	// Get the image reader...
	Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix( "gif" );

	// Check that there is one (this should almost never fail).
	if (!iter.hasNext())
	{
		throw new RuntimeException( "gif image reader not found" );
	}

	// Take the GIF reader
	ImageReader image = iter.next();

	// Set the input stream to which to read the GIF from.
	image.setInput( ImageIO.createImageInputStream( input ) );

	// Calculate the number of frames in the GIF image.
	int imgs = image.getNumImages( true );

	// Read each frame from the GIF image.
	BufferedImage[] frames = new BufferedImage[imgs];

	for (int i = 0; i < imgs; i++)
	{
		frames[i] = image.read( i );
	}

	return frames;
}