Java Code Examples for javax.imageio.stream.FileImageInputStream

The following examples show how to use javax.imageio.stream.FileImageInputStream. 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 check out the related API usage on the sidebar.
Example 1
Source Project: java-bot-sdk   Source 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 2
private static void checkContainerInFile(File file, int nonOptionalIntValue, Integer autoOptionalIntValue)
        throws IOException
{
    final FileImageInputStream stream = new FileImageInputStream(file);

    if (autoOptionalIntValue == null)
    {
        assertEquals((CONTAINER_BIT_SIZE_WITHOUT_OPTIONAL + 7) / Byte.SIZE, stream.length());
        assertEquals(nonOptionalIntValue, (int) stream.readBits(32));
        assertEquals(0, stream.readBit());
    }
    else
    {
        assertEquals((CONTAINER_BIT_SIZE_WITH_OPTIONAL + 7) / Byte.SIZE, stream.length());
        assertEquals(nonOptionalIntValue, (int) stream.readBits(32));
        assertEquals(1, stream.readBit());
        assertEquals((int) autoOptionalIntValue, (int) stream.readBits(32));
    }

    stream.close();
}
 
Example 3
private static void checkContainerInFile(File file, BasicColor basicColor, short numBlackTones)
        throws IOException
{
    final FileImageInputStream stream = new FileImageInputStream(file);

    if (basicColor == BasicColor.WHITE)
    {
        assertEquals(1, stream.length());
        assertEquals(basicColor.getValue(), stream.readByte());
    }
    else
    {
        assertEquals(1 + 1 + 4 * numBlackTones, stream.length());
        assertEquals(basicColor.getValue(), stream.readByte());
        assertEquals(numBlackTones, stream.readByte());
        for (short i = 0; i < numBlackTones; ++i)
            assertEquals(i + 1, stream.readInt());
    }

    stream.close();
}
 
Example 4
Source Project: instagram4j   Source File: InstagramGenericUtil.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);
	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 5
public static void main(String[] args) {
    try {
        File f = new File("ImageInputStreamFromRAF.tmp");
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        ImageInputStream istream = ImageIO.createImageInputStream(raf);
        ImageOutputStream ostream = ImageIO.createImageOutputStream(raf);
        f.delete();
        if (istream == null) {
            throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!");
        }
        if (ostream == null) {
            throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!");
        }
        if (!(istream instanceof FileImageInputStream)) {
            throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!");
        }
        if (!(ostream instanceof FileImageOutputStream)) {
            throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!");
        }
    } catch (IOException ioe) {
        throw new RuntimeException("Unexpected IOException: " + ioe);
    }
}
 
Example 6
Source Project: j-webp   Source File: DecodeTest.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String args[]) throws IOException {
    String inputWebpPath = "test_pic/test.webp";
    String outputJpgPath = "test_pic/test_.jpg";
    String outputJpegPath = "test_pic/test_.jpeg";
    String outputPngPath = "test_pic/test_.png";

    // Obtain a WebP ImageReader instance
    ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();

    // Configure decoding parameters
    WebPReadParam readParam = new WebPReadParam();
    readParam.setBypassFiltering(true);

    // Configure the input on the ImageReader
    reader.setInput(new FileImageInputStream(new File(inputWebpPath)));

    // Decode the image
    BufferedImage image = reader.read(0, readParam);

    ImageIO.write(image, "png", new File(outputPngPath));
    ImageIO.write(image, "jpg", new File(outputJpgPath));
    ImageIO.write(image, "jpeg", new File(outputJpegPath));

}
 
Example 7
Source Project: density-converter   Source 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 8
Source Project: jdk8u_jdk   Source File: ImageStreamFromRAF.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    try {
        File f = new File("ImageInputStreamFromRAF.tmp");
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        ImageInputStream istream = ImageIO.createImageInputStream(raf);
        ImageOutputStream ostream = ImageIO.createImageOutputStream(raf);
        f.delete();
        if (istream == null) {
            throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!");
        }
        if (ostream == null) {
            throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!");
        }
        if (!(istream instanceof FileImageInputStream)) {
            throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!");
        }
        if (!(ostream instanceof FileImageOutputStream)) {
            throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!");
        }
    } catch (IOException ioe) {
        throw new RuntimeException("Unexpected IOException: " + ioe);
    }
}
 
Example 9
Source Project: skin-composer   Source 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 10
Source Project: dashencrypt   Source File: ManifestCreation.java    License: 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 11
Source Project: WorldPainter   Source File: Dimension.java    License: 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 12
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 13
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 14
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 15
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 16
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 17
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 18
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 19
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 20
Source Project: JDKSourceCode1.8   Source File: FileImageInputStreamSpi.java    License: MIT License 5 votes vote down vote up
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 21
Source Project: JDKSourceCode1.8   Source File: RAFImageInputStreamSpi.java    License: MIT License 5 votes vote down vote up
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 22
private void checkItemInFile(File file, Item item, long version) throws IOException
{
    final FileImageInputStream stream = new FileImageInputStream(file);
    assertEquals(item.getParam(), stream.readUnsignedShort());
    if (version >= HIGHER_VERSION)
        assertEquals(item.getExtraParam(), Long.valueOf(stream.readUnsignedInt()));
    stream.close();
}
 
Example 23
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 24
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 25
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 26
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 27
Source Project: Bytecoder   Source File: FileImageInputStreamSpi.java    License: Apache License 2.0 5 votes vote down vote up
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 28
Source Project: Bytecoder   Source File: RAFImageInputStreamSpi.java    License: Apache License 2.0 5 votes vote down vote up
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}
 
Example 29
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof File) {
        try {
            return new FileImageInputStream((File)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 30
public ImageInputStream createInputStreamInstance(Object input,
                                                  boolean useCache,
                                                  File cacheDir) {
    if (input instanceof RandomAccessFile) {
        try {
            return new FileImageInputStream((RandomAccessFile)input);
        } catch (Exception e) {
            return null;
        }
    } else {
        throw new IllegalArgumentException
            ("input not a RandomAccessFile!");
    }
}