Example usage for java.awt Image getHeight

List of usage examples for java.awt Image getHeight

Introduction

In this page you can find the example usage for java.awt Image getHeight.

Prototype

public abstract int getHeight(ImageObserver observer);

Source Link

Document

Determines the height of the image.

Usage

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static BufferedImage toBufferedImage(Image image) throws InterruptedException {

    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    image = new ImageIcon(image).getImage();
    int type = hasAlpha(image) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
    BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);/*  w w  w . j a  v  a2  s . c  o m*/
    g.dispose();
    return bimage;
}

From source file:org.pentaho.reporting.engine.classic.core.layout.output.RenderUtility.java

public static Image scaleImage(final Image img, final int targetWidth, final int targetHeight,
        final Object hintValue, final boolean higherQuality) {
    final int type = BufferedImage.TYPE_INT_ARGB;

    Image ret = img;
    int w;// w  ww .j  a v a  2 s  . co  m
    int h;
    do {

        if (higherQuality) {
            final int imageWidth = ret.getWidth(null);
            final int imageHeight = ret.getHeight(null);
            if (imageWidth < targetWidth) {
                // This is a up-scale operation.
                w = Math.min(imageWidth << 1, targetWidth);
            } else if (imageWidth > targetWidth) {
                // downscale
                w = Math.max(imageWidth >> 1, targetWidth);
            } else {
                w = imageWidth;
            }

            if (imageHeight < targetHeight) {
                // This is a up-scale operation.
                h = Math.min(imageHeight << 1, targetHeight);
            } else if (imageHeight > targetHeight) {
                // downscale
                h = Math.max(imageHeight >> 1, targetHeight);
            } else {
                h = imageHeight;
            }
        } else {
            w = targetWidth;
            h = targetHeight;
        }

        final BufferedImage tmp = new BufferedImage(w, h, type);
        final Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hintValue);
        // this one scales the image ..
        if (ret instanceof BufferedImage) {
            if (g2.drawImage(ret, 0, 0, w, h, null) == false) {
                logger.debug("Failed to scale the image. This should not happen.");
            }
        } else {
            final WaitingImageObserver obs = new WaitingImageObserver(ret);
            while (g2.drawImage(ret, 0, 0, w, h, null) == false) {
                obs.waitImageLoaded();
                if (obs.isError()) {
                    logger.warn("Error while loading the image during the rendering.");
                    break;
                }
            }

        }
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {

        private int shift = 0xFF000000;

        public int rgbToMakeTransparent = color.getRGB() | shift;

        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | shift) == rgbToMakeTransparent) {
                return 0x00FFFFFF & rgb;
            }/*from w w  w. ja  v a2 s .c  o  m*/
            return rgb;
        }
    };
    Image newImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter));
    BufferedImage bufferedImage = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(newImage, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}

From source file:org.paxle.tools.icon.impl.IconTool.java

public static byte[] toBytes(Image icon) {
    try {//from w  w w .  ja v  a  2 s  .  c o  m
        BufferedImage bi = null;
        if (icon instanceof BufferedImage) {
            bi = (BufferedImage) icon;
        } else {

            int width = icon.getWidth(null);
            int height = icon.getHeight(null);
            if (width <= 0)
                width = (height > 0) ? height : 32;
            if (height <= 0)
                height = (width > 0) ? width : 32;

            bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            bi.createGraphics().drawImage(icon, 0, 0, width, height, null);
        }

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ImageIO.write(bi, "png", bout);
        bout.flush();
        bout.close();

        return bout.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:smanilov.mandelbrot.compute.Computer.java

/**
 * Returns a crop view of the Mandelbrot set.
 * @param drawing the image to draw on./* w w  w.ja v a 2 s  .c om*/
 * @param foregroundColor the color to use when drawing Mandelbrot pixels.
 * @param backgroundColor the color to use when drawing non-Mandelbrot pixels.
 * @param drawingLock the lock to use for locking the image.
 * @param center Center of the crop, in units of the complex plane, w.r.t. the origin.
 * @param scale log2(pixels) per unit in the complex plane. 
 */
public static void drawMandelbrotCrop(final Image drawing, final Color foregroundColor,
        final Color backgroundColor, final ReentrantLock drawingLock, final int scale, final Point2D center) {
    initDrawStep(drawing);

    Thread pt = createProducerThread(drawing.getWidth(null), drawing.getHeight(null));
    pt.start();

    for (int i = 0; i < shaders; ++i) {
        Thread sh = createShaderThread(drawing, foregroundColor, backgroundColor, drawingLock, scale, center);
        sh.start();
    }
}

From source file:org.mili.core.graphics.GraphicsUtil.java

/**
 * Scales an image./*from w  w w  .ja v a  2s .c  om*/
 *
 * @param i image.
 * @param scaleX scale to x >= 0.
 * @param scaleY scale to y >= 0.
 * @return scaled image.
 */
public static Image scaleImage(Image i, int scaleX, int scaleY) {
    Validate.notNull(i, "image cannot be null!");
    Validate.isTrue(scaleX >= 0, "scaleX cannot be < 0!");
    Validate.isTrue(scaleY >= 0, "scaleY cannot be < 0!");
    if (scaleX == 0 && scaleY == 0) {
        return i;
    }
    double f = getRelationFactor(scaleX, scaleY, i);
    int x = (int) (i.getWidth(null) * f);
    int y = (int) (i.getHeight(null) * f);
    i = i.getScaledInstance(x, y, Image.SCALE_SMOOTH);
    ImageIcon ii = new ImageIcon(i);
    i = ii.getImage();
    if (i instanceof ToolkitImage) {
        ToolkitImage ti = (ToolkitImage) i;
        i = ti.getBufferedImage();
    }
    return i;
}

From source file:smanilov.mandelbrot.compute.Computer.java

/**
 * Resets the state of the class for a new draw step.
 * @param drawing Used to extract the number of pixels to draw.
 *//*from   w ww  .ja v  a 2s .  co  m*/
private static void initDrawStep(Image drawing) {
    ++currentDrawingId;
    if (toDoList != null) {
        queueLock.lock();
        toDoList.clear();
        queueLock.unlock();
    }
    toDoList = new LinkedBlockingQueue<Point>();
    active = true;

    maxDrawn = drawing.getWidth(null) * drawing.getHeight(null);
    nDrawn = 0;

    startTime = System.currentTimeMillis();
}

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 {//w  w w .ja  va2 s .  co 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:zz.pseas.ghost.utils.BrowserUtil.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from w w w .ja  v a  2  s .  com*/
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        int transparency = Transparency.OPAQUE;
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

From source file:QuestionGUI.java

/**
 * Converts a given Image into a BufferedImage
 *
 * CREDIT/*from   w  w  w . ja v a 2 s. co  m*/
 * https://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/util/ImageTool.java#31
 *
 * @param img The Image to be converted
 * @return The converted BufferedImage
 */
private static BufferedImage toBufferedImage(Image img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }

    // Create a buffered image with transparency
    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;
}