Example usage for java.awt Image getWidth

List of usage examples for java.awt Image getWidth

Introduction

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

Prototype

public abstract int getWidth(ImageObserver observer);

Source Link

Document

Determines the width of the image.

Usage

From source file:SWTUtils.java

/**
 * Converts an AWT image to SWT.//from  w  ww  . j  a v a2s  .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  w w w . ja  v  a  2s .c  om
 */
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

/**
 * ??(???)//from w ww.  java  2s  . c  o  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:de.brazzy.nikki.model.ImageReader.java

private static int heightForWidth(java.awt.Image img, int width) {
    return (int) (img.getHeight(null) / (double) img.getWidth(null) * width);
}

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

/**
 * Centers an image at in defined image.
 *
 * @param og original image.// ww w.  j a v a  2  s. com
 * @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);
}

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 w  w  w .  ja 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, 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   ww w.  j a  va  2 s.com
        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:org.mili.core.graphics.GraphicsUtil.java

/**
 * Centers an image at relative coordinates x/y in defined image.
 *
 * @param og original image./* w  w w. ja v  a  2  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:eu.planets_project.tb.impl.data.util.ImageThumbnail.java

/**
 * Create a reduced jpeg version of an image. The width/height ratio is
 * preserved.//from   w  ww  .j  av a  2 s .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:org.apache.axis2.jaxws.utility.AttachmentUtils.java

public static boolean compareImages(Image img1, Image img2) {
    if (img1 == null && img2 == null) {
        if (log.isDebugEnabled()) {
            log.debug("Both Image Objects are null, cannot compare Images returning false");
        }//from ww w.j a  va  2 s  .c om
        return false;
    }
    int h1 = img1.getHeight(null);
    int h2 = img2.getHeight(null);
    int w1 = img1.getWidth(null);
    int w2 = img1.getWidth(null);
    if (h1 != h2 || w1 != w2) {
        return false;
    }
    if (img1 instanceof BufferedImage && img2 instanceof BufferedImage) {
        BufferedImage bi1 = (BufferedImage) img1;
        BufferedImage bi2 = (BufferedImage) img2;
        for (int h = 0; h < h1; ++h) {
            for (int w = 0; w < w1; ++w) {
                int Img1Pixel = bi1.getRGB(w, h);
                int Img2Pixel = bi2.getRGB(w, h);
                if (Img1Pixel != Img2Pixel) {
                    return false;
                }
            }
        }
    }
    return true;
}