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:SWTUtils.java

/**
 * Converts an AWT image to SWT.//w  w  w  .j a va 2s  .  co m
 *
 * @param image  the image (<code>null</code> not permitted).
 *
 * @return Image data.
 */
public static ImageData convertAWTImageToSWT(Image image) {
    if (image == null) {
        throw new IllegalArgumentException("Null 'image' argument.");
    }
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    if (w == -1 || h == -1) {
        return null;
    }
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return convertToSWT(bi);
}

From source file:z.tool.util.image.ImageUtil.java

/**
 * //from  www . ja v  a  2 s  .co m
 */
public static void resize(InputStream inputStream, ImageType destType, OutputStream outputStream,
        int maxNewWidth, int maxNewHeight) {
    if (null == inputStream) {
        throw new IllegalArgumentException("inputStream is null");
    }

    if (null == destType) {
        throw new IllegalArgumentException("destType is null");
    }

    if (null == outputStream) {
        throw new IllegalArgumentException("outputStream is null");
    }

    try {
        Image srcImage = ImageIO.read(inputStream);

        // ?
        int srcImageWidth = srcImage.getWidth(null);
        int srcImageHeight = srcImage.getHeight(null);

        if (0 == maxNewWidth || 0 == maxNewHeight
                || (srcImageWidth <= maxNewWidth && srcImageHeight <= maxNewHeight)) {
            maxNewWidth = srcImageWidth;
            maxNewHeight = srcImageHeight;
        } else {
            // 
            // ?
            if (srcImageWidth >= srcImageHeight) {
                // ???
                maxNewHeight = (int) Math.round((srcImageHeight * maxNewWidth * 1.0 / srcImageWidth));
            } else {
                // ???
                maxNewWidth = (int) Math.round((srcImageWidth * maxNewHeight * 1.0 / srcImageHeight));
            }
        }

        BufferedImage distImage = new BufferedImage(maxNewWidth, maxNewHeight, BufferedImage.TYPE_INT_ARGB_PRE);

        Graphics2D graphics2d = distImage.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 
        graphics2d.drawImage(srcImage.getScaledInstance(maxNewWidth, maxNewHeight, Image.SCALE_SMOOTH), 0, 0,
                null);

        // ?
        ImageIO.write(distImage, destType.name(), outputStream);
    } catch (IOException e) {
        LOG.error("method:resize,destType:" + destType + ",maxNewHeight:" + maxNewHeight + ",maxNewWidth:"
                + maxNewWidth + ",errorMsg:" + e.getMessage(), e);
        throw new HumanNeededError(Error.IO_ERROR);
    }
}

From source file:z.tool.util.image.ImageUtil.java

/**
 * ??(???)/*ww w .jav  a2 s  . co  m*/
 */
public static void mergeResource(int bufferedImageType, InputStream inputStream, ImageType destType,
        OutputStream outputStream, int newHeight, int newWidth, Mergeable mergeable, Mergeable... mergeables) {
    if (null == inputStream) {
        throw new IllegalArgumentException("inputStream is null");
    }

    if (null == destType) {
        throw new IllegalArgumentException("destType is null");
    }

    if (null == outputStream) {
        throw new IllegalArgumentException("outputStream is null");
    }

    try {
        Image srcImage = ImageIO.read(inputStream);

        // ?
        int srcImageWidth = srcImage.getWidth(null);
        int srcImageHeight = srcImage.getHeight(null);

        // ????
        if (0 == newWidth || 0 == newHeight) {
            newWidth = srcImageWidth;
            newHeight = srcImageHeight;
        }

        BufferedImage distImage = new BufferedImage(newWidth, newHeight, bufferedImageType);

        // 
        Graphics2D graphics2d = distImage.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        graphics2d.drawImage(srcImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);

        // ??(?)
        mergeable.draw(graphics2d);

        // ??(?)
        if (null != mergeables && mergeables.length > 0) {
            for (Mergeable d : mergeables) {
                d.draw(graphics2d);
            }
        }

        // ?
        ImageIO.write(distImage, destType.name(), outputStream);
    } catch (IOException e) {
        LOG.error("method:mergeResource,destType:" + destType + ",newHeight:" + newHeight + ",newWidth:"
                + newWidth + ",errorMsg:" + e.getMessage(), e);
        throw new HumanNeededError(Error.IO_ERROR);
    }
}

From source file:eu.planets_project.tb.impl.data.util.ImageThumbnail.java

/**
 * Create a reduced jpeg version of an image. The width/height ratio is
 * preserved./* ww w .  j a v a  2s  . c  o m*/
 * 
 * @param data
 *            raw data of the image
 * @param thumbWidth
 *            maximum width of the reduced image
 * @param thumbHeight
 *            maximum heigth of the reduced image
 * @param quality
 *            jpeg quality of the reduced image
 * @param out
 *            produce a reduced jpeg image if the image represented by data
 *            is bigger than the maximum dimensions of the reduced image,
 *            otherwise data is written to this stream
 */
