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.chinarewards.gwt.license.util.ImageDisposeUtil.java

/**
 * @param imgsrc   /*  www . ja v  a2s .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:se.trixon.almond.GraphicsHelper.java

public static Image scaleImage(Image image, Dimension dimension) {
    Image newImage;// w  w  w.ja v  a  2s .  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:Main.java

/**
 * Create a custom cursor out of the specified image, with the specified hotspot.
 *///from ww  w .j av a 2  s  . c  o 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: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. java 2s . c  o m*/
    bGr.dispose();
    return bimage;
}

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

/**
 * Calculates relation factor for scaling.
 *
 * @param scaleX scale to x.//from  w  w  w.  jav a 2 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:org.jamwiki.utils.ImageUtil.java

/**
 * Convert a Java Image object to a Java BufferedImage object.
 */// w w w . ja  va2 s  .c  om
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: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. ja  v a2 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.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");
        }//  w  ww. ja v a  2 s .  co m
        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;
}

From source file:BufferedImageConverter.java

static public BufferedImage createBufferedImage(Image imageIn, int imageType, Component comp) {
    MediaTracker mt = new MediaTracker(comp);
    mt.addImage(imageIn, 0);//w  ww  .  j  a va2 s.  c o 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;
}

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);
}