List of usage examples for java.awt Graphics2D drawImage
public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer);
From source file:com.taunova.app.libview.components.ImageHelpers.java
public static Image getScaledImage(BufferedImage image, int width) { Dimension d = getScaledDimension(image, width); Image scaledImage = image.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH); BufferedImage resizedImage = null; final boolean OPTIMIZE_SCALE = true; if (OPTIMIZE_SCALE) { ResampleOp resampleOp = new ResampleOp(100, 200); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); resizedImage = resampleOp.filter(image, null); } else {//from ww w.ja v a 2 s. com resizedImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB); Graphics2D g = resizedImage.createGraphics(); 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); g.drawImage(scaledImage, 0, 0, d.width, d.height, null); g.dispose(); } return resizedImage; }
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 ww . j av a 2 s . co 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:de.mpg.imeji.logic.storage.util.ImageUtils.java
/** * Convenience method that returns a scaled instance of the provided {@link BufferedImage}. * //w ww . ja va 2s . co 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 {@link BufferedImage} */ 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 = img; int w, 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; } } 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:com.cmart.PageControllers.SellItemImagesController.java
private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int width, int height) { BufferedImage resizedImage = new BufferedImage(width, width, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, width, null); g.dispose();// w ww .ja v a2 s .c om 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 resizedImage; }
From source file:eu.planets_project.tb.impl.data.util.ImageThumbnail.java
/** * Create a scaled jpeg of an image. The width/height ratio is preserved. * //w w w . j av a2 s .c om * <p> * If image is smaller than thumbWidth x thumbHeight, it will be magnified, * otherwise it will be scaled down. * </p> * * @param image * the image to reduce * @param thumbWidth * the maximum width of the thumbnail * @param thumbHeight * the maximum heigth of the thumbnail * @param quality * the jpeg quality ot the thumbnail * @param out * a stream where the thumbnail data is written to */ public static void createThumb(Image image, int thumbWidth, int thumbHeight, OutputStream out) throws Exception { int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); double thumbRatio = (double) thumbWidth / (double) thumbHeight; double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } // draw original image to thumbnail image object and // scale it to the new size on-the-fly BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = thumbImage.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_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); // save thumbnail image to out stream log.debug("Start writing rescaled image..."); ImageIO.write(thumbImage, "png", out); log.debug("DONE writing rescaled image..."); }
From source file:net.rptools.lib.image.ImageUtil.java
/** * Create a copy of the image that is compatible with the current graphics context and scaled to the supplied size *//* w ww .j a v a2 s .c o m*/ public static BufferedImage createCompatibleImage(Image img, int width, int height, Map<String, Object> hints) { width = Math.max(width, 1); height = Math.max(height, 1); int transparency; if (hints != null && hints.containsKey(HINT_TRANSPARENCY)) { transparency = (Integer) hints.get(HINT_TRANSPARENCY); } else { transparency = pickBestTransparency(img); } BufferedImage compImg = new BufferedImage(width, height, transparency); Graphics2D g = null; try { g = compImg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawImage(img, 0, 0, width, height, null); } finally { if (g != null) { g.dispose(); } } return compImg; }
From source file:net.sourceforge.subsonic.controller.CoverArtControllerEx.java
public static BufferedImage scale(BufferedImage image, int width, int height) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage thumb = image;//from ww w . jav a 2 s . co m // For optimal results, use step by step bilinear resampling - halfing the size at each step. do { w /= 2; h /= 2; if (w < width) { w = width; } if (h < height) { h = height; } double thumbRatio = (double) width / (double) height; double aspectRatio = (double) w / (double) h; // LOG.debug("## thumbsRatio: " + thumbRatio); // LOG.debug("## aspectRatio: " + aspectRatio); if (thumbRatio < aspectRatio) { h = (int) (w / aspectRatio); } else { w = (int) (h * aspectRatio); } BufferedImage temp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(thumb, 0, 0, w, h, null); g2.dispose(); thumb = temp; } while (w != width); //FIXME: check if (thumb.getHeight() > thumb.getWidth()) { thumb = thumb.getSubimage(0, 0, thumb.getWidth(), thumb.getWidth()); } return thumb; }
From source file:com.t3.image.ImageUtil.java
/** * Create a copy of the image that is compatible with the current graphics context * and scaled to the supplied size//w w w . j a va2 s. c om */ public static BufferedImage createCompatibleImage(Image img, int width, int height, Map<String, Object> hints) { width = Math.max(width, 1); height = Math.max(height, 1); int transparency; if (hints != null && hints.containsKey(HINT_TRANSPARENCY)) { transparency = (Integer) hints.get(HINT_TRANSPARENCY); } else { transparency = pickBestTransparency(img); } BufferedImage compImg = new BufferedImage(width, height, transparency); Graphics2D g = null; try { g = compImg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawImage(img, 0, 0, width, height, null); } finally { if (g != null) { g.dispose(); } } return compImg; }
From source file:com.silverpeas.thumbnail.control.ThumbnailController.java
protected static void createCropThumbnailFileOnServer(String pathOriginalFile, String pathCropdir, String pathCropFile, ThumbnailDetail thumbnail, int thumbnailWidth, int thumbnailHeight) { try {// ww w. j a va 2 s. c o m // Creates folder if not exists File dir = new File(pathCropdir); if (!dir.exists()) { FileFolderManager.createFolder(pathCropdir); } // create empty file File cropFile = new File(pathCropFile); if (!cropFile.exists()) { cropFile.createNewFile(); } File originalFile = new File(pathOriginalFile); BufferedImage bufferOriginal = ImageIO.read(originalFile); // crop image BufferedImage cropPicture = bufferOriginal.getSubimage(thumbnail.getXStart(), thumbnail.getYStart(), thumbnail.getXLength(), thumbnail.getYLength()); BufferedImage cropPictureFinal = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB); // Redimensionnement de l'image Graphics2D g2 = cropPictureFinal.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(cropPicture, 0, 0, thumbnailWidth, thumbnailHeight, null); g2.dispose(); // save crop image String extension = FilenameUtils.getExtension(originalFile.getName()); ImageIO.write(cropPictureFinal, extension, cropFile); } catch (Exception e) { SilverTrace.warn("thumbnail", "ThumbnailController.createThumbnailFileOnServer()", "thumbnail_MSG_CREATE_CROP_FILE_KO", "originalFileName=" + thumbnail.getOriginalFileName() + " cropFileName = " + thumbnail.getCropFileName(), e); } }
From source file:game.com.HandleUploadGameThumbServlet.java
public static BufferedImage resizeImage(final Image image, int width, int height) { final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setComposite(AlphaComposite.Src); //below three lines are for RenderingHints for better image quality at cost of higher processing time graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.drawImage(image, 0, 0, width, height, null); graphics2D.dispose();/*w w w . java2 s. c o m*/ return bufferedImage; }