Example usage for java.awt.image BufferedImage getWidth

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

Introduction

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

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using arbitrary intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16//from  w w  w . j a va  2  s.co m
 * @return
 */
public static BufferedImage convertTo8bit(BufferedImage bi16, double intScalingMin, double intScalignMax) {
    // TODO: use min,max from plate meta data, not image specific
    int[][] minMax = ImageUtils.getMinMaxIntensitiesOfBI(bi16);
    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, getPercentileIntensity(bi16, intScalingMin),
                getPercentileIntensity(bi16, intScalignMax));
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else if (bi16.getSampleModel().getNumBands() == 3) {
        bi16 = ImageUtils.scaleIntensities(bi16, getPercentileIntensity(bi16, intScalingMin),
                getPercentileIntensity(bi16, intScalignMax));
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_INT_RGB);
    } else {
        throw new IllegalArgumentException(
                "Only images with 1 band (gray-color) or 3 bands (rgb) supported. This image has "
                        + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}

From source file:net.pms.util.UMSUtils.java

@SuppressWarnings("deprecation")
public static InputStream scaleThumb(InputStream in, RendererConfiguration r) throws IOException {
    if (in == null) {
        return in;
    }//from w  w  w.j a  v  a 2s .c o m
    String ts = r.getThumbSize();
    if (StringUtils.isEmpty(ts) && StringUtils.isEmpty(r.getThumbBG())) {
        // no need to convert here
        return in;
    }
    int w;
    int h;
    Color col = null;
    BufferedImage img;
    try {
        img = ImageIO.read(in);
    } catch (Exception e) {
        // catch whatever is thrown at us
        // we can at least log it
        LOGGER.debug("couldn't read thumb to manipulate it " + e);
        img = null; // to make sure
    }
    if (img == null) {
        return in;
    }
    w = img.getWidth();
    h = img.getHeight();
    if (StringUtils.isNotEmpty(ts)) {
        // size limit thumbnail
        w = getHW(ts.split("x"), 0);
        h = getHW(ts.split("x"), 1);
        if (w == 0 || h == 0) {
            LOGGER.debug("bad thumb size {} skip scaling", ts);
            w = h = 0; // just to make sure
        }
    }
    if (StringUtils.isNotEmpty(r.getThumbBG())) {
        try {
            Field field = Color.class.getField(r.getThumbBG());
            col = (Color) field.get(null);
        } catch (Exception e) {
            LOGGER.debug("bad color name " + r.getThumbBG());
        }
    }
    if (w == 0 && h == 0 && col == null) {
        return in;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedImage img1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img1.createGraphics();
    if (col != null) {
        g.setColor(col);
    }
    g.fillRect(0, 0, w, h);
    g.drawImage(img, 0, 0, w, h, null);
    ImageIO.write(img1, "jpeg", out);
    out.flush();
    return new ByteArrayInputStream(out.toByteArray());
}

From source file:GraphicsUtil.java

/**
 * Copies data from one bufferedImage to another paying attention
 * to the state of AlphaPreMultiplied./* ww w.ja  v  a  2s.c  om*/
 *
 * @param src The source
 * @param dst The destination
 */
public static void copyData(BufferedImage src, BufferedImage dst) {
    Rectangle srcRect = new Rectangle(0, 0, src.getWidth(), src.getHeight());
    copyData(src, srcRect, dst, new Point(0, 0));
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from ww w.  jav  a  2 s .  co m*/
 * Returns a thumbnail of a source image.
 * </p>
 * <p>
 * This method favors speed over quality. When the new size is less than half
 * the longest dimension of the source image,
 * {@link #createThumbnail(BufferedImage, int)} or
 * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to
 * ensure the quality of the result without sacrificing too much performance.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newWidth
 *            the width of the thumbnail
 * @param newHeight
 *            the height of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newWidth</code> is larger than the width of
 *             <code>image</code> or if code>newHeight</code> is larger
 *             than the height of <code>image</code> or if one of the
 *             dimensions is &lt;= 0
 */
public static BufferedImage createThumbnailFast(BufferedImage image, int newWidth, int newHeight) {
    if (newWidth >= image.getWidth() || newHeight >= image.getHeight()) {
        throw new IllegalArgumentException(
                "newWidth and newHeight cannot" + " be greater than the image" + " dimensions");
    } else if (newWidth <= 0 || newHeight <= 0) {
        throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0");
    }

    BufferedImage temp = createCompatibleImage(image, newWidth, newHeight);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
    g2.dispose();

    return temp;
}

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

/**
 * Rotate an image using the requested angle
 *
 * @param bufferedImage imeg to rotate/*w w w  .j  a v a2 s .  co m*/
 * @param angle         angle to rotate
 * @return BufferedImage containing the rotation
 */
@SuppressWarnings({ "UnusedAssignment" })
private static BufferedImage rotate(BufferedImage bufferedImage, int angle) {
    angle = angle % 360;
    if (angle == 0)
        return bufferedImage;
    if (angle < 0)
        angle += 360;
    switch (angle) {
    case 90:
        BufferedImageOp rot90 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI / 2.0,
                bufferedImage.getHeight() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img90 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot90.filter(bufferedImage, img90);
    case 180:
        BufferedImageOp rot180 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
                bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img180 = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                bufferedImage.getType());
        return rot180.filter(bufferedImage, img180);
    case 270:
        BufferedImageOp rot270 = new AffineTransformOp(AffineTransform.getRotateInstance(-Math.PI / 2.0,
                bufferedImage.getWidth() / 2.0, bufferedImage.getWidth() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img270 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot270.filter(bufferedImage, img270);
    default:
        //rotate using a non-standard angle (have to draw a box around the image that can fit it)
        int box = (int) Math.sqrt(bufferedImage.getHeight() * bufferedImage.getHeight()
                + bufferedImage.getWidth() * bufferedImage.getWidth());
        BufferedImage imgFree = new BufferedImage(box, box, bufferedImage.getType());
        BufferedImage imgRet = new BufferedImage(box, box, bufferedImage.getType());
        Graphics2D g = imgFree.createGraphics();
        if (bufferedImage.getTransparency() == Transparency.OPAQUE) {
            //draw a white background on opaque images since they dont support transparency
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, box, box);
            Graphics2D gr = imgRet.createGraphics();
            gr.setBackground(Color.WHITE);
            gr.clearRect(0, 0, box, box);
            gr = null;
        }
        g.drawImage(bufferedImage, box / 2 - bufferedImage.getWidth() / 2,
                box / 2 - bufferedImage.getHeight() / 2, bufferedImage.getWidth(), bufferedImage.getHeight(),
                null);
        g = null;
        AffineTransform at = new AffineTransform();
        at.rotate(angle * Math.PI / 180.0, box / 2.0, box / 2.0);
        BufferedImageOp bio;
        bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        return bio.filter(imgFree, imgRet);
    }
}

From source file:ImageOpByRomain.java

/**
 * <p>/*  www .  j  ava2  s . co m*/
 * Returns a thumbnail of a source image. <code>newSize</code> defines the
 * length of the longest dimension of the thumbnail. The other dimension is
 * then computed according to the dimensions ratio of the original picture.
 * </p>
 * <p>
 * This method offers a good trade-off between speed and quality. The result
 * looks better than
 * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the
 * new size is less than half the longest dimension of the source image, yet
 * the rendering speed is almost similar.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newSize
 *            the length of the largest dimension of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newSize</code> is larger than the largest dimension
 *             of <code>image</code> or &lt;= 0
 */
public static BufferedImage createThumbnail(BufferedImage image, int newSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    boolean isWidthGreater = width > height;

    if (isWidthGreater) {
        if (newSize >= width) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image width");
        }
    } else if (newSize >= height) {
        throw new IllegalArgumentException("newSize must be lower than" + " the image height");
    }

    if (newSize <= 0) {
        throw new IllegalArgumentException("newSize must" + " be greater than 0");
    }

    float ratioWH = (float) width / (float) height;
    float ratioHW = (float) height / (float) width;

    BufferedImage thumb = image;

    do {
        if (isWidthGreater) {
            width /= 2;
            if (width < newSize) {
                width = newSize;
            }
            height = (int) (width / ratioWH);
        } else {
            height /= 2;
            if (height < newSize) {
                height = newSize;
            }
            width = (int) (height / ratioHW);
        }

        BufferedImage temp = createCompatibleImage(image, width, height);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
        g2.dispose();

        thumb = temp;
    } while (newSize != (isWidthGreater ? width : height));

    return thumb;
}

From source file:ImageOpByRomain.java

/**
 * <p>/*w ww.  j  ava 2s. co  m*/
 * Returns a thumbnail of a source image.
 * </p>
 * <p>
 * This method offers a good trade-off between speed and quality. The result
 * looks better than
 * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the
 * new size is less than half the longest dimension of the source image, yet
 * the rendering speed is almost similar.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @param image
 *            the source image
 * @param newWidth
 *            the width of the thumbnail
 * @param newHeight
 *            the height of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newWidth</code> is larger than the width of
 *             <code>image</code> or if code>newHeight</code> is larger
 *             than the height of <code>image or if one the dimensions is not
 *             &gt; 0</code>
 */
public static BufferedImage createThumbnail(BufferedImage image, int newWidth, int newHeight) {
    int width = image.getWidth();
    int height = image.getHeight();

    if (newWidth >= width || newHeight >= height) {
        throw new IllegalArgumentException(
                "newWidth and newHeight cannot" + " be greater than the image" + " dimensions");
    } else if (newWidth <= 0 || newHeight <= 0) {
        throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0");
    }

    BufferedImage thumb = image;

    do {
        if (width > newWidth) {
            width /= 2;
            if (width < newWidth) {
                width = newWidth;
            }
        }

        if (height > newHeight) {
            height /= 2;
            if (height < newHeight) {
                height = newHeight;
            }
        }

        BufferedImage temp = createCompatibleImage(image, width, height);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
        g2.dispose();

        thumb = temp;
    } while (width != newWidth || height != newHeight);

    return thumb;
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from   w ww .  j av  a  2s .co  m*/
 * Returns a thumbnail of a source image. <code>newSize</code> defines the
 * length of the longest dimension of the thumbnail. The other dimension is
 * then computed according to the dimensions ratio of the original picture.
 * </p>
 * <p>
 * This method favors speed over quality. When the new size is less than half
 * the longest dimension of the source image,
 * {@link #createThumbnail(BufferedImage, int)} or
 * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to
 * ensure the quality of the result without sacrificing too much performance.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newSize
 *            the length of the largest dimension of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newSize</code> is larger than the largest dimension
 *             of <code>image</code> or &lt;= 0
 */
public static BufferedImage createThumbnailFast(BufferedImage image, int newSize) {
    float ratio;
    int width = image.getWidth();
    int height = image.getHeight();

    if (width > height) {
        if (newSize >= width) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image width");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) width / (float) height;
        width = newSize;
        height = (int) (newSize / ratio);
    } else {
        if (newSize >= height) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image height");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) height / (float) width;
        height = newSize;
        width = (int) (newSize / ratio);
    }

    BufferedImage temp = createCompatibleImage(image, width, height);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
    g2.dispose();

    return temp;
}

From source file:TexturedText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Times New Roman", Font.PLAIN, 72);
    g2.setFont(font);//from ww w.  j  a  v a2s  . co m

    String s = "Java Source and Support";
    Dimension d = getSize();
    float x = 20, y = 100;

    BufferedImage bi = getTextureImage();
    Rectangle r = new Rectangle(0, 0, bi.getWidth(), bi.getHeight());
    TexturePaint tp = new TexturePaint(bi, r);
    g2.setPaint(tp);

    g2.drawString(s, x, y);
}

From source file:TextureByReference.java

public static void flipImage(BufferedImage bImage) {
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    int[] rgbArray = new int[width * height];
    bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
    int[] tempArray = new int[width * height];
    int y2 = 0;/*from   w ww.j  av  a2  s.c o m*/
    for (int y = height - 1; y >= 0; y--) {
        for (int x = 0; x < width; x++) {
            tempArray[y2 * width + x] = rgbArray[y * width + x];
        }
        y2++;
    }
    bImage.setRGB(0, 0, width, height, tempArray, 0, width);
}