Example usage for java.awt.image BufferedImage getHeight

List of usage examples for java.awt.image BufferedImage getHeight

Introduction

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

Prototype

public int getHeight(ImageObserver observer) 

Source Link

Document

Returns the height of the BufferedImage .

Usage

From source file:ImageUtils.java

/**
 * Resizes an image.// www  .  j a v  a 2 s.  c  o m
 * 
 * @param image
 *            The image to resize
 * @param maxWidth
 *            The image's max width
 * @param maxHeight
 *            The image's max height
 * @return A resized <code>BufferedImage</code>
 * @param type
 *            int
 */
public static BufferedImage resizeImage(BufferedImage image, int type, int maxWidth, int maxHeight) {
    Dimension largestDimension = new Dimension(maxWidth, maxHeight);

    // Original size
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);

    float aspectRatio = (float) imageWidth / imageHeight;

    if (imageWidth > maxWidth || imageHeight > maxHeight) {
        if ((float) largestDimension.width / largestDimension.height > aspectRatio) {
            largestDimension.width = (int) Math.ceil(largestDimension.height * aspectRatio);
        } else {
            largestDimension.height = (int) Math.ceil(largestDimension.width / aspectRatio);
        }

        imageWidth = largestDimension.width;
        imageHeight = largestDimension.height;
    }

    return createHeadlessSmoothBufferedImage(image, type, imageWidth, imageHeight);
}

From source file:de.mpg.imeji.logic.storage.util.ImageUtils.java

/**
 * Scale a {@link BufferedImage} to new size. Is faster than the basic {@link ImageUtils}.scaleImage method, has the
 * same quality. If it is a thumbnail, cut the images to fit into the raster
 * //from www  .ja  va 2  s  .c om
 * @param image original image
 * @param size the size to be resized to
 * @param resolution the type of the image. Might be thumb or web
 * @return the resized images
 * @throws Exception
 */
