Java examples for 2D Graphics:Image Size
Creates a thumb of the specified image with the given thumb size.
//package com.java2s; import java.awt.*; import java.awt.image.*; public class Main { /**/*from w w w . j a v a 2 s. com*/ * Creates a thumb of the specified image with the given thumb size. * @param img * @param thumbSize * @return */ public static BufferedImage CreateThumb(BufferedImage img, int thumbSize) { try { if (img.getWidth() < thumbSize && img.getHeight() < thumbSize) { return (addPhotoBorder(img, 2, 1, Color.DARK_GRAY, Color.LIGHT_GRAY)); } BufferedImage ret; if (img.getWidth() < img.getHeight()) { ret = getScaledInstance(img, -1, thumbSize, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } else { ret = getScaledInstance(img, thumbSize, -1, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } return (addPhotoBorder(ret, 2, 1, Color.DARK_GRAY, Color.LIGHT_GRAY)); } catch (Exception e) { e.printStackTrace(); } return (null); } /** * Adds a two colored border to the given image. * @param src * @param outer * @param inner * @param colOuter * @param colInner * @return */ public static BufferedImage addPhotoBorder(BufferedImage src, int outer, int inner, Color colOuter, Color colInner) { BufferedImage img = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.setColor(colOuter); g2d.fillRect(0, 0, img.getWidth(), img.getHeight()); g2d.setColor(colInner); g2d.fillRect(outer, outer, img.getWidth() - 2 * outer, img.getHeight() - 2 * outer); g2d.drawImage(src, outer + inner, outer + inner, img.getWidth() - 2 * (outer + inner), img.getHeight() - 2 * (outer + inner), null); g2d.dispose(); return img; } /** * Gets a scaled instance of the source image. * Uses Graphics2D for scaling. * @param img * @param destw Target width, -1 if width should be in correct aspect ratio to target height. * @param desth Target height, -1 if height should be in correct aspect ratio to target width. * @param hint See RenderingHints.VALUE_INTERPOLATION_XXX * @return */ public static BufferedImage getScaledInstance(BufferedImage img, int destw, int desth, Object hint) { Dimension d = getScaledSize(img.getWidth(), img.getHeight(), destw, desth); BufferedImage bimg = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bimg.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g2d.drawImage(img, 0, 0, d.width, d.height, null); g2d.dispose(); return (bimg); } /** * Calculates the scaled size. * @param ow * @param oh * @param tw * @param th * @return */ private static Dimension getScaledSize(int ow, int oh, int tw, int th) { if ((tw > -1 && th > -1)) { return new Dimension(tw, th); } else if (tw < 0 && th < 0) { return new Dimension(ow, oh); } float fAspect = (float) ow / (float) oh; if (tw == -1) { // change width tw = (int) (th * fAspect); } else if (th == -1) { // change height th = (int) (tw / fAspect); } return new Dimension(tw, th); } }