List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:Hexagon.java
public static void drawImage(Graphics g, BufferedImage img, Align align) { drawImage(g, img, align, img.getWidth(), img.getHeight(), null); }
From source file:org.jamwiki.utils.ImageUtil.java
/** * Given a virtualWiki and WikiFIle that correspond to an existing image, * return the WikiImage object. In addition, an optional maxDimension * parameter may be specified, in which case a resized version of the image * may be created./*from w w w. jav a2 s .co m*/ * * @param wikiFile Given a WikiFile object, use it to initialize a * WikiImage object. * @param maxDimension The maximum width or height for the initialized * WikiImage object. Setting this value to 0 or less will cause the * value to be ignored. * @return An initialized WikiImage object. * @throws Exception Thrown if an error occurs while initializing the * WikiImage object. */ public static WikiImage initializeImage(WikiFile wikiFile, int maxDimension) throws Exception { Assert.notNull(wikiFile, "wikiFile may not be null"); WikiImage wikiImage = new WikiImage(wikiFile); BufferedImage imageObject = null; if (maxDimension > 0) { imageObject = ImageUtil.resizeImage(wikiImage, maxDimension); setScaledDimensions(imageObject, wikiImage, maxDimension); } else { File imageFile = new File(Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH), wikiImage.getUrl()); imageObject = ImageUtil.loadImage(imageFile); wikiImage.setWidth(imageObject.getWidth()); wikiImage.setHeight(imageObject.getHeight()); } return wikiImage; }
From source file:iqq.util.ImageUtil.java
/** * ?/*from w w w. jav a2 s . co m*/ * * @param srcFile ? * @param destFile * @param watermarkFile ? * @param watermarkPosition ?? * @param alpha ?? */ public synchronized static void addWatermark(File srcFile, File destFile, InputStream watermarkFile, WatermarkPosition watermarkPosition, int alpha) { //watermarkFile == null || !watermarkFile.exists() || if (!srcFile.exists() || watermarkFile == null || watermarkPosition == null || watermarkPosition == WatermarkPosition.no) { Log.println("addWatermark null"); return; } if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); if (srcBufferedImage == null) { return; } int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); BufferedImage destBufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, srcWidth, srcHeight); graphics2D.drawImage(srcBufferedImage, 0, 0, null); graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha / 100.0F)); BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile); int watermarkImageWidth = watermarkBufferedImage.getWidth(); int watermarkImageHeight = watermarkBufferedImage.getHeight(); int x = srcWidth - watermarkImageWidth; int y = srcHeight - watermarkImageHeight; if (watermarkPosition == WatermarkPosition.topLeft) { x = 0; y = 0; } else if (watermarkPosition == WatermarkPosition.topRight) { x = srcWidth - watermarkImageWidth; y = 0; } else if (watermarkPosition == WatermarkPosition.center) { x = (srcWidth - watermarkImageWidth) / 2; y = (srcHeight - watermarkImageHeight) / 2; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { x = 0; y = srcHeight - watermarkImageHeight; } else if (watermarkPosition == WatermarkPosition.bottomRight) { x = srcWidth - watermarkImageWidth; y = srcHeight - watermarkImageHeight; } graphics2D.drawImage(watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, 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 { String gravity = "SouthEast"; if (watermarkPosition == WatermarkPosition.topLeft) { gravity = "NorthWest"; } else if (watermarkPosition == WatermarkPosition.topRight) { gravity = "NorthEast"; } else if (watermarkPosition == WatermarkPosition.center) { gravity = "Center"; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { gravity = "SouthWest"; } else if (watermarkPosition == WatermarkPosition.bottomRight) { gravity = "SouthEast"; } IMOperation operation = new IMOperation(); operation.gravity(gravity); operation.dissolve(alpha); operation.quality((double) DEST_QUALITY); //operation.addImage(watermarkFile); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { CompositeCmd compositeCmd = new CompositeCmd(true); if (graphicsMagickPath != null) { compositeCmd.setSearchPath(graphicsMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { CompositeCmd compositeCmd = new CompositeCmd(false); if (imageMagickPath != null) { compositeCmd.setSearchPath(imageMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java
/** * Convert a {@link BufferedImage} to a SWT Image. * //from ww w .j a v a 2 s. c o m * {@link "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java?view=co"} * * @param bufferedImage the AWT {@link BufferedImage} * @return the SWT {@link ImageData} */ public static ImageData convertToSWT(BufferedImage bufferedImage) { if (bufferedImage.getColorModel() instanceof DirectColorModel) { DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel(); PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask()); ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int rgb = bufferedImage.getRGB(x, y); int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); data.setPixel(x, y, pixel); // also set the alpha value (ST) data.setAlpha(x, y, colorModel.getAlpha(rgb)); } } return data; } else if (bufferedImage.getColorModel() instanceof IndexColorModel) { IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel(); int size = colorModel.getMapSize(); byte[] reds = new byte[size]; byte[] greens = new byte[size]; byte[] blues = new byte[size]; colorModel.getReds(reds); colorModel.getGreens(greens); colorModel.getBlues(blues); RGB[] rgbs = new RGB[size]; for (int i = 0; i < rgbs.length; i++) { rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); } PaletteData palette = new PaletteData(rgbs); ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); data.transparentPixel = colorModel.getTransparentPixel(); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[1]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { raster.getPixel(x, y, pixelArray); data.setPixel(x, y, pixelArray[0]); } } return data; } return null; }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
private static BufferedImage generateBordersImage(BufferedImage source, int trimmedWidth, int trimmedHeight) throws Wrong9PatchException, IOException { BufferedImage finalBorder = UIUtil.createImage(trimmedWidth + 2, trimmedHeight + 2, BufferedImage.TYPE_INT_ARGB); int cutW = source.getWidth() - 2; int cutH = source.getHeight() - 2; // left border BufferedImage leftBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB); leftBorder.setRGB(0, 0, 1, cutH, source.getRGB(0, 1, 1, cutH, null, 0, 1), 0, 1); verifyBorderImage(leftBorder);//from www . j av a2s.c o m leftBorder = resizeBorder(leftBorder, 1, trimmedHeight); finalBorder.setRGB(0, 1, 1, trimmedHeight, leftBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1); // right border BufferedImage rightBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB); rightBorder.setRGB(0, 0, 1, cutH, source.getRGB(cutW + 1, 1, 1, cutH, null, 0, 1), 0, 1); verifyBorderImage(rightBorder); rightBorder = resizeBorder(rightBorder, 1, trimmedHeight); finalBorder.setRGB(trimmedWidth + 1, 1, 1, trimmedHeight, rightBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1); // top border BufferedImage topBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB); topBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, 0, cutW, 1, null, 0, cutW), 0, cutW); verifyBorderImage(topBorder); topBorder = resizeBorder(topBorder, trimmedWidth, 1); finalBorder.setRGB(1, 0, trimmedWidth, 1, topBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0, trimmedWidth); // bottom border BufferedImage bottomBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB); bottomBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, cutH + 1, cutW, 1, null, 0, cutW), 0, cutW); verifyBorderImage(bottomBorder); bottomBorder = resizeBorder(bottomBorder, trimmedWidth, 1); finalBorder.setRGB(1, trimmedHeight + 1, trimmedWidth, 1, bottomBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0, trimmedWidth); return finalBorder; }
From source file:net.rptools.lib.image.ImageUtil.java
public static int pickBestTransparency(BufferedImage image) { // See if we can short circuit ColorModel colorModel = image.getColorModel(); if (colorModel.getTransparency() == Transparency.OPAQUE) { return Transparency.OPAQUE; }// ww w.j a va 2 s . co m // Get the pixels int width = image.getWidth(); int height = image.getHeight(); // Look for specific pixels boolean foundTransparent = false; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the next pixel int pixel = image.getRGB(x, y); int alpha = (pixel >> 24) & 0xff; // Is there translucency or just pure transparency ? if (alpha > 0 && alpha < 255) { return Transparency.TRANSLUCENT; } if (alpha == 0 && !foundTransparent) { foundTransparent = true; } } } return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE; }
From source file:com.simiacryptus.mindseye.applications.ArtistryUtil.java
/** * Paint circles.//from www . ja v a2 s.c o m * * @param canvas the canvas * @param scale the scale */ public static void paint_Circles(final Tensor canvas, final int scale) { BufferedImage originalImage = canvas.toImage(); BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = (Graphics2D) newImage.getGraphics(); IntStream.range(0, 10000).forEach(i -> { Random random = new Random(); int positionX = random.nextInt(originalImage.getWidth()); int positionY = random.nextInt(originalImage.getHeight()); int width = 1 + random.nextInt(2 * scale); int height = 1 + random.nextInt(2 * scale); DoubleStatistics[] stats = { new DoubleStatistics(), new DoubleStatistics(), new DoubleStatistics() }; canvas.coordStream(false).filter(c -> { int[] coords = c.getCoords(); int x = coords[0]; int y = coords[1]; double relX = Math.pow(1 - 2 * ((double) (x - positionX) / width), 2); double relY = Math.pow(1 - 2 * ((double) (y - positionY) / height), 2); return relX + relY < 1.0; }).forEach(c -> stats[c.getCoords()[2]].accept(canvas.get(c))); graphics.setStroke(new Stroke() { @Override public Shape createStrokedShape(final Shape p) { return null; } }); graphics.setColor(new Color((int) stats[0].getAverage(), (int) stats[1].getAverage(), (int) stats[2].getAverage())); graphics.fillOval(positionX, positionY, width, height); }); canvas.set(Tensor.fromRGB(newImage)); }
From source file:com.t3.image.ImageUtil.java
public static int pickBestTransparency(BufferedImage image) { // See if we can short circuit ColorModel colorModel = image.getColorModel(); if (colorModel.getTransparency() == Transparency.OPAQUE) { return Transparency.OPAQUE; }//ww w .j a v a 2 s . co m // Get the pixels int width = image.getWidth(); int height = image.getHeight(); // Look for specific pixels boolean foundTransparent = false; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the next pixel int pixel = image.getRGB(x, y); int alpha = (pixel >> 24) & 0xff; // Is there translucency or just pure transparency ? if (alpha > 0 && alpha < 255) { return Transparency.TRANSLUCENT; } if (alpha == 0 && !foundTransparent) { foundTransparent = true; } } } return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE; }
From source file:com.t3.image.ImageUtil.java
public static void clearImage(BufferedImage image) { if (image == null) { return;/*from w w w .j a v a2s. co m*/ } Graphics2D g = null; try { g = (Graphics2D) image.getGraphics(); Composite oldComposite = g.getComposite(); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, image.getWidth(), image.getHeight()); g.setComposite(oldComposite); } finally { if (g != null) { g.dispose(); } } }
From source file:com.sun.socialsite.util.ImageUtil.java
/** * Convenience method that returns a scaled instance of the * provided {@code BufferedImage}./*w ww. java2 s . com*/ * * NOTE: Adapted from code at * {@link 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 downscaling 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} */ private static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality) { int type = BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (BufferedImage) img; int w, h; if (higherQuality) { // Use multi-step technique: start with original size, then // scale down in multiple passes with drawImage() // until the target size is reached w = img.getWidth(); h = img.getHeight(); } else { // Use one-step technique: scale directly from original // size to target size with a single drawImage() call w = targetWidth; h = targetHeight; } do { if (higherQuality && w > targetWidth) { w /= 2; if (w < targetWidth) { w = targetWidth; } } if (higherQuality && h > targetHeight) { h /= 2; if (h < targetHeight) { h = targetHeight; } } BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } while (w > targetWidth || h > targetHeight); return ret; }