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.piaoyou.util.ImageUtil.java

/**
 * This method returns a fit-sized image for a source image,
 * this method retains the ratio of the source image
 *///from w  w w.  ja  v  a 2 s. co m
public static Image getFitSizeImage(Image srcImage, int fitWidth, int fitHeight) throws IOException {
    if ((fitWidth < 100) || (fitHeight < 100))
        throw new IllegalArgumentException("Cannot accept values < 100");

    int srcWidth = srcImage.getWidth(null);//xem lai cho nay vi neu dung BufferedImage thi khong can null
    int srcHeight = srcImage.getHeight(null);
    //log.trace("src w = " + srcWidth + " h = " + srcHeight);

    // don't need any transforms
    if ((srcWidth == fitWidth) && (srcHeight == fitHeight))
        return srcImage;

    int newWidth = srcWidth;
    int newHeight = srcHeight;

    double fitRatio = (double) fitWidth / fitHeight;
    double srcRatio = (double) srcWidth / srcHeight;
    if (srcRatio > fitRatio) {// must cut the width of the source image
        newWidth = (int) (srcHeight * fitRatio);
    } else {// must cut the height of the source image
        newHeight = (int) (srcWidth / fitRatio);
    }
    //log.trace("new w = " + newWidth + " h = " + newHeight);

    ImageFilter cropFilter = new CropImageFilter((srcWidth - newWidth) / 2, (srcHeight - newHeight) / 2,
            newWidth, newHeight);
    ImageProducer cropProd = new FilteredImageSource(srcImage.getSource(), cropFilter);
    Image cropImage = Toolkit.getDefaultToolkit().createImage(cropProd);

    Image retImage = new ImageIcon(getScaledInstance(cropImage, fitWidth, fitHeight)).getImage();

    return retImage;
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.InstitutionConfigDlg.java

public static BufferedImage toBufferedImage(final Image img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }/*  w ww  . j a v a2 s . c o  m*/

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

From source file:net.rptools.lib.image.ImageUtil.java

/**
 * Converts a byte array into an {@link Image} instance.
 * //from ww  w .  j  a  va  2  s . c  om
 * @param imageBytes
 *            bytes to convert
 * @return
 * @throws IOException
 */
public static Image bytesToImage(byte[] imageBytes) throws IOException {
    if (imageBytes == null) {
        throw new IOException("Could not load image - no data provided");
    }
    boolean interrupted = false;
    Throwable exception = null;
    Image image = null;
    image = Toolkit.getDefaultToolkit().createImage(imageBytes);
    MediaTracker tracker = new MediaTracker(observer);
    tracker.addImage(image, 0);
    do {
        try {
            interrupted = false;
            tracker.waitForID(0); // This is the only method that throws an exception
        } catch (InterruptedException t) {
            interrupted = true;
            continue;
        } catch (Throwable t) {
            exception = t;
        }
    } while (interrupted);
    if (image == null || exception != null || image.getWidth(null) <= 0 || image.getHeight(null) <= 0) {
        // Try the newer way (although it pretty much sucks rocks)
        image = ImageIO.read(new ByteArrayInputStream(imageBytes));
    }
    if (image == null) {
        throw new IOException("Could not load image", exception);
    }
    return image;
}

From source file:ImageTest.java

public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);//  www .j ava 2s  . c o m
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
}

From source file:GrayImage.java

public void paint(Graphics g) {
    Image myImage = new ImageIcon("yourImage.png").getImage();
    BufferedImage bufferedImage = new BufferedImage(myImage.getHeight(this), myImage.getWidth(this),
            BufferedImage.TYPE_BYTE_GRAY);

    Graphics gi = bufferedImage.getGraphics();
    gi.drawImage(myImage, 0, 0, null);//from ww  w  .  j  av  a  2 s  . c om
    gi.dispose();

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(bufferedImage, null, 0, 0);
}

From source file:com.piaoyou.util.ImageUtil.java

/**
 * This method write the image to a stream.
 * It auto detect the image is Image or BufferedImage.
 * This method close the output stream before it return.
 *
 * @param image Image/*w ww  .j  a  va2 s.  co m*/
 * @param outputStream OutputStream
 * @throws IOException
 */
public static void writeJpegImage_Sun(Image image, OutputStream outputStream) throws IOException {

    if (outputStream == null) {
        return;
    }

    try {
        BufferedImage bufferedImage = null;
        if (image instanceof BufferedImage) {
            bufferedImage = (BufferedImage) image;
        } else {
            // 30% cpu resource
            bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);

            // 7.5 cpu
            Graphics2D g = bufferedImage.createGraphics();

            // 50% cpu
            g.drawImage(image, 0, 0, null);
            g.dispose(); // free resource
        }

        // write it to disk
        // 12% cpu
        //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
        //encoder.encode(bufferedImage);
        ImageIO.write(bufferedImage, "jpeg", outputStream);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

From source file:MyCanvas.java

public void paint(Graphics g) {
    Image img1 = Toolkit.getDefaultToolkit().getImage("yourFile.gif");

    int width = img1.getWidth(this);
    int height = img1.getHeight(this);

    int scale = 2;
    int w = scale * width;
    int h = scale * height;
    // explicitly specify width (w) and height (h)
    g.drawImage(img1, 10, 10, (int) w, (int) h, this);

}

From source file:ImageUtil.java

/**
 * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm
 *//*from w  w w.  j  a  v  a 2  s  .c om*/
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;

        if (hasAlpha == true)
            transparency = Transparency.BITMASK;

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    } //No screen

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;

        if (hasAlpha == true) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        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:ImageFlip.java

public void paint(Graphics g) {
    Image myImage = new ImageIcon("yourImage.jpg").getImage();
    BufferedImage bufferedImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) g;

    Graphics gb = bufferedImage.getGraphics();
    gb.drawImage(myImage, 0, 0, null);/* w ww  . j  a v a2 s . c om*/
    gb.dispose();

    AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-myImage.getWidth(null), 0);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);

    g2d.drawImage(myImage, 10, 10, null);
    g2d.drawImage(bufferedImage, null, 300, 10);
}

From source file:IconLine.java

public void setImage(int index, Image image) {
    vImages.set(index, image);
    iHeight = image.getHeight(null);
}