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

The following examples show how to use javax.imageio.ImageIO#getImageReadersBySuffix() . 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 check out the related API usage on the sidebar.
Example 1
Source File: FileImageSink.java    From youkefu with 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 File: ImageUtils.java    From java-bot-sdk with 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
Source File: InstagramGenericUtil.java    From instagram4j with 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);
	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 File: ImageUtil.java    From density-converter with 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 File: Utils.java    From skin-composer with 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
Source File: ManifestCreation.java    From dashencrypt with Mozilla Public License 2.0 6 votes vote down vote up
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
Source File: AWTLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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
Source File: Dimension.java    From WorldPainter with GNU General Public License v3.0 6 votes vote down vote up
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
Source File: GetReaderWriterInfo.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TestSIMPPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 File: JPEGMetaDataDemo.java    From pumpernickel with 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
Source File: GetReaderWriterInfo.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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 File: GifFormat.java    From Azzet with 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;
}