public static BufferedImage scaleImageFast(BufferedImage image, int size, FileResolution resolution)
        throws Exception {
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    BufferedImage newImg = null;
    Image rescaledImage;
    if (width > height) {
        if (FileResolution.THUMBNAIL.equals(resolution)) {
            newImg = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
            Graphics g1 = newImg.createGraphics();
            g1.drawImage(image, (height - width) / 2, 0, null);
            if (height > size)
                rescaledImage = getScaledInstance(newImg, size, size,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
            else
                rescaledImage = newImg;
        } else
            rescaledImage = getScaledInstance(image, size, height * size / width,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
    } else {
        if (FileResolution.THUMBNAIL.equals(resolution)) {
            newImg = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
            Graphics g1 = newImg.createGraphics();
            g1.drawImage(image, 0, (width - height) / 2, null);
            if (width > size)
                rescaledImage = getScaledInstance(newImg, size, size,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
            else
                rescaledImage = newImg;
        } else
            rescaledImage = getScaledInstance(image, width * size / height, size,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
    }
    BufferedImage rescaledBufferedImage = new BufferedImage(rescaledImage.getWidth(null),
            rescaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics g2 = rescaledBufferedImage.getGraphics();
    g2.drawImage(rescaledImage, 0, 0, null);
    return rescaledBufferedImage;
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

public static BufferedImage scale(BufferedImage bi, int width, int height) {
    BufferedImage bi2;/*from www . j a v  a  2  s  .co  m*/
    int scaleWidth = bi.getWidth(null);
    int scaleHeight = bi.getHeight(null);
    double scaleX = (double) width / scaleWidth;
    double scaleY = (double) height / scaleHeight;
    double scale = Math.min(scaleX, scaleY);
    scaleWidth = (int) ((double) scaleWidth * scale);
    scaleHeight = (int) ((double) scaleHeight * scale);
    Image scaledImage;
    if (HEADLESS) {
        // create a new buffered image, don't rely on a local graphics system (headless mode)
        final int type;
        if (bi.getType() != BufferedImage.TYPE_CUSTOM) {
            type = bi.getType();
        } else if (bi.getAlphaRaster() != null) {
            // alpha channel available
            type = BufferedImage.TYPE_INT_ARGB;
        } else {
            type = BufferedImage.TYPE_INT_RGB;
        }
        bi2 = new BufferedImage(scaleWidth, scaleHeight, type);
    } else {
        GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration();
        bi2 = gc.createCompatibleImage(scaleWidth, scaleHeight, bi.getTransparency());
    }
    Graphics2D g = bi2.createGraphics();
    if (scale < 0.3 && Math.max(scaleWidth, scaleHeight) < 500) {
        scaledImage = bi.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);
        new ImageIcon(scaledImage).getImage();
        g.drawImage(scaledImage, 0, 0, scaleWidth, scaleHeight, null);
    } else {
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.drawImage(bi, 0, 0, scaleWidth, scaleHeight, null);
    }
    g.dispose();
    return bi2;
}

From source file:com.t3.persistence.PersistenceUtil.java

static public void saveCampaignThumbnail(String fileName) {
    BufferedImage screen = TabletopTool.takeMapScreenShot(new PlayerView(TabletopTool.getPlayer().getRole()));
    if (screen == null)
        return;/*from w  w w .j a  va 2s .c o m*/

    Dimension imgSize = new Dimension(screen.getWidth(null), screen.getHeight(null));
    SwingUtil.constrainTo(imgSize, 200, 200);

    BufferedImage thumb = new BufferedImage(imgSize.width, imgSize.height, BufferedImage.TYPE_INT_BGR);
    Graphics2D g2d = thumb.createGraphics();
    g2d.drawImage(screen, 0, 0, imgSize.width, imgSize.height, null);
    g2d.dispose();

    File thumbFile = getCampaignThumbnailFile(fileName);

    try {
        ImageIO.write(thumb, "jpg", thumbFile);
    } catch (IOException ioe) {
        TabletopTool.showError("msg.error.failedSaveCampaignPreview", ioe);
    }
}

From source file:BlurredImage.java

public void paint(Graphics g) {
    try {/*from   w ww  .  ja v  a  2s .c  o m*/
        BufferedImage myImage = ImageIO.read(this.getClass().getResource("redrock.png"));

        BufferedImage filteredImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null),
                BufferedImage.TYPE_BYTE_GRAY);

        Graphics g1 = filteredImage.getGraphics();
        g1.drawImage(myImage, 400, 200, null);

        float[] blurKernel = { 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f };

        BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel));
        myImage = blur.filter(myImage, null);
        g1.dispose();

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(myImage, null, 3, 3);
    } catch (Exception e) {

    }
}

From source file:net.rptools.maptool.util.PersistenceUtil.java

static public void saveCampaignThumbnail(String fileName) {
    BufferedImage screen = MapTool.takeMapScreenShot(new PlayerView(MapTool.getPlayer().getRole()));
    if (screen == null)
        return;/*from w w w. j  av a2  s.c  om*/

    Dimension imgSize = new Dimension(screen.getWidth(null), screen.getHeight(null));
    SwingUtil.constrainTo(imgSize, 200, 200);

    BufferedImage thumb = new BufferedImage(imgSize.width, imgSize.height, BufferedImage.TYPE_INT_BGR);
    Graphics2D g2d = thumb.createGraphics();
    g2d.drawImage(screen, 0, 0, imgSize.width, imgSize.height, null);
    g2d.dispose();

    File thumbFile = getCampaignThumbnailFile(fileName);

    try {
        ImageIO.write(thumb, "jpg", thumbFile);
    } catch (IOException ioe) {
        MapTool.showError("msg.error.failedSaveCampaignPreview", ioe);
    }
}

From source file:com.fusesource.forge.jmstest.persistence.rrd.RrdGraphPostProcessor.java

private void createThumbnail(BufferedImage image, String name, int thumbWidth, int thumbHeight) {

    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;

    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {// ww  w.j a  v  a  2s. co  m
        thumbWidth = (int) (thumbHeight * imageRatio);
    }

    BufferedImage thumbImage = new BufferedImage(thumbWidth,

            thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    File thumbFile = new File(getWorkDir().getAbsolutePath() + "/" + name + "-thumb.png");

    try {
        ImageIO.write(thumbImage, "PNG", thumbFile);
    } catch (IOException ioe) {
        log().error("Error creating thumbnail for: " + name);
    }
}

From source file:dk.dma.msinm.web.wms.WmsProxyServlet.java

/**
 * Masks out white colour//from   www  .j a v a  2  s.  co m
 * @param image the image to mask out
 * @return the resulting image
 */
private BufferedImage transformWhiteToTransparent(BufferedImage image) {

    BufferedImage dest = image;
    if (image.getType() != BufferedImage.TYPE_INT_ARGB) {
        dest = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = dest.createGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();

        image.flush();
    }

    // Mask out the white pixels
    final int width = image.getWidth();
    int[] imgData = new int[width];

    for (int y = 0; y < dest.getHeight(); y++) {
        // fetch a line of data from each image
        dest.getRGB(0, y, width, 1, imgData, 0, 1);
        // apply the mask
        for (int x = 0; x < width; x++) {
            for (Color col : MASKED_COLORS) {
                int colDist = Math.abs(col.getRed() - (imgData[x] >> 16 & 0x000000FF))
                        + Math.abs(col.getGreen() - (imgData[x] >> 8 & 0x000000FF))
                        + Math.abs(col.getBlue() - (imgData[x] & 0x000000FF));
                if (colDist <= COLOR_DIST) {
                    imgData[x] = 0x00FFFFFF & imgData[x];
                }
            }
        }
        // replace the data
        dest.setRGB(0, y, width, 1, imgData, 0, 1);
    }
    return dest;
}

From source file:WaitingImageObserver.java

/**
 * The workerthread. Simply draws the image to a BufferedImage's
 * Graphics-Object and waits for the AWT to load the image.
 *///  w ww  .  jav a2s  . co  m
public synchronized void waitImageLoaded() {

    if (this.lock == false) {
        return;
    }

    final BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    final Graphics g = img.getGraphics();

    while (this.lock) {
        if (g.drawImage(this.image, 0, 0, img.getWidth(this), img.getHeight(this), this)) {
            return;
        }

        try {
            wait(500);
        } catch (InterruptedException e) {
            System.out.println("WaitingImageObserver.waitImageLoaded(): InterruptedException thrown" + e);
        }
    }
}