private BufferedImage scaleImage(Image originalImage, int maxWidth, int maxHeight) { return scaleImage(originalImage, determineScaleSize(maxWidth, maxHeight, originalImage)); }
private Thumbnail createThumbnail(InputStream inputStream, File thumbnailFile, int maxWidth, int maxHeight, long thumbId, String fileName) throws IOException, ThumbnailRenderException { final BufferedImage thumbnailImage = scaleImage(maxWidth, maxHeight, inputStream); final int height = thumbnailImage.getHeight(); final int width = thumbnailImage.getWidth(); storeImage(thumbnailImage, thumbnailFile); return new Thumbnail(height, width, fileName, thumbId, mimeType); }
public BufferedImage scaleImage(int maxWidth, int maxHeight, InputStream imageStream) throws IOException { final ImageInputStream imageInputStream = ThumbnailUtil.getImageInputStream(imageStream); try { return scaleImage(maxWidth, maxHeight, imageInputStream); } finally { imageInputStream.close(); } }
private Thumbnail createThumbnail(InputStream originalFile, File thumbnailFile, int maxWidth, int maxHeight, long thumbId, String fileName) throws IOException, FileNotFoundException { // Load original image. Image originalImage = getImage(originalFile); // Create scaled buffered image from original image. BufferedImage scaledImage = scaleImage(originalImage, maxWidth, maxHeight); int height = scaledImage.getHeight(); int width = scaledImage.getWidth(); storeImage(scaledImage, thumbnailFile); return new Thumbnail(height, width, fileName, thumbId); }
public UploadedAvatar scaleImageToTempFile(final Image sourceImage, final File targetFile, final int edgeSize) throws IOException { ThumbnailDimension targetDimension = thumber.determineScaleSize(edgeSize, edgeSize, sourceImage.getWidth(null), sourceImage.getHeight(null)); BufferedImage scaledImage = thumber.scaleImage(sourceImage, targetDimension); ImageIO.write(scaledImage, AVATAR_IMAGE_FORMAT, targetFile); return new UploadedAvatar(targetFile, AVATAR_IMAGE_FORMAT_FULL.getContentType(), targetDimension.getWidth(), targetDimension.getHeight()); } }
private ResultAndContentTypeChanged cropImageToFile(final int edgeSize, final Image sourceImage) { Result<File> prescaledFile = createTemporaryFile(TEMP_FILE_EXTENSION); if (!prescaledFile.isValid()) { return new ResultAndContentTypeChanged(prescaledFile, false); } ThumbnailDimension dimensions = thumber.determineScaleSize(edgeSize, edgeSize, sourceImage.getWidth(null), sourceImage.getHeight(null)); BufferedImage scaledImage = thumber.scaleImage(sourceImage, dimensions); try { ImageIO.write(scaledImage, AVATAR_IMAGE_FORMAT, prescaledFile.getResult()); return new ResultAndContentTypeChanged(prescaledFile, true); } catch (IOException e) { return new ResultAndContentTypeChanged(new Result<File>().addError(getText("avatarpicker.upload.temp.io", e.getMessage()), ErrorCollection.Reason.SERVER_ERROR), false); } }
private BufferedImage scaleImage(int maxWidth, int maxHeight, ImageInputStream imageInputStream) throws IOException { final Optional<ImageReader> imageReader = ThumbnailUtil.getFirstImageReader(imageInputStream); if (!imageReader.isPresent()) { throw new IOException("Cannot read the image"); } final ImageReader reader = imageReader.get(); try { reader.setInput(imageInputStream); final ThumbnailDimension originalImageDimensions = new ThumbnailDimension(reader.getWidth(0), reader.getHeight(0)); final ThumbnailDimension scaledImageDimension = ThumbnailUtil.determineScaledDimensions(maxWidth, maxHeight, originalImageDimensions.getWidth(), originalImageDimensions.getHeight()); final int ratio = ThumbnailUtil.computeSubsamplingRatio(originalImageDimensions, scaledImageDimension, samplingFactor); final ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(ratio, ratio, 0, 0); final BufferedImage scaledImage = reader.read(0, param); //Subsampling does not return an exact size of an image - we need to resize it return scaleImage(scaledImage, scaledImageDimension); } finally { reader.dispose(); } }