List of usage examples for java.awt Graphics2D dispose
public abstract void dispose();
From source file:com.o2d.pkayjava.editor.proxy.ResolutionManager.java
public static BufferedImage imageResize(File file, float ratio) { BufferedImage destinationBufferedImage = null; try {/*from w w w . j a va 2s .c o m*/ 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:iqq.util.ImageUtil.java
/** * //from ww w. j a va 2s . c o m * * @param srcFile ? * @param destFile * @param destWidth * @param destHeight */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); graphics2D.dispose(); FileOutputStream out = new FileOutputStream(destFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage); param.setQuality((float) DEST_QUALITY / 100, false); encoder.setJPEGEncodeParam(param); encoder.encode(destBufferedImage); out.close(); } catch (IOException e) { e.printStackTrace(); } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:net.shopxx.util.ImageUtil.java
/** * /*from w ww . ja v a 2 s . co m*/ * @param srcFile ? * @param destFile * @param destWidth * @param destHeight */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); graphics2D.dispose(); FileOutputStream out = new FileOutputStream(destFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage); param.setQuality((float) DEST_QUALITY / 100, false); encoder.setJPEGEncodeParam(param); encoder.encode(destBufferedImage); out.close(); } catch (IOException e) { e.printStackTrace(); } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Gets a scaled icon and if it doesn't exist it creates one and scales it * @param icon image to be scaled/*from w ww.ja va2 s.com*/ * @param iconSize the icon size (Std) * @param scaledIconSize the new scaled size in pixels * @return the scaled icon */ public static BufferedImage getBufferedImage(final ImageIcon icon) { Image imgMemory = icon.getImage(); //make sure all pixels in the image were loaded imgMemory = new ImageIcon(imgMemory).getImage(); int w = icon.getIconWidth(); int h = icon.getIconHeight(); BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgMemory, 0, 0, w, h, 0, 0, w, h, null); graphics2D.dispose(); return bufferedImage; }
From source file:fr.msch.wissl.server.Library.java
public static String resizeArtwork(String artPath) throws IOException { BufferedImage orig = ImageIO.read(new File(artPath)); if (orig == null) { throw new IOException("Failed to open image"); }//from w ww .j a va2 s. c o m Image sc = orig.getScaledInstance(70, 70, Image.SCALE_SMOOTH); BufferedImage scaled = new BufferedImage(70, 70, BufferedImage.TYPE_INT_RGB); Graphics2D g = scaled.createGraphics(); g.drawImage(sc, 0, 0, 70, 70, null); g.dispose(); File ret = new File(artPath + "_SCALED.jpg"); ImageIO.write(scaled, "JPG", ret); return ret.getAbsolutePath(); }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Convenience method that returns a scaled instance of the * provided {@code BufferedImage}./*from ww w. j a v a 2 s. c om*/ * * 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: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 w w. ja va 2 s . com*/ } 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:edu.ku.brc.ui.GraphicsUtils.java
/** * @param name/*from ww w .j a v a 2s. c o m*/ * @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:at.tugraz.sss.serv.SSFileU.java
public static void scalePNGAndWrite(final BufferedImage buffImage, final String pngFilePath, final Integer width, final Integer height) throws IOException { final BufferedImage scaledThumb = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D graphics2D = scaledThumb.createGraphics(); graphics2D.setComposite(AlphaComposite.Src); graphics2D.drawImage(buffImage, 0, 0, width, height, null); graphics2D.dispose(); ImageIO.write(scaledThumb, SSFileExtE.png.toString(), new File(pngFilePath)); }
From source file:com.t3.persistence.PersistenceUtil.java
static public void saveCampaignThumbnail(String fileName) { BufferedImage screen = TabletopTool.takeMapScreenShot(new PlayerView(TabletopTool.getPlayer().getRole())); if (screen == null) return;/*from w ww. j av a2s . com*/ Dimension imgSize = new Dimension(screen.getWidth(null), screen.getHeight(null)); SwingUtil.constrainTo(imgSize, 200, 200); BufferedImage thumb = new BufferedImage(imgSize.width, imgSize.height, BufferedImage.TYPE_INT_BGR); Graphics2D g2d = thumb.createGraphics(); g2d.drawImage(screen, 0, 0, imgSize.width, imgSize.height, null); g2d.dispose(); File thumbFile = getCampaignThumbnailFile(fileName); try { ImageIO.write(thumb, "jpg", thumbFile); } catch (IOException ioe) { TabletopTool.showError("msg.error.failedSaveCampaignPreview", ioe); } }