javax.imageio.stream.FileImageInputStream Java Examples

The following examples show how to use javax.imageio.stream.FileImageInputStream. 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: OptionalExpressionTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #2
Source File: DecodeTest.java    From j-webp with 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 #3
Source File: ImageStreamFromRAF.java    From openjdk-jdk9 with 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 #4
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 #5
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 #6
Source File: ImageStreamFromRAF.java    From jdk8u_jdk with 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 #7
Source File: AutoOptionalTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: ExifReader.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
@Execute
public void readExif() throws IOException {
    ImageReader reader = ExifUtil.findReader();
    reader.setInput(new FileImageInputStream(new File(file)));
    IIOMetadata imageMetadata = reader.getImageMetadata(0);

    parseExifMeta(imageMetadata);

}
 
Example #13
Source File: FileImageInputStreamSpi.java    From hottub with GNU General Public License v2.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 #14
Source File: RAFImageInputStreamSpi.java    From hottub with GNU General Public License v2.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 #15
Source File: IOModule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public BufferedImage load(FileObject file) throws IOException, URISyntaxException {
    if (file.getExt().equalsIgnoreCase("tga")) {
        ImageInputStream in = new FileImageInputStream(new File(file.getURL().toURI()));
        TGAImageReaderSpi spi = new TGAImageReaderSpi();
        TGAImageReader rea = new TGAImageReader(spi);
        rea.setInput(in);
        return rea.read(0);
    } else {
        BufferedImage image = ImageIO.read(file.getInputStream());
        return image;
    }
}
 
Example #16
Source File: FileImageInputStreamSpi.java    From openjdk-8-source with GNU General Public License v2.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 #17
Source File: RAFImageInputStreamSpi.java    From openjdk-8-source with GNU General Public License v2.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 #18
Source File: FileImageInputStreamSpi.java    From openjdk-8 with GNU General Public License v2.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 #19
Source File: RAFImageInputStreamSpi.java    From openjdk-8 with GNU General Public License v2.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 #20
Source File: FileImageInputStreamSpi.java    From jdk8u-jdk with GNU General Public License v2.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 #21
Source File: FileImageInputStreamSpi.java    From jdk8u_jdk with GNU General Public License v2.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 #22
Source File: RAFImageInputStreamSpi.java    From jdk8u_jdk with GNU General Public License v2.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 #23
Source File: RAFImageInputStreamSpi.java    From jdk8u-dev-jdk with GNU General Public License v2.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 #24
Source File: CreateAvdVisualPanel3.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private void sdcardSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sdcardSelectActionPerformed
    // TODO add your handling code here:
    FileChooserBuilder builder = new FileChooserBuilder(CreateAvdVisualPanel3.class);
    builder.setFilesOnly(true);
    File sdcardImage = builder.showOpenDialog();
    if (sdcardImage != null && sdcardImage.exists()) {
        try (FileImageInputStream fi = new FileImageInputStream(sdcardImage)) {
            byte[] boot = new byte[3];
            fi.read(boot);
            if (boot[0] == ((byte) 0xeb) && boot[1] == ((byte) 0x5a) && boot[2] == ((byte) 0x90)) {
                sdcardPath.setText(sdcardImage.getAbsolutePath());
            } else {
                NotifyDescriptor nd = new NotifyDescriptor.Confirmation("<html>"
                        + "Signature of selected file does not match Android SD Card image.<br/>"
                        + "Are you sure you want to use the selected file?", "SD Card image problem...",
                        NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
                Object notify = DialogDisplayer.getDefault().notify(nd);
                if (NotifyDescriptor.YES_OPTION.equals(notify)) {
                    sdcardPath.setText(sdcardImage.getAbsolutePath());
                }
            }

        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }

    }
}
 
Example #25
Source File: FileImageInputStreamSpi.java    From jdk8u-dev-jdk with GNU General Public License v2.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 #26
Source File: JPEGsNotAcceleratedTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static BufferedImage readTestImage(String fileName,
                               BufferedImage dest,
                               Rectangle srcROI)
{
    BufferedImage bi = null;

    try {
        FileImageInputStream is =
            new FileImageInputStream(new File(fileName));
        ImageReader reader =
            (ImageReader)ImageIO.getImageReaders(is).next();
        ImageReadParam param = reader.getDefaultReadParam();
        if (dest != null) {
            param.setDestination(dest);
        }
        if (srcROI != null) {
            param.setSourceRegion(srcROI);
        }
        reader.setInput(is);
        bi = reader.read(0, param);
    } catch (IOException e) {
        System.err.println("Error " + e +
                           " when reading file: " + fileName);
        throw new RuntimeException(e);
    }

    return bi;
}
 
Example #27
Source File: RAFImageInputStreamSpi.java    From jdk8u-jdk with GNU General Public License v2.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 #28
Source File: SimpleParamTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #29
Source File: RAFImageInputStreamSpi.java    From jdk1.8-source-analysis with 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 #30
Source File: FileImageInputStreamSpi.java    From dragonwell8_jdk with GNU General Public License v2.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();
    }
}