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:no.geosoft.cc.io.GifEncoder.java

/**
 * Constructing a GIF encoder./*from w  w  w  .  j a v a  2s . c  o  m*/
 *
 * @param image  The image to encode. The image must be
 *               completely loaded.
 * @throws AWTException  If memory is exhausted or image contains
 *                       more than 256 colors.
 */
public GifEncoder(Image image) throws AWTException {
    imageWidth_ = (short) image.getWidth(null);
    imageHeight_ = (short) image.getHeight(null);

    int values[] = new int[imageWidth_ * imageHeight_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, imageWidth_, imageHeight_, values, 0, imageWidth_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException exception) {
    }

    byte[][] r = new byte[imageWidth_][imageHeight_];
    byte[][] g = new byte[imageWidth_][imageHeight_];
    byte[][] b = new byte[imageWidth_][imageHeight_];

    int index = 0;

    for (int y = 0; y < imageHeight_; y++) {
        for (int x = 0; x < imageWidth_; x++, index++) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index] >> 0) & 0xFF);
        }
    }

    toIndexColor(r, g, b);
}

From source file:edu.uci.ics.jung.visualization.decorators.VertexIconShapeTransformer.java

/**
 * get the shape from the image. If not available, get
 * the shape from the delegate VertexShapeFunction
 *//*from   w w w.  j a  va2  s  .c  om*/
public Shape transform(V v) {
    Icon icon = iconMap.get(v);
    if (icon != null && icon instanceof ImageIcon) {
        Image image = ((ImageIcon) icon).getImage();
        Shape shape = (Shape) shapeMap.get(image);
        if (shape == null) {
            shape = FourPassImageShaper.getShape(image, 30);
            if (shape.getBounds().getWidth() > 0 && shape.getBounds().getHeight() > 0) {
                // don't cache a zero-sized shape, wait for the image
                // to be ready
                int width = image.getWidth(null);
                int height = image.getHeight(null);
                AffineTransform transform = AffineTransform.getTranslateInstance(-width / 2, -height / 2);
                shape = transform.createTransformedShape(shape);
                shapeMap.put(image, shape);
            }
        }
        return shape;
    } else {
        return delegate.transform(v);
    }
}

From source file:pl.datamatica.traccar.api.providers.ImageProvider.java

public byte[] getMarker(String name) throws IOException {
    if (!markerCache.containsKey(name)) {
        Image icon = getImage(name + ".png");
        if (icon == null)
            return null;
        float l = 30f / 141, t = 28f / 189, r = 110f / 141, b = 108f / 189;
        int w = emptyMarker.getWidth(null), h = emptyMarker.getHeight(null);
        int tw = (int) Math.round((r - l) * w), th = (int) Math.round((b - t) * h);

        //https://stackoverflow.com/a/7951324
        int wi = icon.getWidth(null), hi = icon.getHeight(null);
        while (wi >= 2 * tw || hi >= 2 * th) {
            if (wi >= 2 * tw)
                wi /= 2;//from w  w  w.  j a  v a 2 s . c o m
            if (hi >= 2 * th)
                hi /= 2;
            BufferedImage tmp = new BufferedImage(wi, hi, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = tmp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(icon, 0, 0, wi, hi, null);
            g2.dispose();
            icon = tmp;
        }

        BufferedImage marker = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = marker.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(emptyMarker, 0, 0, null);
        g.drawImage(icon, (int) Math.round(l * w), (int) Math.round(t * h), tw, th, null);
        g.dispose();

        ByteArrayOutputStream boss = new ByteArrayOutputStream();
        ImageIO.write((RenderedImage) marker, "png", boss);
        markerCache.put(name, boss.toByteArray());
    }
    return markerCache.get(name);
}

From source file:gg.msn.ui.panel.MainPanel.java

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    super.paintComponent(g2d);
    g2d.setRenderingHints(/*w ww.  ja va  2  s .c  o m*/
            new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR));

    try {

        ImageIcon icon = ThemeManager.getTheme().get(ThemeManager.MAIN_BACKGROUND);

        if (icon != null) {
            Image image = icon.getImage();
            g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        } else {
            log.warn("image " + icon.getDescription() + " not Found");
        }

        icon = ThemeManager.getTheme().get(ThemeManager.MAIN_IMAGE);

        if (icon != null) {

            Image cartel = icon.getImage();
            g2d.drawImage(cartel, 0 - cartel.getWidth(this) / 10, getHeight() - cartel.getHeight(this), this);
        } else {
            log.warn("image " + icon.getDescription() + " not Found");
        }
    } catch (Exception e) {
        //            log.error(e);
    }
}

