Search in sources :

Example 16 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project jdk8u_jdk by JetBrains.

the class JPEGsNotAcceleratedTest method readTestImage.

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;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReadParam(javax.imageio.ImageReadParam) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 17 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project jdk8u_jdk by JetBrains.

the class ImageStreamFromRAF method main.

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);
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) RandomAccessFile(java.io.RandomAccessFile) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 18 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project Lucee by lucee.

the class Image method getMetaData.

public IIOMetadata getMetaData(Struct parent) {
    InputStream is = null;
    javax.imageio.stream.ImageInputStreamImpl iis = null;
    try {
        if (source instanceof File) {
            iis = new FileImageInputStream((File) source);
        } else if (source == null)
            iis = new MemoryCacheImageInputStream(new ByteArrayInputStream(getImageBytes(format, true)));
        else
            iis = new MemoryCacheImageInputStream(is = source.getInputStream());
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
        if (readers.hasNext()) {
            // pick the first available ImageReader
            ImageReader reader = readers.next();
            IIOMetadata meta = null;
            synchronized (sync) {
                // attach source to the reader
                reader.setInput(iis, true);
                // read metadata of first image
                meta = reader.getImageMetadata(0);
                meta.setFromTree(FORMAT, meta.getAsTree(FORMAT));
                reader.reset();
            }
            // generating dump
            if (parent != null) {
                String[] formatNames = meta.getMetadataFormatNames();
                for (int i = 0; i < formatNames.length; i++) {
                    Node root = meta.getAsTree(formatNames[i]);
                    // print.out(XMLCaster.toString(root));
                    addMetaddata(parent, "metadata", root);
                }
            }
            return meta;
        }
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
    } finally {
        ImageUtil.closeEL(iis);
        IOUtil.closeEL(is);
    }
    return null;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) AttributedString(java.text.AttributedString) Point(java.awt.Point) FileImageInputStream(javax.imageio.stream.FileImageInputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReader(javax.imageio.ImageReader) File(java.io.File)

Example 19 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project imageio-ext by geosolutions-it.

the class JPEGWriterCompareTest method writeAsJpeg.

@Test
public void writeAsJpeg() throws Exception {
    if (!TestData.isExtensiveTest()) {
        LOGGER.info("Skipping compare tests. Use Extensive tests to enable it");
        return;
    }
    if (SKIP_TESTS) {
        LOGGER.warning(ERROR_LIB_MESSAGE);
        assumeTrue(!SKIP_TESTS);
        return;
    }
    ImageReaderSpi spiReader = new PNGImageReaderSpi();
    ImageReader reader = null;
    File inputFile = TestData.file(this, "testmergb.png");
    FileImageInputStream fis = null;
    try {
        reader = spiReader.createReaderInstance();
        fis = new FileImageInputStream(inputFile);
        reader.setInput(fis);
        BufferedImage buffered = reader.read(0);
        writeAsJpeg(buffered);
    } catch (Throwable th) {
        if (fis != null) {
            try {
                fis.close();
            } catch (Throwable t) {
            }
        }
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
            }
        }
        th.printStackTrace();
    }
// System.in.read();
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) PNGImageReaderSpi(com.sun.imageio.plugins.png.PNGImageReaderSpi) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) JPEGImageReaderSpi(com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi) PNGImageReaderSpi(com.sun.imageio.plugins.png.PNGImageReaderSpi) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 20 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project imageio-ext by geosolutions-it.

the class TIFFMetadataTest method testImageRead.

/**
 * This test class is used for testing the capability of the TIFFImageReader
 * class to read EXIF metadata. TIFFImageReader class presented a bug when
 * the EXIF IFD pointer tag type was not LONG (type = 4) but was the new
 * IFD_POINTER type (type = 13) defined in the Technical Note 1 of the TIFF
 * Specification Supplement documentation. This variation provoked a
 * ClassCastException when the reader tried to cast the EXIF IFD pointer
 * data to the TIFFIFD class. This bug has been fixed by adding the
 * possibility to contain the IFD_POINTER type to the EXIF IFD pointer tag.
 * The testImageRead() method reads an image with the new TIFF tag type and
 * checks if the EXIF tags has been read by the TIFFImageReader.
 *
 * @throws IOException
 */
@Test
public void testImageRead() throws IOException {
    // Selection of the input file from the TestData directory
    File inputFile = TestData.file(this, "test_IFD.tif");
    // Instantiation of the read-params
    final TIFFImageReadParam param = new TIFFImageReadParam();
    // Instantiation of the file-reader
    TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    // Creation of the file input stream associated to the selected file
    FileImageInputStream stream0 = new FileImageInputStream(inputFile);
    try {
        // Setting the inputstream to the reader
        reader.setInput(stream0);
        // Reading of the image
        RenderedImage img = reader.read(0, param);
        // Reading of the associated metadata
        TIFFImageMetadata metadata = (TIFFImageMetadata) reader.getImageMetadata(0);
        // Check if the Exif pointer metadata is present
        int tagPointer = 34665;
        boolean fieldPointer = metadata.getRootIFD().containsTIFFField(tagPointer);
        assertTrue(fieldPointer);
        // Selection of the subIFD associated to the exif pointer
        TIFFIFD subIFD = (TIFFIFD) metadata.getTIFFField(tagPointer).getData();
        // Selection of the tags associated to the EXIF pointer
        int tagNumberExifVersion = 36864;
        int tagNumberDateTime = 36868;
        int tagNumberCompConfig = 37121;
        int tagNumberFlashPix = 40960;
        int tagNumberColor = 40961;
        int tagNumberXpixelRes = 40962;
        int tagNumberYpixelRes = 40963;
        // Test Assertions
        assertTrue(subIFD.containsTIFFField(tagNumberExifVersion));
        assertTrue(subIFD.containsTIFFField(tagNumberDateTime));
        assertTrue(subIFD.containsTIFFField(tagNumberCompConfig));
        assertTrue(subIFD.containsTIFFField(tagNumberFlashPix));
        assertTrue(subIFD.containsTIFFField(tagNumberColor));
        assertTrue(subIFD.containsTIFFField(tagNumberXpixelRes));
        assertTrue(subIFD.containsTIFFField(tagNumberYpixelRes));
    } catch (Exception e) {
        // If an exception occurred the logger catch the exception and print
        // the message
        logger.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        // and the input stream are closed
        if (stream0 != null) {
            stream0.flush();
            stream0.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) TIFFImageMetadata(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageMetadata) TIFFIFD(it.geosolutions.imageioimpl.plugins.tiff.TIFFIFD) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) IOException(java.io.IOException) TIFFImageReadParam(it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam) Test(org.junit.Test)

Aggregations

FileImageInputStream (javax.imageio.stream.FileImageInputStream)36 File (java.io.File)22 BufferedImage (java.awt.image.BufferedImage)19 IOException (java.io.IOException)16 ImageInputStream (javax.imageio.stream.ImageInputStream)15 ImageReader (javax.imageio.ImageReader)14 Test (org.junit.Test)14 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)10 TIFFImageReaderSpi (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi)9 TIFFImageReader (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader)7 RenderedImage (java.awt.image.RenderedImage)7 ImageReadParam (javax.imageio.ImageReadParam)7 Rectangle (java.awt.Rectangle)6 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)4 CMMException (java.awt.color.CMMException)3 InputStream (java.io.InputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 IIOImage (javax.imageio.IIOImage)3 ImageOutputStream (javax.imageio.stream.ImageOutputStream)3 JPEGImageReaderSpi (com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi)2