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);
}
}
Aggregations