From source file:GifEncoder.java

/** Constructs a new GifEncoder using an 8-bit AWT Image.
 The image is assumed to be fully loaded. */
public GifEncoder(Image img) {
    width = img.getWidth(null);/* w ww.j  ava  2s. com*/
    height = img.getHeight(null);
    pixels = new byte[width * height];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println(e);
    }
    ColorModel cm = pg.getColorModel();
    if (cm instanceof IndexColorModel) {
        pixels = (byte[]) (pg.getPixels());
        // hpm
        IndexColorModel icm = (IndexColorModel) cm;
        setTransparentPixel(icm.getTransparentPixel());
    } else
        throw new IllegalArgumentException("IMAGE_ERROR");
    IndexColorModel m = (IndexColorModel) cm;
    int mapSize = m.getMapSize();
    r = new byte[mapSize];
    g = new byte[mapSize];
    b = new byte[mapSize];
    m.getReds(r);
    m.getGreens(g);
    m.getBlues(b);
    interlace = false;
    pixelIndex = 0;
    numPixels = width * height;
}

From source file:org.sbs.util.ImageCompress.java

/**
 * gif//w  w  w  . j a va  2  s.c  o m
 * 
 * @param originalFile
 *            
 * @param resizedFile
 *            ?
 * @param newWidth
 *            
 * @param newHeight
 *             -1?
 * @param quality
 *             ()
 * @throws IOException
 */
public void resize(File originalFile, File resizedFile, int newWidth, int newHeight, float quality)
        throws IOException {
    if (quality < 0 || quality > 1) {
        throw new IllegalArgumentException("Quality has to be between 0 and 1");
    }
    ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
    Image i = ii.getImage();
    Image resizedImage = null;
    int iWidth = i.getWidth(null);
    int iHeight = i.getHeight(null);
    if (newHeight == -1) {
        if (iWidth > iHeight) {
            resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
        } else {
            resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
        }
    } else {
        resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    }
    // This code ensures that all the pixels in the image are loaded.
    Image temp = new ImageIcon(resizedImage).getImage();
    // Create the buffered image.
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    // Copy image to buffered image.
    Graphics g = bufferedImage.createGraphics();
    // Clear background and paint the image.
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();
    // Soften.
    float softenFactor = 0.05f;
    float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
            softenFactor, 0 };
    Kernel kernel = new Kernel(3, 3, softenArray);
    ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    bufferedImage = cOp.filter(bufferedImage, null);
    // Write the jpeg to a file.
    FileOutputStream out = FileUtils.openOutputStream(resizedFile);
    // Encodes image as a JPEG data stream
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
    param.setQuality(quality, true);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(bufferedImage);
}

From source file:org.exist.xquery.modules.image.CropFunction.java

