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:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
public static void updateImage(JLabel imageContainer, File imageFile) { if (!imageFile.exists()) { return;// w w w . j a va 2 s . c om } 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 = imageContainer.getWidth(); int imageViewHeight = imageContainer.getHeight(); double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), new Dimension(imageViewWidth, imageViewHeight)); factor = Math.min(factor, 1f); 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; g2.drawImage(img, x, y, imageWidth, imageHeight, null); g2.dispose(); imageContainer.setIcon(new ImageIcon(tmp)); }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * @param bufImg//w w w . java2 s . c om * @param size * @return */ public static BufferedImage generateScaledImage(final BufferedImage bufImg, @SuppressWarnings("unused") final Object hintsArg, final int size) { BufferedImage sourceImage = bufImg; int srcWidth = sourceImage.getWidth(); int srcHeight = sourceImage.getHeight(); double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = size; double multiplier = longSideForDest / longSideForSource; int destWidth = (int) (srcWidth * multiplier); int destHeight = (int) (srcHeight * multiplier); BufferedImage destImage = null; destImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destImage.createGraphics(); graphics2D.drawImage(sourceImage, 0, 0, destWidth, destHeight, null); graphics2D.dispose(); return destImage; }
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;//w ww . ja v a 2 s. co 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.uwsoft.editor.proxy.ResolutionManager.java
public static BufferedImage imageResize(File file, float ratio) { BufferedImage destinationBufferedImage = null; try {/*from w w w.j a v a 2s.c o m*/ BufferedImage sourceBufferedImage = ImageIO.read(file); if (ratio == 1.0) { return sourceBufferedImage; } // When image has to be resized smaller then 3 pixels we should leave it as is, as to ResampleOP limitations // But it should also trigger a warning dialog at the and of the import, to notify the user of non resized images. if (sourceBufferedImage.getWidth() * ratio < 3 || sourceBufferedImage.getHeight() * ratio < 3) { return null; } int newWidth = Math.max(3, Math.round(sourceBufferedImage.getWidth() * ratio)); int newHeight = Math.max(3, Math.round(sourceBufferedImage.getHeight() * ratio)); String name = file.getName(); Integer[] patches = null; if (name.endsWith(EXTENSION_9PATCH)) { patches = NinePatchUtils.findPatches(sourceBufferedImage); sourceBufferedImage = NinePatchUtils.removePatches(sourceBufferedImage); newWidth = Math.round(sourceBufferedImage.getWidth() * ratio); newHeight = Math.round(sourceBufferedImage.getHeight() * ratio); System.out.println(sourceBufferedImage.getWidth()); destinationBufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = destinationBufferedImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); g2.drawImage(sourceBufferedImage, 0, 0, newWidth, newHeight, null); g2.dispose(); } else { // resize with bilinear filter ResampleOp resampleOp = new ResampleOp(newWidth, newHeight); destinationBufferedImage = resampleOp.filter(sourceBufferedImage, null); } if (patches != null) { destinationBufferedImage = NinePatchUtils.convertTo9Patch(destinationBufferedImage, patches, ratio); } } catch (IOException ignored) { } return destinationBufferedImage; }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
public static BufferedImage scale(BufferedImage bi, int width, int height) { BufferedImage bi2;/*from w w w.jav a 2 s .c o m*/ int scaleWidth = bi.getWidth(null); int scaleHeight = bi.getHeight(null); double scaleX = (double) width / scaleWidth; double scaleY = (double) height / scaleHeight; double scale = Math.min(scaleX, scaleY); scaleWidth = (int) ((double) scaleWidth * scale); scaleHeight = (int) ((double) scaleHeight * scale); Image scaledImage; if (HEADLESS) { // create a new buffered image, don't rely on a local graphics system (headless mode) final int type; if (bi.getType() != BufferedImage.TYPE_CUSTOM) { type = bi.getType(); } else if (bi.getAlphaRaster() != null) { // alpha channel available type = BufferedImage.TYPE_INT_ARGB; } else { type = BufferedImage.TYPE_INT_RGB; } bi2 = new BufferedImage(scaleWidth, scaleHeight, type); } else { GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration(); bi2 = gc.createCompatibleImage(scaleWidth, scaleHeight, bi.getTransparency()); } Graphics2D g = bi2.createGraphics(); if (scale < 0.3 && Math.max(scaleWidth, scaleHeight) < 500) { scaledImage = bi.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH); new ImageIcon(scaledImage).getImage(); g.drawImage(scaledImage, 0, 0, scaleWidth, scaleHeight, null); } else { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.drawImage(bi, 0, 0, scaleWidth, scaleHeight, null); } g.dispose(); return bi2; }
From source file:com.o2d.pkayjava.editor.proxy.ResolutionManager.java
public static BufferedImage imageResize(File file, float ratio) { BufferedImage destinationBufferedImage = null; try {// ww w . j av a 2 s . c om BufferedImage sourceBufferedImage = ImageIO.read(file); if (ratio == 1.0) { return null; } // When image has to be resized smaller then 3 pixels we should leave it as is, as to ResampleOP limitations // But it should also trigger a warning dialog at the and of the import, to notify the user of non resized images. if (sourceBufferedImage.getWidth() * ratio < 3 || sourceBufferedImage.getHeight() * ratio < 3) { return null; } int newWidth = Math.max(3, Math.round(sourceBufferedImage.getWidth() * ratio)); int newHeight = Math.max(3, Math.round(sourceBufferedImage.getHeight() * ratio)); String name = file.getName(); Integer[] patches = null; if (name.endsWith(EXTENSION_9PATCH)) { patches = NinePatchUtils.findPatches(sourceBufferedImage); sourceBufferedImage = NinePatchUtils.removePatches(sourceBufferedImage); newWidth = Math.round(sourceBufferedImage.getWidth() * ratio); newHeight = Math.round(sourceBufferedImage.getHeight() * ratio); System.out.println(sourceBufferedImage.getWidth()); destinationBufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = destinationBufferedImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); g2.drawImage(sourceBufferedImage, 0, 0, newWidth, newHeight, null); g2.dispose(); } else { // resize with bilinear filter ResampleOp resampleOp = new ResampleOp(newWidth, newHeight); destinationBufferedImage = resampleOp.filter(sourceBufferedImage, null); } if (patches != null) { destinationBufferedImage = NinePatchUtils.convertTo9Patch(destinationBufferedImage, patches, ratio); } } catch (IOException ignored) { } return destinationBufferedImage; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * @param name// ww w. j a va2 s . c om * @param imgIcon * @return */ public static String uuencodeImage(final String name, final ImageIcon imgIcon) { try { BufferedImage tmp = new BufferedImage(imgIcon.getIconWidth(), imgIcon.getIconWidth(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = tmp.createGraphics(); //g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(imgIcon.getImage(), 0, 0, imgIcon.getIconWidth(), imgIcon.getIconWidth(), null); g2.dispose(); ByteArrayOutputStream output = new ByteArrayOutputStream(8192); ImageIO.write(tmp, "PNG", output); byte[] outputBytes = output.toByteArray(); output.close(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); UUEncoder uuencode = new UUEncoder(name); uuencode.encode(new ByteArrayInputStream(outputBytes), bos); return bos.toString(); } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(GraphicsUtils.class, ex); ex.printStackTrace(); } return ""; }
From source file:com.sketchy.image.ImageProcessingThread.java
public static BufferedImage resizeImage(BufferedImage image, int drawingWidth, int drawingHeight, CenterOption centerOption, ScaleOption scaleOption) { int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double widthScale = drawingWidth / (double) imageWidth; double heightScale = drawingHeight / (double) imageHeight; double scale = Math.min(widthScale, heightScale); int scaleWidth = (int) (imageWidth * scale); int scaleHeight = (int) (imageHeight * scale); BufferedImage resizedImage = new BufferedImage(scaleWidth, scaleHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resizedImage.createGraphics(); if (scaleOption == ScaleOption.SCALE_AREA_AVERAGING) { ImageProducer prod = new FilteredImageSource(image.getSource(), new AreaAveragingScaleFilter(scaleWidth, scaleHeight)); Image img = Toolkit.getDefaultToolkit().createImage(prod); g.drawImage(img, 0, 0, scaleWidth, scaleHeight, null); // it's already scaled } else {/* ww w . j a va2s. c o m*/ if (scaleOption == ScaleOption.SCALE_NEAREST_NEIGHBOR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); } else if (scaleOption == ScaleOption.SCALE_BICUBIC) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } else if (scaleOption == ScaleOption.SCALE_BILINEAR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); } g.drawImage(image, 0, 0, scaleWidth, scaleHeight, 0, 0, imageWidth, imageHeight, null); } g.dispose(); int cropWidth = (int) Math.min(scaleWidth, drawingWidth); int cropHeight = (int) Math.min(scaleHeight, drawingHeight); int drawingLeft = 0; int drawingTop = 0; if ((centerOption == CenterOption.CENTER_HORIZONTAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingLeft = (int) ((drawingWidth - cropWidth) / 2.0); } if ((centerOption == CenterOption.CENTER_VERTICAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingTop = (int) ((drawingHeight - cropHeight) / 2.0); } BufferedImage croppedImage = resizedImage.getSubimage(0, 0, cropWidth, cropHeight); resizedImage = null; BufferedImage drawingImage = new BufferedImage(drawingWidth, drawingHeight, BufferedImage.TYPE_INT_ARGB); g = drawingImage.createGraphics(); g.drawImage(croppedImage, drawingLeft, drawingTop, drawingLeft + cropWidth, drawingTop + cropHeight, 0, 0, cropWidth, cropHeight, null); g.dispose(); croppedImage = null; return drawingImage; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Convenience method that returns a scaled instance of the * provided {@code BufferedImage}.//from w w w.j a va 2 s . co m * * Code stolen from http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * * @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 down-scaling 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(final BufferedImage img, final int targetWidth, final int targetHeight, final boolean higherQuality) { int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage temp = img; BufferedImage result = new BufferedImage(targetWidth, targetHeight, type); Graphics2D g2 = result.createGraphics(); if (higherQuality) { g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); } g2.drawImage(temp, 0, 0, targetWidth, targetHeight, null); g2.dispose(); return result; }
From source file:net.pms.util.UMSUtils.java
@SuppressWarnings("deprecation") public static InputStream scaleThumb(InputStream in, RendererConfiguration r) throws IOException { if (in == null) { return in; }//from www.ja v a 2s.c o m String ts = r.getThumbSize(); if (StringUtils.isEmpty(ts) && StringUtils.isEmpty(r.getThumbBG())) { // no need to convert here return in; } int w; int h; Color col = null; BufferedImage img; try { img = ImageIO.read(in); } catch (Exception e) { // catch whatever is thrown at us // we can at least log it LOGGER.debug("couldn't read thumb to manipulate it " + e); img = null; // to make sure } if (img == null) { return in; } w = img.getWidth(); h = img.getHeight(); if (StringUtils.isNotEmpty(ts)) { // size limit thumbnail w = getHW(ts.split("x"), 0); h = getHW(ts.split("x"), 1); if (w == 0 || h == 0) { LOGGER.debug("bad thumb size {} skip scaling", ts); w = h = 0; // just to make sure } } if (StringUtils.isNotEmpty(r.getThumbBG())) { try { Field field = Color.class.getField(r.getThumbBG()); col = (Color) field.get(null); } catch (Exception e) { LOGGER.debug("bad color name " + r.getThumbBG()); } } if (w == 0 && h == 0 && col == null) { return in; } ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage img1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = img1.createGraphics(); if (col != null) { g.setColor(col); } g.fillRect(0, 0, w, h); g.drawImage(img, 0, 0, w, h, null); ImageIO.write(img1, "jpeg", out); out.flush(); return new ByteArrayInputStream(out.toByteArray()); }