List of usage examples for java.awt Image getWidth
public abstract int getWidth(ImageObserver observer);
From source file:Main.java
/** * Draw the image with 9 grids scaling/* ww w .j a v a 2s . co m*/ * * @param g * @param img * @param dx1 * @param dy1 * @param dx2 * @param dy2 * @param insets * @param paintCenter */ public static void drawImageWith9Grids(Graphics g, Image img, int dx1, int dy1, int dx2, int dy2, Insets insets, boolean paintCenter) { final int imgWidth = img.getWidth(null); final int imgHeight = img.getHeight(null); // top-left cornor g.drawImage(img, dx1, dy1, dx1 + insets.left, dy1 + insets.top, 0, 0, insets.left, insets.top, null); // top-right cornor g.drawImage(img, dx2 - insets.right, dy1, dx2, dy1 + insets.top, imgWidth - insets.right, 0, imgWidth, insets.top, null); // bottom-left cornor g.drawImage(img, dx1, dy2 - insets.bottom, dx1 + insets.left, dy2, 0, imgHeight - insets.bottom, insets.left, imgHeight, null); // bottom-right cornor g.drawImage(img, dx2 - insets.right, dy2 - insets.bottom, dx2, dy2, imgWidth - insets.right, imgHeight - insets.bottom, imgWidth, imgHeight, null); // top border g.drawImage(img, dx1 + insets.left, dy1, dx2 - insets.right, dy1 + insets.top, insets.left, 0, imgWidth - insets.right, insets.top, null); // bottom border g.drawImage(img, dx1 + insets.left, dy2 - insets.bottom, dx2 - insets.right, dy2, insets.left, imgHeight - insets.bottom, imgWidth - insets.right, imgHeight, null); // left border g.drawImage(img, dx1, dy1 + insets.top, dx1 + insets.left, dy2 - insets.bottom, 0, insets.top, insets.left, imgHeight - insets.bottom, null); // right border g.drawImage(img, dx2 - insets.right, dy1 + insets.top, dx2, dy2 - insets.bottom, imgWidth - insets.right, insets.top, imgWidth, imgHeight - insets.bottom, null); // center if (paintCenter) { g.drawImage(img, dx1 + insets.left, dy1 + insets.top, dx2 - insets.right, dy2 - insets.bottom, insets.left, insets.top, imgWidth - insets.right, imgHeight - insets.bottom, null); } }
From source file:com.chinarewards.gwt.license.util.ImageDisposeUtil.java
/** * @param imgsrc // ww w. j a va 2 s.c o m * @param imgdist ? * @param widthdist * @param heightdist * @param int benchmark :0,12 * */ public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist, int benchmark) { try { // System.out.println("*******widthdist********:"+widthdist); // System.out.println("*******heightdist********:"+heightdist); // System.out.println("*******benchmark********:"+benchmark); File srcfile = new File(imgsrc); if (!srcfile.exists()) { return; } Image src = javax.imageio.ImageIO.read(srcfile); int width = src.getWidth(null); int height = src.getHeight(null); if (width <= widthdist && height <= heightdist) { // SysUtil.cpoyFile(imgsrc, imgdist); FileUtils.copyFile(new File(imgsrc), new File(imgdist)); return; } // float wh = (float) width / (float) height; if (benchmark == 0) { if (wh > 1) { float tmp_heigth = (float) widthdist / wh; BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } else { float tmp_width = (float) heightdist * wh; BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } if (benchmark == 1) { float tmp_heigth = (float) widthdist / wh; BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } if (benchmark == 2) { float tmp_width = (float) heightdist * wh; BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } catch (IOException ex) { logger.error(ex); } }
From source file:Main.java
/** * Converts a {@link Image} into a {@link BufferedImage}. If the image * is already a {@link BufferedImage} then the same instance is returned. * * @param image the image to convert to a buffered image. * @param imageType the image type to use. * @return the converted image or the same instance if already a * {@link BufferedImage}.// www. j a va2 s. co m */ public static BufferedImage toBufferedImage(Image image, int imageType) { if (image instanceof BufferedImage) { return (BufferedImage) image; } final BufferedImage buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), imageType); final Graphics2D gfx = buffImage.createGraphics(); gfx.drawImage(image, 0, 0, null); return buffImage; }
From source file:Main.java
/** * Create a custom cursor out of the specified image, with the specified hotspot. *//*w w w . ja va2 s .co m*/ public static Cursor createImageCursor(Image img, Point hotspot) { Toolkit tk = Toolkit.getDefaultToolkit(); // for now, just report the cursor restrictions, then blindly create int w = img.getWidth(null); int h = img.getHeight(null); Dimension d = tk.getBestCursorSize(w, h); // int colors = tk.getMaximumCursorColors(); // Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h + // ", bestSize=" + d.width + "x" + d.height + // ", maxcolors=" + colors + "]."); // if the passed-in image is smaller, pad it with transparent pixels and use it anyway. if (((w < d.width) && (h <= d.height)) || ((w <= d.width) && (h < d.height))) { Image padder = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().createCompatibleImage(d.width, d.height, Transparency.BITMASK); Graphics g = padder.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); // and reassign the image to the padded image img = padder; // and adjust the 'best' to cheat the hotspot checking code d.width = w; d.height = h; } // make sure the hotspot is valid if (hotspot == null) { hotspot = new Point(d.width / 2, d.height / 2); } else { hotspot.x = Math.min(d.width - 1, Math.max(0, hotspot.x)); hotspot.y = Math.min(d.height - 1, Math.max(0, hotspot.y)); } // and create the cursor return tk.createCustomCursor(img, hotspot, "samskivertDnDCursor"); }
From source file:org.mili.core.graphics.GraphicsUtil.java
/** * Calculates relation factor for scaling. * * @param scaleX scale to x.// www .j a v a2 s. c o m * @param scaleY scale to y. * @param i image. * @return relation factor for scaling. */ public static double getRelationFactor(int scaleX, int scaleY, Image i) { double fx = (double) scaleX / i.getWidth(null); double fy = (double) scaleY / i.getHeight(null); return fx < fy ? fx : fy; }
From source file:net.chris54721.infinitycubed.utils.Utils.java
public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) return (BufferedImage) img; BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(img, 0, 0, null);//from w w w . j a v a 2 s .com bGr.dispose(); return bimage; }
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. * /* w w w .j a v a 2 s . c o m*/ * <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:org.jamwiki.utils.ImageUtil.java
/** * Convert a Java Image object to a Java BufferedImage object. *//*w w w. j ava 2s . c o m*/ private static BufferedImage imageToBufferedImage(Image image) throws Exception { BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = bufferedImage.createGraphics(); graphics.drawImage(image, 0, 0, null); graphics.dispose(); return bufferedImage; }
From source file:se.trixon.almond.GraphicsHelper.java
public static Image scaleImage(Image image, Dimension dimension) { Image newImage;// ww w . j av a2 s.co m int height = image.getHeight(null); int width = image.getWidth(null); if (width > height) { newImage = image.getScaledInstance((int) dimension.getWidth(), (int) (dimension.getWidth() * height) / width, Image.SCALE_SMOOTH); } else { newImage = image.getScaledInstance((int) (dimension.getWidth() * width) / height, (int) dimension.getWidth(), Image.SCALE_SMOOTH); } return newImage; }
From source file:BufferedImageConverter.java
static public BufferedImage createBufferedImage(Image imageIn, int imageType, Component comp) { MediaTracker mt = new MediaTracker(comp); mt.addImage(imageIn, 0);//from ww w. ja v a 2s . co m try { mt.waitForID(0); } catch (InterruptedException ie) { } BufferedImage bufferedImageOut = new BufferedImage(imageIn.getWidth(null), imageIn.getHeight(null), imageType); Graphics g = bufferedImageOut.getGraphics(); g.drawImage(imageIn, 0, 0, null); return bufferedImageOut; }