public static void createThumb(byte[] data, int thumbWidth, int thumbHeight, OutputStream out)
        throws Exception {
    // NOTE that this support JPEG, PNG or GIF only.
    Image image = Toolkit.getDefaultToolkit().createImage(data);
    MediaTracker mediaTracker = new MediaTracker(new Frame());
    int trackID = 0;
    mediaTracker.addImage(image, trackID);
    mediaTracker.waitForID(trackID);
    if (image.getWidth(null) <= thumbWidth && image.getHeight(null) <= thumbHeight)
        out.write(data);
    else
        createThumb(image, thumbWidth, thumbHeight, out);
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nonnull
public static Image makeBadgedRightBottom(@Nonnull final Image base, @Nonnull final Image badge) {
    final int width = Math.max(base.getWidth(null), badge.getWidth(null));
    final int height = Math.max(base.getHeight(null), badge.getHeight(null));
    final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = result.getGraphics();
    try {/*from ww  w  . j  a  va 2s  . c  om*/
        gfx.drawImage(base, (width - base.getWidth(null)) / 2, (height - base.getHeight(null)) / 2, null);
        gfx.drawImage(badge, width - badge.getWidth(null) - 1, height - badge.getHeight(null) - 1, null);
    } finally {
        gfx.dispose();
    }
    return result;
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nonnull
public static Image makeBadgedRightTop(@Nonnull final Image base, @Nonnull final Image badge) {
    final int width = Math.max(base.getWidth(null), badge.getWidth(null));
    final int height = Math.max(base.getHeight(null), badge.getHeight(null));
    final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = result.getGraphics();
    try {/*from  w ww. j  a  v a2 s .co  m*/
        gfx.drawImage(base, (width - base.getWidth(null)) / 2, (height - base.getHeight(null)) / 2, null);
        gfx.drawImage(badge, width - badge.getWidth(null) - 1, 1, null);
    } finally {
        gfx.dispose();
    }
    return result;
}

From source file:net.sf.webphotos.tools.Thumbnail.java

private static Image estampar(Image im) {
    try {/*  w  w w  .j a  v a 2s  .  com*/
        Image temp = new ImageIcon(im).getImage();

        BufferedImage buf = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g2 = (Graphics2D) buf.getGraphics();

        g2.drawImage(temp, 0, 0, null);
        g2.setBackground(Color.BLUE);

        Dimension dimensaoFoto = new Dimension(im.getWidth(null), im.getHeight(null));

        // aplicar texto
        if (texto != null) {
            Util.out.println("aplicando texto " + texto);

            Font fonte = new Font(txFamilia, txEstilo, txTamanho);
            g2.setFont(fonte);
            FontMetrics fm = g2.getFontMetrics(fonte);
            Dimension dimensaoTexto = new Dimension(fm.stringWidth(texto), fm.getHeight());
            Point posTexto = calculaPosicao(dimensaoFoto, dimensaoTexto, txMargem, txPosicao);

            g2.setPaint(txCorFundo);
            g2.drawString(texto, (int) posTexto.getX() + 1, (int) posTexto.getY() + 1 + fm.getHeight());
            g2.setPaint(txCorFrente);
            g2.drawString(texto, (int) posTexto.getX(), (int) posTexto.getY() + fm.getHeight());
        }

        // aplicar marca dagua
        if (marcadagua != null) {
            Image marca = new ImageIcon(marcadagua).getImage();
            int rule = AlphaComposite.SRC_OVER;
            float alpha = (float) mdTransparencia / 100;
            Point pos = calculaPosicao(dimensaoFoto, new Dimension(marca.getWidth(null), marca.getHeight(null)),
                    mdMargem, mdPosicao);

            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.drawImage(marca, (int) pos.getX(), (int) pos.getY(), null);
        }

        g2.dispose();
        //return (Image) buf;
        return Toolkit.getDefaultToolkit().createImage(buf.getSource());
    } catch (Exception e) {
        Util.err.println("[Thumbnail.estampar]/ERRO: Inesperado - " + e.getMessage());
        e.printStackTrace(Util.err);
        return im;
    }
}

From source file:com.baobao121.baby.common.SimpleUploaderServlet.java

public static void changeDimension(InputStream bis, FileOutputStream desc, int w, int h) throws IOException {
    Image src = javax.imageio.ImageIO.read(bis); // Image
    int width = src.getWidth(null); // ?
    int height = src.getHeight(null); // ?
    if (width <= w && height <= h) {
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(src, 0, 0, width, height, null); // ??
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(desc);
        encoder.encode(tag);//from   www  .j a v  a  2 s .c o  m
        desc.flush();
        desc.close();
        bis.close();
    } else {
        if (width != height) {
            if (w * height < h * width) {
                h = (int) (height * w / width);
            } else {
                w = (int) (width * h / height);
            }
        }
        BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(src, 0, 0, w, h, null); // ??
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(desc);
        encoder.encode(tag);
        desc.flush();
        desc.close();
        bis.close();
    } // JPEG?
}

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

/**
 * Centers an image at relative coordinates x/y in defined image.
 *
 * @param og original image.// w  w  w .  j ava2  s  . c  o  m
 * @param i image to point.
 * @param x coordinate.
 * @param y coordinate.
 * @return new image included image to point.
 */
public static Image pointCenterImage(Image og, Image i, int x, int y) {
    Validate.notNull(og, "original image cannot be null!");
    Validate.notNull(i, "image to center cannot be null!");
    int x0 = x - (i.getWidth(null) / 2);
    if (x0 < 0) {
        x0 = 0;
    }
    int y0 = y - (i.getHeight(null) / 2);
    if (y0 < 0) {
        y0 = 0;
    }
    return pointImage(og, i, x0, y0);
}

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

/**
 * Centers an image at in defined image.
 *
 * @param og original image./*from   w  w  w . ja va  2s . c o m*/
 * @param i image to point.
 * @return new image included image to point.
 */
public static Image centerImage(Image og, Image i) {
    Validate.notNull(og, "original image cannot be null!");
    Validate.notNull(i, "image to center cannot be null!");
    int x = (og.getWidth(null) - i.getWidth(null)) / 2;
    if (x < 0) {
        x = 0;
    }
    int y = (og.getHeight(null) - i.getHeight(null)) / 2;
    if (y < 0) {
        y = 0;
    }
    return pointImage(og, i, x, y);
}