List of usage examples for java.awt Graphics2D setRenderingHint
public abstract void setRenderingHint(Key hintKey, Object hintValue);
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 {// www . j a v a2 s . c o m 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:eu.planets_project.tb.impl.data.util.ImageThumbnail.java
/** * Create a scaled jpeg of an image. The width/height ratio is preserved. * /*from w w w . ja v a 2 s .com*/ * <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:de.bamamoto.mactools.png2icns.Scaler.java
public static BufferedImage resize(BufferedImage source, int width, int height) { double xScale = ((double) width) / (double) source.getWidth(); double yScale = ((double) height) / (double) source.getHeight(); BufferedImage result = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration()//from www . j a va 2 s . c o m .createCompatibleImage(width, height, source.getColorModel().getTransparency()); Graphics2D newImage = null; try { newImage = result.createGraphics(); newImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); newImage.drawRenderedImage(source, AffineTransform.getScaleInstance(xScale, yScale)); } finally { if (newImage != null) { newImage.dispose(); } } return result; }
From source file:Utils.java
/** * Renders a paragraph of text (line breaks ignored) to an image (created and returned). * * @param font The font to use/* w w w . j av a 2 s. c o m*/ * @param textColor The color of the text * @param text The message * @param width The width the text should be limited to * @return An image with the text rendered into it */ public static BufferedImage renderTextToImage(Font font, Color textColor, String text, int width) { Hashtable map = new Hashtable(); map.put(TextAttribute.FONT, font); AttributedString attributedString = new AttributedString(text, map); AttributedCharacterIterator paragraph = attributedString.getIterator(); FontRenderContext frc = new FontRenderContext(null, false, false); int paragraphStart = paragraph.getBeginIndex(); int paragraphEnd = paragraph.getEndIndex(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc); float drawPosY = 0; //First time around, just determine the height while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(width); // Move it down drawPosY += layout.getAscent() + layout.getDescent() + layout.getLeading(); } BufferedImage image = createCompatibleImage(width, (int) drawPosY); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); drawPosY = 0; lineMeasurer.setPosition(paragraphStart); while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(width); // Move y-coordinate by the ascent of the layout. drawPosY += layout.getAscent(); /* Compute pen x position. If the paragraph is right-to-left, we want to align the TextLayouts to the right edge of the panel. */ float drawPosX; if (layout.isLeftToRight()) { drawPosX = 0; } else { drawPosX = width - layout.getAdvance(); } // Draw the TextLayout at (drawPosX, drawPosY). layout.draw(graphics, drawPosX, drawPosY); // Move y-coordinate in preparation for next layout. drawPosY += layout.getDescent() + layout.getLeading(); } graphics.dispose(); return image; }
From source file:net.mindengine.oculus.frontend.web.controllers.project.ProjectEditController.java
private static BufferedImage resize(BufferedImage image, int width, int height) { BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resizedImage.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(image, 0, 0, width, height, null); g.dispose();//from w ww . j av a 2 s .c om return resizedImage; }
From source file:com.igormaznitsa.jhexed.renders.svg.SVGImage.java
private static void processAntialias(final boolean flag, final Graphics2D g) { if (flag) {//from ww w. j a va 2 s . com g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); } else { g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED); } }
From source file:org.apache.stratos.theme.mgt.ui.processors.AddThemeResourceProcessor.java
private static DataHandler scaleImage(DataHandler dataHandler, int height, int width) throws IOException { Image image = ImageIO.read(new BufferedInputStream(dataHandler.getInputStream())); // Check if the image has transparent pixels boolean hasAlpha = ((BufferedImage) image).getColorModel().hasAlpha(); // Maintain Aspect ratio int thumbHeight = height; int thumbWidth = width; double thumbRatio = (double) width / (double) height; double imageRatio = (double) image.getWidth(null) / (double) image.getHeight(null); if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else {//from w w w. j a v a 2s .c o m thumbWidth = (int) (thumbHeight * imageRatio); } BufferedImage thumb; // Check if transparent pixels are available and set the color mode accordingly if (hasAlpha) { thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); } else { thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); } Graphics2D graphics2D = thumb.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); // Save the image as PNG so that transparent images are rendered as intended ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageIO.write(thumb, "PNG", output); DataSource dataSource = new ByteArrayDataSource(output.toByteArray(), "application/octet-stream"); return new DataHandler(dataSource); }
From source file:com.jaeksoft.searchlib.util.ImageUtils.java
public final static BufferedImage reduceImage(BufferedImage image, int width, int height) { int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (BufferedImage) image; int w = image.getWidth(); int h = image.getHeight(); while (w != width || h != height) { if (w > width) { w /= 2;//w w w. j ava2 s .c o m if (w < width) w = width; } if (h > height) { h /= 2; if (h < height) h = height; } BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } return ret; }
From source file:net.sourceforge.subsonic.controller.CoverArtController.java
public static BufferedImage scale(BufferedImage image, int width, int height) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage thumb = image;//from w w w . j a v a 2 s. c o 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; } 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, temp.getWidth(), temp.getHeight(), null); g2.dispose(); thumb = temp; } while (w != width); return thumb; }
From source file:Main.java
/** * Fills a gradient using the start color and end color specified. * * @param g/*w w w. j av a 2 s . co m*/ * the gradient * @param s * the shape to fill * @param start * the start color * @param end * the end color * @param vertical * true for a vertical gradient; false for horizontal */ public static void fillGradient(Graphics2D g, Shape s, Color start, Color end, boolean vertical) { Rectangle r = s.getBounds(); Paint gp = new GradientPaint(0, 0, start, vertical ? 0 : r.width, vertical ? r.height : 0, end); Object o = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setPaint(gp); g.fill(s); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, o); }