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.funambol.foundation.util.MediaUtils.java
/** * Creates the thumbnail./* w ww .j ava 2 s. c o m*/ * * @param imageFile the image file * @param thumbFile the empty thumbnail file * @param thumbX the width of the thumbnail * @param thumbY the height of the thumbnail * @param imageName the image file name with extension * @param tolerance the percentage of tolerance before creating a thumbnail * @return true is the thumbnail has been created, false otherwise * @throws IOException if an error occurs */ private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName, double tolerance) throws IOException { FileInputStream fileis = null; ImageInputStream imageis = null; Iterator readers = null; try { readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1)); if (readers == null || (!readers.hasNext())) { throw new IOException("File not supported"); } ImageReader reader = (ImageReader) readers.next(); fileis = new FileInputStream(imageFile); imageis = ImageIO.createImageInputStream(fileis); reader.setInput(imageis, true); // Determines thumbnail height, width and quality int thumbWidth = thumbX; int thumbHeight = thumbY; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = reader.getWidth(0); int imageHeight = reader.getHeight(0); // // Don't create the thumbnail if the original file is smaller // than required size increased by % tolerance // if (imageWidth <= (thumbWidth * (1 + tolerance / 100)) && imageHeight <= (thumbHeight * (1 + tolerance / 100))) { return false; } double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(3, 3, 0, 0); BufferedImage bi = reader.read(0, param); Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH); BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null); FileOutputStream fileOutputStream = new FileOutputStream(thumbFile); ImageIO.write(thumbImage, "jpg", fileOutputStream); thumb.flush(); thumbImage.flush(); fileOutputStream.flush(); fileOutputStream.close(); graphics2D.dispose(); } finally { if (fileis != null) { fileis.close(); } if (imageis != null) { imageis.close(); } } return true; }
From source file:ImageOpByRomain.java
/** * <p>//from w w w. j ava2 s. com * Returns a thumbnail of a source image. * </p> * <p> * This method favors speed over quality. When the new size is less than half * the longest dimension of the source image, * {@link #createThumbnail(BufferedImage, int)} or * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to * ensure the quality of the result without sacrificing too much performance. * </p> * * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image * the source image * @param newWidth * the width of the thumbnail * @param newHeight * the height of the thumbnail * @return a new compatible <code>BufferedImage</code> containing a * thumbnail of <code>image</code> * @throws IllegalArgumentException * if <code>newWidth</code> is larger than the width of * <code>image</code> or if code>newHeight</code> is larger * than the height of <code>image</code> or if one of the * dimensions is <= 0 */ public static BufferedImage createThumbnailFast(BufferedImage image, int newWidth, int newHeight) { if (newWidth >= image.getWidth() || newHeight >= image.getHeight()) { throw new IllegalArgumentException( "newWidth and newHeight cannot" + " be greater than the image" + " dimensions"); } else if (newWidth <= 0 || newHeight <= 0) { throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0"); } BufferedImage temp = createCompatibleImage(image, newWidth, newHeight); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null); g2.dispose(); return temp; }
From source file:nl.b3p.viewer.image.ImageTool.java
/** * Draws the image to the graphics object. * @param gbi graphics object/*w w w . ja va 2s . c o m*/ * @param image the referenced image. */ private static void drawImage(Graphics2D gbi, ReferencedImage image) { if (image.getAlpha() != null) { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, image.getAlpha())); } else { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); } Integer x = image.getX(); Integer y = image.getY(); if (x == null) { x = 0; } if (y == null) { y = 0; } if (image.getHeight() != null && image.getWidth() != null) { gbi.drawImage(image.getImage(), x, y, image.getWidth(), image.getHeight(), null); } else { gbi.drawImage(image.getImage(), x, y, null); } }
From source file:ImageOpByRomain.java
/** * <p>// w ww .j a v a 2 s. co m * Returns a thumbnail of a source image. <code>newSize</code> defines the * length of the longest dimension of the thumbnail. The other dimension is * then computed according to the dimensions ratio of the original picture. * </p> * <p> * This method favors speed over quality. When the new size is less than half * the longest dimension of the source image, * {@link #createThumbnail(BufferedImage, int)} or * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to * ensure the quality of the result without sacrificing too much performance. * </p> * * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image * the source image * @param newSize * the length of the largest dimension of the thumbnail * @return a new compatible <code>BufferedImage</code> containing a * thumbnail of <code>image</code> * @throws IllegalArgumentException * if <code>newSize</code> is larger than the largest dimension * of <code>image</code> or <= 0 */ public static BufferedImage createThumbnailFast(BufferedImage image, int newSize) { float ratio; int width = image.getWidth(); int height = image.getHeight(); if (width > height) { if (newSize >= width) { throw new IllegalArgumentException("newSize must be lower than" + " the image width"); } else if (newSize <= 0) { throw new IllegalArgumentException("newSize must" + " be greater than 0"); } ratio = (float) width / (float) height; width = newSize; height = (int) (newSize / ratio); } else { if (newSize >= height) { throw new IllegalArgumentException("newSize must be lower than" + " the image height"); } else if (newSize <= 0) { throw new IllegalArgumentException("newSize must" + " be greater than 0"); } ratio = (float) height / (float) width; height = newSize; width = (int) (newSize / ratio); } BufferedImage temp = createCompatibleImage(image, width, height); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null); g2.dispose(); return temp; }
From source file:ImageOpByRomain.java
/** * <p>//ww w . j a v a2 s . c om * Returns a thumbnail of a source image. * </p> * <p> * This method offers a good trade-off between speed and quality. The result * looks better than * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the * new size is less than half the longest dimension of the source image, yet * the rendering speed is almost similar. * </p> * * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @param image * the source image * @param newWidth * the width of the thumbnail * @param newHeight * the height of the thumbnail * @return a new compatible <code>BufferedImage</code> containing a * thumbnail of <code>image</code> * @throws IllegalArgumentException * if <code>newWidth</code> is larger than the width of * <code>image</code> or if code>newHeight</code> is larger * than the height of <code>image or if one the dimensions is not * > 0</code> */ public static BufferedImage createThumbnail(BufferedImage image, int newWidth, int newHeight) { int width = image.getWidth(); int height = image.getHeight(); if (newWidth >= width || newHeight >= height) { throw new IllegalArgumentException( "newWidth and newHeight cannot" + " be greater than the image" + " dimensions"); } else if (newWidth <= 0 || newHeight <= 0) { throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0"); } BufferedImage thumb = image; do { if (width > newWidth) { width /= 2; if (width < newWidth) { width = newWidth; } } if (height > newHeight) { height /= 2; if (height < newHeight) { height = newHeight; } } BufferedImage temp = createCompatibleImage(image, width, height); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null); g2.dispose(); thumb = temp; } while (width != newWidth || height != newHeight); return thumb; }
From source file:ImageOpByRomain.java
/** * <p>//from w ww. j a v a 2s. c o m * Returns a thumbnail of a source image. <code>newSize</code> defines the * length of the longest dimension of the thumbnail. The other dimension is * then computed according to the dimensions ratio of the original picture. * </p> * <p> * This method offers a good trade-off between speed and quality. The result * looks better than * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the * new size is less than half the longest dimension of the source image, yet * the rendering speed is almost similar. * </p> * * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image * the source image * @param newSize * the length of the largest dimension of the thumbnail * @return a new compatible <code>BufferedImage</code> containing a * thumbnail of <code>image</code> * @throws IllegalArgumentException * if <code>newSize</code> is larger than the largest dimension * of <code>image</code> or <= 0 */ public static BufferedImage createThumbnail(BufferedImage image, int newSize) { int width = image.getWidth(); int height = image.getHeight(); boolean isWidthGreater = width > height; if (isWidthGreater) { if (newSize >= width) { throw new IllegalArgumentException("newSize must be lower than" + " the image width"); } } else if (newSize >= height) { throw new IllegalArgumentException("newSize must be lower than" + " the image height"); } if (newSize <= 0) { throw new IllegalArgumentException("newSize must" + " be greater than 0"); } float ratioWH = (float) width / (float) height; float ratioHW = (float) height / (float) width; BufferedImage thumb = image; do { if (isWidthGreater) { width /= 2; if (width < newSize) { width = newSize; } height = (int) (width / ratioWH); } else { height /= 2; if (height < newSize) { height = newSize; } width = (int) (height / ratioHW); } BufferedImage temp = createCompatibleImage(image, width, height); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null); g2.dispose(); thumb = temp; } while (newSize != (isWidthGreater ? width : height)); return thumb; }
From source file:com.joliciel.jochre.search.highlight.ImageSnippet.java
public BufferedImage getImage() { try {/*from ww w. j av a2 s . c om*/ BufferedImage originalImage = ImageIO.read(imageFile); BufferedImage imageSnippet = new BufferedImage(this.rectangle.getWidth(), this.rectangle.getHeight(), BufferedImage.TYPE_INT_ARGB); originalImage = originalImage.getSubimage(this.rectangle.getLeft(), this.rectangle.getTop(), this.rectangle.getWidth(), this.rectangle.getHeight()); Graphics2D graphics2D = imageSnippet.createGraphics(); graphics2D.drawImage(originalImage, 0, 0, this.rectangle.getWidth(), this.rectangle.getHeight(), null); int extra = 2; for (Rectangle rect : this.highlights) { graphics2D.setStroke(new BasicStroke(1)); graphics2D.setPaint(Color.BLACK); graphics2D.drawRect(rect.getLeft() - this.rectangle.getLeft() - extra, rect.getTop() - this.rectangle.getTop() - extra, rect.getWidth() + (extra * 2), rect.getHeight() + (extra * 2)); graphics2D.setColor(new Color(255, 255, 0, 127)); graphics2D.fillRect(rect.getLeft() - this.rectangle.getLeft() - extra, rect.getTop() - this.rectangle.getTop() - extra, rect.getWidth() + (extra * 2), rect.getHeight() + (extra * 2)); } return imageSnippet; } catch (IOException e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:net.sf.jabref.gui.PdfPreviewPanel.java
private BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) { int h = originalImage.getHeight(); int w = originalImage.getWidth(); if ((height == 0) || (width == 0)) { height = h;//from w w w . j a va 2 s.co m width = w; } else { float factorH = (float) height / (float) h; float factorW = (float) width / (float) w; if (factorH < factorW) { // use factorH, only width has to be changed as height is // already correct width = Math.round(w * factorH); } else { width = Math.round(h * factorW); } } BufferedImage resizedImage = new BufferedImage(width, height, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, height, null); return resizedImage; }
From source file:nl.b3p.imagetool.ImageTool.java
/** * Draws the image to the graphics object. * * @param gbi graphics object/*from ww w.j a v a2 s . c o m*/ * @param image the referenced image. */ private static void drawImage(Graphics2D gbi, ReferencedImage image) throws Exception { if (image.getAlpha() != null) { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, image.getAlpha())); } else { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); } Integer x = image.getX(); Integer y = image.getY(); if (x == null) { x = 0; } if (y == null) { y = 0; } if (image.getHeight() != null && image.getWidth() != null) { gbi.drawImage(image.getImage(), x, y, image.getWidth(), image.getHeight(), null); } else { gbi.drawImage(image.getImage(), x, y, null); } }
From source file:doge.photo.DogePhotoManipulator.java
private void renderBackground(BufferedImage sourceImage, BufferedImage destinationImage, Graphics2D destinationGraphics) { destinationGraphics.drawImage(sourceImage, 0, 0, IMAGE_WIDTH, destinationImage.getHeight(), null); }