Example usage for javax.imageio ImageReader setInput

List of usage examples for javax.imageio ImageReader setInput

Introduction

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

Prototype

public void setInput(Object input) 

Source Link

Document

Sets the input source to use to the given ImageInputStream or other Object .

Usage

From source file:it.tidalwave.imageio.example.stats.FocalLengthStats.java

public static void main(final String[] args) {
    try {/* ww  w.j a  v  a2 s  .  c o  m*/
        final PrintWriter out = new PrintWriter(new File(args[1]));
        new DirectoryWalker() {
            @Override
            protected void handleFile(final File file, final int depth, final Collection results)
                    throws IOException {
                if (file.getName().toUpperCase().endsWith(".NEF")) {
                    System.out.printf("Processing %s...\n", file.getCanonicalPath());
                    final ImageReader reader = (ImageReader) ImageIO.getImageReaders(file).next();
                    reader.setInput(ImageIO.createImageInputStream(file));
                    final IIOMetadata metadata = reader.getImageMetadata(0);
                    final NEFMetadata nefMetadata = (NEFMetadata) metadata;
                    final IFD exifIFD = nefMetadata.getExifIFD();
                    final TagRational focalLength = exifIFD.getFocalLength();
                    out.println(focalLength.doubleValue());
                }
            }

            public void start() throws IOException {
                super.walk(new File(args[0]), new ArrayList<Object>());
            }
        }.start();

        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static Dimension getImageDimensions(InputStream is) throws IOException {
    ImageInputStream in = ImageIO.createImageInputStream(is);
    try {/*from  w  w  w  .  j  av  a2s  .  co m*/
        final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                return new Dimension(reader.getWidth(0), reader.getHeight(0));
            } finally {
                reader.dispose();
            }
        }
        throw new IOException("Failed to read Image file");
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:se.trixon.almond.GraphicsHelper.java

public static Dimension getImgageDimension(File imageFile) throws IOException {
    ImageInputStream inputStream = ImageIO.createImageInputStream(imageFile);

    try {/*from  w  ww.  j  a va 2s  .co  m*/
        final Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(inputStream);
        if (imageReaders.hasNext()) {
            ImageReader imageReader = imageReaders.next();
            try {
                imageReader.setInput(inputStream);
                return new Dimension(imageReader.getWidth(0), imageReader.getHeight(0));
            } finally {
                imageReader.dispose();
            }
        }
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }

    return null;
}

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

/**
 * Tries to find an <code>ImageReader</code> that is capable of reading
 * the given image data. The returned image reader will be initialized
 * by passing an ImageInputStream that is created from the given data
 * to its <code>setInput</code> method. The caller is responsible for 
 * disposing the returned image reader./* w  w  w .  j  a  v  a  2 s . c o m*/
 *  
 * @param imageData The image data
 * @return The image reader
 * @throws IOException If no matching image reader can be found
 */
@SuppressWarnings("resource")
private static ImageReader findImageReader(ByteBuffer imageData) throws IOException {
    InputStream inputStream = Buffers.createByteBufferInputStream(imageData.slice());
    ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
    Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        imageReader.setInput(imageInputStream);
        return imageReader;
    }
    throw new IOException("Could not find ImageReader for image data");
}

From source file:nl.b3p.viewer.print.PrintInfo.java

/**
 * Load image dimensions, ideally without decoding the entire image.
 *///from w  w  w . j a  v  a  2 s.  c o  m
private static Dimension getImageDimension(InputStream image) throws IOException {
    // http://stackoverflow.com/questions/1559253/java-imageio-getting-image-dimension-without-reading-the-entire-file

    ImageInputStream in = ImageIO.createImageInputStream(image);
    try {
        final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                return new Dimension(reader.getWidth(0), reader.getHeight(0));
            } finally {
                reader.dispose();
            }
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    return null;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static Dimension getDimensions(File imageFile) throws IOException {
    FileImageInputStream fiis = null;
    try {/*www  .  j a  v  a  2s.  co m*/
        fiis = new FileImageInputStream(imageFile);
        ImageReader imageReader = ImageIO
                .getImageReadersBySuffix(FilenameUtils.getExtension(imageFile.getName())).next();
        imageReader.setInput(fiis);
        return new Dimension(imageReader.getWidth(0), imageReader.getHeight(0));
    } finally {
        IOUtils.close(fiis);
    }
}

From source file:FileUtils.java

/**
 * Utility method to read image from disk and transform image to BufferedImage object
 * @param data - relative path to the image
 * @param format - file prefix of the image
 * @return BufferedImage representation of the image
 *
 *///  w  w  w .j a  v a2 s . c  o m
public static BufferedImage bitmapToImage(String data, String format) throws IOException {
    final InputStream inb = new FileInputStream(data);
    final ImageReader rdr = ImageIO.getImageReadersByFormatName(format).next();
    final ImageInputStream imageInput = ImageIO.createImageInputStream(inb);
    rdr.setInput(imageInput);
    final BufferedImage image = rdr.read(0);
    inb.close();
    return image;
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Read a JPG image with CMYK ICC profile
 * @param imageByteArray/*from  w w w .j a va 2  s.c  om*/
 * @return The BufferedImage obj
 * @throws IOException
 * @throws ImageReadException
 */
public static BufferedImage readCMYK_JPG(byte[] imageByteArray) throws IOException, ImageReadException {

    colorType = COLOR_TYPE_RGB;
    hasAdobeMarker = false;

    InputStream in = new ByteArrayInputStream(imageByteArray);
    ImageInputStream stream = ImageIO.createImageInputStream(in);
    Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

    ImageReader reader = iter.next();
    reader.setInput(stream);

    BufferedImage image = null;
    ICC_Profile profile = null;

    colorType = COLOR_TYPE_CMYK;
    checkAdobeMarker(imageByteArray);
    profile = Imaging.getICCProfile(imageByteArray);

    WritableRaster raster = (WritableRaster) reader.readRaster(0, null);

    if (colorType == COLOR_TYPE_YCCK) {
        convertYcckToCmyk(raster);
    }
    if (hasAdobeMarker) {
        //convertInvertedColors(raster);
    }
    image = convertCmykToRgb(raster, profile);

    in.close();
    reader.dispose();
    return image;
}

From source file:it.reexon.lib.files.FileUtils.java

/**
 * Utility method to read image from disk and transform image to BufferedImage object 
 *  /*from w w w .  j  a  v  a  2  s .c om*/
 * @param data - relative path to the image 
 * @param format - file prefix of the image 
 * @return BufferedImage representation of the image 
 *  
 */
public static BufferedImage bitmapToImage(InputStream is, String format) throws IOException {
    final ImageReader rdr = ImageIO.getImageReadersByFormatName(format).next();
    final ImageInputStream imageInput = ImageIO.createImageInputStream(is);
    rdr.setInput(imageInput);
    final BufferedImage image = rdr.read(0);
    is.close();
    return image;
}

From source file:org.onehippo.forge.gallerymagick.core.command.ScalrProcessorUtils.java

/**
 * Creates and returns an {@link ImageReader} instance from the {@code sourceFile}.
 * @param sourceFile source file/*  w  w w  .ja v a  2 s.  c o  m*/
 * @return an {@link ImageReader} instance
 * @throws IOException if IOException occurs
 */
private static ImageReader getImageReader(File sourceFile) throws IOException {
    ImageReader reader = null;
    String extension = FilenameUtils.getExtension(sourceFile.getName());
    Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(extension);

    if (it.hasNext()) {
        reader = it.next();
        ImageInputStream input = new FileImageInputStream(sourceFile);
        reader.setInput(input);
    }

    return reader;
}