Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* Copyright 2017 Patrick Favre-Bulle
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class ImageUtil {
public static BufferedImage scaleImgScalr(BufferedImage imageToScale, int dWidth, int dHeight, ImageType.ECompression compression, Color background, boolean antiAlias) {
BufferedImage scaledImage = null;
if (imageToScale != null) {
scaledImage = Scalr.resize(imageToScale, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_EXACT, dWidth, dHeight, null);
if (!compression.hasTransparency) {
BufferedImage convertedImg = new BufferedImage(scaledImage.getWidth(), scaledImage.getHeight(), BufferedImage.TYPE_INT_RGB);
convertedImg.getGraphics().drawImage(scaledImage, 0, 0, background, null);
scaledImage = convertedImg;
}
}
return scaledImage;
}
public static BufferedImage scaleThumbnailator(BufferedImage imageToScale, int dWidth, int dHeight, ImageType.ECompression compression, Color background, boolean antiAlias) {
BufferedImage scaledImage = null;
if (imageToScale != null) {
Resizer resizer = DefaultResizerFactory.getInstance()
.getResizer(new Dimension(imageToScale.getWidth(), imageToScale.getHeight()), new Dimension(dWidth, dHeight));
scaledImage = new FixedSizeThumbnailMaker(dWidth, dHeight, false, true)
.resizer(resizer)
.make(imageToScale);
if (!compression.hasTransparency) {
BufferedImage convertedImg = new BufferedImage(scaledImage.getWidth(), scaledImage.getHeight(), BufferedImage.TYPE_INT_RGB);
convertedImg.getGraphics().drawImage(scaledImage, 0, 0, background, null);
scaledImage = convertedImg;
}
return scaledImage;
}
public static BufferedImage scaleJava2d(BufferedImage imageToScale, int dWidth, int dHeight, ImageType.ECompression compression, Color background, boolean antiAlias) {
BufferedImage scaledImage = null;
if (imageToScale != null) {
int imageType = BufferedImage.TYPE_INT_RGB;
if (compression == ImageType.ECompression.PNG || compression == ImageType.ECompression.GIF || imageType == 0) {
imageType = BufferedImage.TYPE_INT_ARGB;
}
scaledImage = new BufferedImage(dWidth, dHeight, imageType);
Graphics2D graphics2D = scaledImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (compression == ImageType.ECompression.JPG) {
graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null);
} else {
graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null);
}
graphics2D.dispose();
}
return scaledImage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.