/**
 * evaluate the call to the xquery crop() function,
 * it is really the main entry point of this class
 * /*from  w  ww  .  j a v  a  2 s . c  o m*/
 * @param args      arguments from the crop() function call
 * @param contextSequence   the Context Sequence to operate on (not used here internally!)
 * @return      A sequence representing the result of the crop() function call
 * 
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    //was an image and a mime-type speficifed
    if (args[0].isEmpty() || args[2].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }

    //get the maximum dimensions to crop to
    int x1 = 0;
    int y1 = 0;
    int x2 = MAXHEIGHT;
    int y2 = MAXWIDTH;

    if (!args[1].isEmpty()) {
        x1 = ((IntegerValue) args[1].itemAt(0)).getInt();
        if (args[1].hasMany()) {
            y1 = ((IntegerValue) args[1].itemAt(1)).getInt();
            x2 = ((IntegerValue) args[1].itemAt(2)).getInt();
            y2 = ((IntegerValue) args[1].itemAt(3)).getInt();
        }
    }

    //get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);

    //TODO currently ONLY tested for JPEG!!!
    Image image = null;
    BufferedImage bImage = null;
    try {
        //get the image data
        image = ImageIO.read(((BinaryValue) args[0].itemAt(0)).getInputStream());
        //         image = ImageModule.getImage((Base64BinaryValueType)args[0].itemAt(0));
        //               image = ImageIO.read(new ByteArrayInputStream(getImageData((Base64BinaryValueType)args[0].itemAt(0))));
        if (image == null) {
            logger.error("Unable to read image data!");
            return Sequence.EMPTY_SEQUENCE;
        }

        //crop the image
        Image cropImage = Toolkit.getDefaultToolkit()
                .createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(x1, y1, x2, y2)));
        if (cropImage instanceof BufferedImage) {
            // just in case cropImage is allready an BufferedImage
            bImage = (BufferedImage) cropImage;
        } else {
            bImage = new BufferedImage(cropImage.getHeight(null), cropImage.getWidth(null),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bImage.createGraphics(); // Paint the image onto the buffered image
            g.drawImage(cropImage, 0, 0, null);
            g.dispose();
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bImage, formatName, os);

        //return the new croped image data
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(),
                new ByteArrayInputStream(os.toByteArray()));

    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}

From source file:com.celements.photo.image.GenerateThumbnail.java

BufferedImage convertImageToBufferedImage(Image thumbImg, String watermark, String copyright, Color defaultBg) {
    BufferedImage thumb = new BufferedImage(thumbImg.getWidth(null), thumbImg.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = thumb.createGraphics();
    if (defaultBg != null) {
        g2d.setColor(defaultBg);/*from  ww w .  j  a v  a 2s .c o  m*/
        g2d.fillRect(0, 0, thumbImg.getWidth(null), thumbImg.getHeight(null));
    }
    g2d.drawImage(thumbImg, 0, 0, null);

    if ((watermark != null) && (!watermark.equals(""))) {
        drawWatermark(watermark, g2d, thumb.getWidth(), thumb.getHeight());
    }

    if ((copyright != null) && (!copyright.equals(""))) {
        drawCopyright(copyright, g2d, thumb.getWidth(), thumb.getHeight());
    }
    mLogger.info("thumbDimensions: " + thumb.getHeight() + "x" + thumb.getWidth());
    return thumb;
}

From source file:jmap2gml.ItemImage.java

@Override
public void draw(Graphics g) {
    Image img = IMAGES.get(itemName);

    int xOffset, yOffset;

    if (config.has(itemName + "XOFFSET")) {
        xOffset = config.getInt(itemName + "XOFFSET");
    } else {/*from  ww w.  j  a va2 s  .  c o  m*/
        xOffset = 0;
    }

    if (config.has(itemName + "YOFFSET")) {
        yOffset = config.getInt(itemName + "YOFFSET");
    } else {
        yOffset = 0;
    }

    if (img != null) {
        int width, height;

        width = (int) (img.getWidth(null) * xScale);
        height = (int) (img.getHeight(null) * yScale);

        g.drawImage(img, x + xOffset, y + yOffset, width, height, null);
    } else {
        super.draw(g);
    }
}

From source file:info.magnolia.cms.taglibs.util.TextToImageTag.java

/**
 * Create an image file that is a scaled version of the original image
 * @param the original BufferedImage/*from w  w w.  j  a v  a2  s  .  co m*/
 * @param the scale factor
 * @return the new BufferedImage
 */
private BufferedImage scaleImage(BufferedImage oriImgBuff, double scaleFactor) {

    // get the dimesnions of the original image
    int oriWidth = oriImgBuff.getWidth();
    int oriHeight = oriImgBuff.getHeight();
    // get the width and height of the new image
    int newWidth = new Double(oriWidth * scaleFactor).intValue();
    int newHeight = new Double(oriHeight * scaleFactor).intValue();
    // create the thumbnail as a buffered image
    Image newImg = oriImgBuff.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
    BufferedImage newImgBuff = new BufferedImage(newImg.getWidth(null), newImg.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g = newImgBuff.createGraphics();
    g.drawImage(newImg, 0, 0, null);
    g.dispose();
    // return the newImgBuff
    return newImgBuff;
}