Search in sources :

Example 26 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project TranskribusCore by Transkribus.

the class ImgUtils method readImageDimensionsWithImageIO.

public static Dimension readImageDimensionsWithImageIO(File imgFile) throws FileNotFoundException, IOException {
    Dimension dim = null;
    logger.debug("Loading file with imageIO...");
    ImageInputStream iis = new FileImageInputStream(imgFile);
    final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
    while (readers.hasNext()) {
        ImageReader reader = readers.next();
        try {
            logger.debug("reader format = " + reader.getFormatName());
            reader.setInput(iis);
            final int xDim = reader.getWidth(0);
            final int yDim = reader.getHeight(0);
            logger.debug("Success with reader impl: " + reader.getClass().getCanonicalName());
            dim = new Dimension(xDim, yDim);
        } catch (Exception e) {
            logger.warn("Could not read image dimensions with reader: " + reader.getFormatName() + ": " + e.getMessage());
            logger.debug("Cause: ", e);
            logger.debug("Reader impl: " + reader.getClass().getCanonicalName());
        } finally {
            reader.dispose();
        }
        if (dim != null) {
            break;
        }
    }
    if (dim == null) {
        throw new IOException("Could not read image dimensions with ImageIO.");
    }
    return dim;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) Dimension(java.awt.Dimension) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) CorruptImageException(eu.transkribus.core.exceptions.CorruptImageException) FileNotFoundException(java.io.FileNotFoundException)

Example 27 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project TranskribusCore by Transkribus.

the class ImgUtils method readImage.

/**
 * Reads image in the specified image file. For multiimage tiff files, the first image is read.
 */
public static BufferedImage readImage(File imgFile) throws FileNotFoundException, IOException {
    ImageInputStream iis = new FileImageInputStream(imgFile);
    BufferedImage img;
    try {
        img = readImage(iis);
    } catch (IOException e) {
        throw new IOException("Could not read image: " + imgFile.getAbsolutePath(), e);
    }
    return img;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 28 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project WorldPainter by Captain-Chaos.

the class Dimension method getImageSize.

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;
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader)

Example 29 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project OpenOLAT by OpenOLAT.

the class ImageHelperImpl method scaleImage.

/**
 * @param image the image to scale
 * @param scaledImaged the new scaled image
 * @param maxSize the maximum size (height or width) of the new scaled image
 * @return
 */
@Override
public Size scaleImage(File image, String imageExt, VFSLeaf scaledImage, int maxWidth, int maxHeight) {
    ImageInputStream imageIns = null;
    OutputStream bos = new BufferedOutputStream(scaledImage.getOutputStream(false));
    try {
        imageIns = new FileImageInputStream(image);
        SizeAndBufferedImage scaledSize = calcScaledSize(imageIns, imageExt, maxWidth, maxHeight, false);
        if (scaledSize == null) {
            return null;
        }
        if (!scaledSize.getScaledSize().isChanged() && isSameFormat(image, scaledImage)) {
            InputStream cloneIns = new FileInputStream(image);
            IOUtils.copy(cloneIns, bos);
            IOUtils.closeQuietly(cloneIns);
            return scaledSize.getScaledSize();
        } else {
            BufferedImage imageSrc = scaledSize.getImage();
            if (imageSrc == null) {
                // unsupported image format
                return null;
            }
            BufferedImage scaledBufferedImage = scaleTo(imageSrc, scaledSize.getScaledSize());
            if (writeTo(scaledBufferedImage, bos, scaledSize.getScaledSize(), getImageFormat(scaledImage))) {
                return scaledSize.getScaledSize();
            }
            return null;
        }
    } catch (IOException e) {
        return null;
    } finally {
        closeQuietly(imageIns);
        FileUtils.closeSafely(bos);
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileInputStream(java.io.FileInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) BufferedImage(java.awt.image.BufferedImage)

Example 30 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project openmrs-core by openmrs.

the class ImageHandler method getObs.

/**
 * Currently supports all views and puts the Image file data into the ComplexData object
 *
 * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String)
 */
@Override
public Obs getObs(Obs obs, String view) {
    File file = getComplexDataFile(obs);
    // Raw image
    if (ComplexObsHandler.RAW_VIEW.equals(view)) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(file);
        } catch (IOException e) {
            log.error("Trying to read file: " + file.getAbsolutePath(), e);
        }
        ComplexData complexData = new ComplexData(file.getName(), img);
        String mimeType = null;
        // Image MIME type
        try {
            FileImageInputStream imgStream = new FileImageInputStream(file);
            Iterator<ImageReader> imgReader = ImageIO.getImageReaders(imgStream);
            imgStream.close();
            if (imgReader.hasNext()) {
                mimeType = "image/" + imgReader.next().getFormatName().toLowerCase();
            } else {
                log.warn("MIME type of " + file.getAbsolutePath() + " is not known");
            }
        } catch (FileNotFoundException e) {
            log.error("Image " + file.getAbsolutePath() + " was not found", e);
        } catch (IOException e) {
            log.error("Trying to determine MIME type of " + file.getAbsolutePath(), e);
        }
        // If the mimetype is still null, determine it via getFileMimeType()
        mimeType = mimeType != null ? mimeType : OpenmrsUtil.getFileMimeType(file);
        complexData.setMimeType(mimeType);
        obs.setComplexData(complexData);
    } else {
        // NOTE: if adding support for another view, don't forget to update supportedViews list above
        return null;
    }
    return obs;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ComplexData(org.openmrs.obs.ComplexData) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

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