List of usage examples for java.awt Graphics2D setRenderingHint
public abstract void setRenderingHint(Key hintKey, Object hintValue);
From source file:se.dibbler.backend.generics.DibblerImageUtil.java
private static Response<BufferedImage> resizeWithHint(BufferedImage originalImage, int type, int height, int width) { try {/* w w w. j ava 2 s . co m*/ BufferedImage resizedImage = new BufferedImage(width, height, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, height, null); g.dispose(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return Response.success(resizedImage); } catch (Exception e) { LOG.error("[ ERROR when resizing file ] [ MESSAGE : {}]", e.getMessage()); return Response.error(GenericError.FILE_HANDLING); } }
From source file:org.jamwiki.parser.image.ImageProcessor.java
/** * *//* w w w . j ava 2 s . c o m*/ private static BufferedImage resizeImage(BufferedImage tmp, int targetWidth, int targetHeight) throws IOException { int type = (tmp.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; int width = tmp.getWidth(); int height = tmp.getHeight(); BufferedImage resized = tmp; do { width /= 2; if (width < targetWidth) { width = targetWidth; } height /= 2; if (height < targetHeight) { height = targetHeight; } tmp = new BufferedImage(width, height, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(resized, 0, 0, width, height, null); g2.dispose(); resized = tmp; } while (width != targetWidth || height != targetHeight); return resized; }
From source file:imageLines.ImageHelpers.java
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality) { int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (BufferedImage) img; int w, h;//from w ww . j a va 2 s . co m if (higherQuality) { // Use multi-step technique: start with original size, then // scale down in multiple passes with drawImage() // until the target size is reached w = img.getWidth(); h = img.getHeight(); } else { // Use one-step technique: scale directly from original // size to target size with a single drawImage() call w = targetWidth; h = targetHeight; } do { if (higherQuality && w > targetWidth) { w /= 2; if (w < targetWidth) { w = targetWidth; } } if (higherQuality && h > targetHeight) { h /= 2; if (h < targetHeight) { h = targetHeight; } } BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } while (w != targetWidth || h != targetHeight); return ret; }
From source file:br.com.diegosilva.jsfcomponents.util.Utils.java
public static byte[] resizeImage(byte[] imageData, String contentType, int width) { ImageIcon icon = new ImageIcon(imageData); double ratio = (double) width / icon.getIconWidth(); int resizedHeight = (int) (icon.getIconHeight() * ratio); int imageType = "image/png".equals(contentType) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; BufferedImage bImg = new BufferedImage(width, resizedHeight, imageType); Graphics2D g2d = bImg.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.drawImage(icon.getImage(), 0, 0, width, resizedHeight, null); g2d.dispose();/*w w w . j a v a 2 s. c o m*/ String formatName = ""; if ("image/png".equals(contentType)) formatName = "png"; else if ("image/jpeg".equals(contentType) || "image/jpg".equals(contentType)) formatName = "jpeg"; ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { ImageIO.write(bImg, formatName, baos); return baos.toByteArray(); } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:be.fedict.eid.applet.maven.DocbookMojo.java
private static void graphToFile(BasicVisualizationServer<String, String> visualization, File file) throws IOException { Dimension size = visualization.getSize(); int width = (int) (size.getWidth() + 1); int height = (int) (size.getHeight() + 1); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = bufferedImage.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, 900, 650);/* ww w .j a v a 2 s. co m*/ visualization.setBounds(0, 0, 900, 650); visualization.paint(graphics); graphics.dispose(); ImageIO.write(bufferedImage, "png", file); }
From source file:it.reexon.lib.files.FileUtils.java
/** * Convenience method that returns a scaled instance of the provided {@code BufferedImage}. * /*from ww w .j a va2 s . c o m*/ * @param img the original image to be scaled * @param targetWidth the desired width of the scaled instance, in pixels * @param targetHeight the desired height of the scaled instance, in pixels * @param hint one of the rendering hints that corresponds to {@code RenderingHints.KEY_INTERPOLATION} (e.g. * {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR}, {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR}, * {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC}) * @param higherQuality if true, this method will use a multi-step scaling technique that provides higher quality than the * usual one-step technique (only useful in downscaling cases, where {@code targetWidth} or {@code targetHeight} is * smaller than the original dimensions, and generally only when the {@code BILINEAR} hint is specified) * @return a scaled version of the original {@code BufferedImage} */ public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality) { final int type = img.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = img; int w; int h; if (higherQuality) { // Use multi-step technique: start with original size, then // scale down in multiple passes with drawImage() // until the target size is reached w = img.getWidth(); h = img.getHeight(); } else { // Use one-step technique: scale directly from original // size to target size with a single drawImage() call w = targetWidth; h = targetHeight; } do { if (higherQuality && w > targetWidth) { w /= 2; if (w < targetWidth) { w = targetWidth; } } if (higherQuality && h > targetHeight) { h /= 2; if (h < targetHeight) { h = targetHeight; } } final BufferedImage tmp = new BufferedImage(w, h, type); final Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } while (w != targetWidth || h != targetHeight); return ret; }
From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java
public static void updateImage(JLabel imageContainer, File imageFile, Format format) { if (imageFile == null || !imageFile.exists()) { return;//from w w w .j ava 2 s. c o m } BufferedImage img = null; try { img = ImageIO.read(imageFile); } catch (IOException e) { e.printStackTrace(); } if (img == null) { return; } int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); int imageViewWidth = (int) imageContainer.getPreferredSize().getWidth(); int imageViewHeight = (int) imageContainer.getPreferredSize().getHeight(); double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), new Dimension(imageViewWidth, imageViewHeight)); imageWidth = (int) (factor * imageWidth); imageHeight = (int) (factor * imageHeight); if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) { return; } BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); int x = (imageViewWidth - imageWidth) / 2; int y = (imageViewHeight - imageHeight) / 2; if (format == Format.PNG || format == Format.XML) { g2.drawImage(img, x, y, imageWidth, imageHeight, null); } else { g2.drawImage(img, x, y, imageWidth, imageHeight, Color.WHITE, null); } g2.dispose(); imageContainer.setIcon(new ImageIcon(tmp)); }
From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java
/** * ?/*from w w w . j a v a2 s . c o m*/ * * @param source * @param targetW * @param targetH * @param ifScaling ? * @return */ public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) { // targetWtargetH int type = source.getType(); BufferedImage target = null; double sx = (double) targetW / source.getWidth(); double sy = (double) targetH / source.getHeight(); // targetWtargetH??,?if else??? if (ifScaling) { if (sx > sy) { sx = sy; targetW = (int) (sx * source.getWidth()); } else { sy = sx; targetH = (int) (sy * source.getHeight()); } } if (type == BufferedImage.TYPE_CUSTOM) { // handmade ColorModel cm = source.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); target = new BufferedImage(cm, raster, alphaPremultiplied, null); } else target = new BufferedImage(targetW, targetH, type); Graphics2D g = target.createGraphics(); // smoother than exlax: g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); return target; }
From source file:org.pentaho.reporting.engine.classic.core.layout.output.RenderUtility.java
public static Image scaleImage(final Image img, final int targetWidth, final int targetHeight, final Object hintValue, final boolean higherQuality) { final int type = BufferedImage.TYPE_INT_ARGB; Image ret = img;/*from ww w .j ava 2 s. co m*/ int w; int h; do { if (higherQuality) { final int imageWidth = ret.getWidth(null); final int imageHeight = ret.getHeight(null); if (imageWidth < targetWidth) { // This is a up-scale operation. w = Math.min(imageWidth << 1, targetWidth); } else if (imageWidth > targetWidth) { // downscale w = Math.max(imageWidth >> 1, targetWidth); } else { w = imageWidth; } if (imageHeight < targetHeight) { // This is a up-scale operation. h = Math.min(imageHeight << 1, targetHeight); } else if (imageHeight > targetHeight) { // downscale h = Math.max(imageHeight >> 1, targetHeight); } else { h = imageHeight; } } else { w = targetWidth; h = targetHeight; } final BufferedImage tmp = new BufferedImage(w, h, type); final Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hintValue); // this one scales the image .. if (ret instanceof BufferedImage) { if (g2.drawImage(ret, 0, 0, w, h, null) == false) { logger.debug("Failed to scale the image. This should not happen."); } } else { final WaitingImageObserver obs = new WaitingImageObserver(ret); while (g2.drawImage(ret, 0, 0, w, h, null) == false) { obs.waitImageLoaded(); if (obs.isError()) { logger.warn("Error while loading the image during the rendering."); break; } } } g2.dispose(); ret = tmp; } while (w != targetWidth || h != targetHeight); return ret; }
From source file:com.fluidops.iwb.deepzoom.DZConvert.java
/** * Returns resized image//from w ww . j av a 2s .c o m * NB - useful reference on high quality image resizing can be found here: * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * @param width the required width * @param height the frequired height * @param img the image to be resized */ private static BufferedImage resizeImage(BufferedImage img, double width, double height) { int w = (int) width; int h = (int) height; BufferedImage result = new BufferedImage(w, h, getType(img)); Graphics2D g = result.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.drawImage(img, 0, 0, w, h, 0, 0, img.getWidth(), img.getHeight(), null); return result; }