Search in sources :

Example 36 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project openolat by klemens.

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)

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