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:de.mpg.imeji.logic.storage.util.ImageUtils.java

/**
 * cale the image if too big for the size
 * //from ww w .java  2  s. co m
 * @param image
 * @param resolution
 * @return
 * @throws Exception
 */
public static BufferedImage scaleImage(BufferedImage image, FileResolution resolution) throws Exception {
    BufferedImage bufferedImage = null;
    int size = getResolution(resolution);
    if (image.getWidth() > size || image.getHeight() > size) {
        bufferedImage = scaleImageFast(image, size, resolution);
    } else {
        bufferedImage = image;
    }
    return bufferedImage;
}

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

public static BufferedImage rgbToGrayscale(BufferedImage image) {
    if (image == null) {
        return null;
    }/*from   w w  w  . ja v a  2 s  . c  o m*/
    BufferedImage returnImage = new BufferedImage(image.getWidth(), image.getHeight(),
            pickBestTransparency(image));
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            int encodedPixel = image.getRGB(x, y);

            int alpha = (encodedPixel >> 24) & 0xff;
            int red = (encodedPixel >> 16) & 0xff;
            int green = (encodedPixel >> 8) & 0xff;
            int blue = (encodedPixel) & 0xff;

            int average = (int) ((red + blue + green) / 3.0);

            // y = 0.3R + 0.59G + 0.11B luminance formula
            int value = (alpha << 24) + (average << 16) + (average << 8) + average;
            returnImage.setRGB(x, y, value);
        }
    }
    return returnImage;
}

From source file:com.ckfinder.connector.utils.ImageUtils.java

/**
 * Uploads image and if the image size is larger than maximum allowed it
 * resizes the image.//www .  j  a v  a  2s .  c  o  m
 *
 * @param stream input stream.
 * @param file file name
 * @param fileName name of file
 * @param conf connector configuration
 * @throws IOException when error occurs.
 */
public static void createTmpThumb(final InputStream stream, final File file, final String fileName,
        final IConfiguration conf) throws IOException {

    BufferedInputStream bufferedIS = new BufferedInputStream(stream);
    bufferedIS.mark(Integer.MAX_VALUE);
    BufferedImage image = ImageIO.read(bufferedIS);
    if (image == null) {
        throw new IOException("Wrong file");
    }
    Dimension dimension = createThumbDimension(image, conf.getImgWidth(), conf.getImgHeight());
    if (image.getHeight() == dimension.height && image.getWidth() == dimension.width) {
        bufferedIS.reset();
        writeUntouchedImage(bufferedIS, file);
    } else {
        resizeImage(image, dimension.width, dimension.height, conf.getImgQuality(), file);
    }
    stream.close();
}

From source file:com.zacwolf.commons.email.Email.java

public static BufferedImage makeRoundedBanner(BufferedImage image, int cornerRadius) {
    int w = image.getWidth();
    int h = image.getHeight() + 10;
    BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = output.createGraphics();
    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);//from  w  w w . jav  a  2 s  .c  om
    g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
    g2.setComposite(AlphaComposite.SrcAtop);
    g2.drawImage(image, 0, 0, null);
    g2.setComposite(AlphaComposite.SrcOver);
    //               g2.setColor(new Color(153,153,153));//slight grey border
    //               g2.drawRoundRect(0, 0, w-1, h, cornerRadius, cornerRadius);
    g2.dispose();
    return output.getSubimage(0, 0, image.getWidth(), image.getHeight());
}

From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java

/**
 * create thumb file.//  w w w  . j ava 2  s.  co  m
 *
 * @param orginFile origin image file.
 * @param file file to save thumb
 * @param conf connector configuration
 * @throws IOException when error occurs.
 */
public static void createThumb(final File orginFile, final File file, final IConfiguration conf)
        throws IOException {
    BufferedImage image = ImageIO.read(orginFile);
    if (image != null) {
        Dimension dimension = createThumbDimension(image, conf.getMaxThumbWidth(), conf.getMaxThumbHeight());
        FileUtils.createPath(file, true);
        if (image.getHeight() == dimension.height && image.getWidth() == dimension.width) {
            writeUntouchedImage(orginFile, file);
        } else {
            resizeImage(image, dimension.width, dimension.height, conf.getThumbsQuality(), file);
        }
    } else {
        if (conf.isDebugMode()) {
            throw new IOException("Wrong image file");
        }
    }

}

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

/**
 * Flip the image horizontal/*from  www  .  j  a  v  a  2s  . c om*/
 *
 * @param bufferedImage original image
 * @return horizontally flipped image
 */
private static BufferedImage flipHorizontal(BufferedImage bufferedImage) {
    AffineTransform at = AffineTransform.getTranslateInstance(bufferedImage.getWidth(), 0);
    at.scale(-1.0, 1.0);
    BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
            bufferedImage.getType());
    return biOp.filter(bufferedImage, imgRes);
}

From source file:com.t3.image.ImageUtil.java

public static BufferedImage rgbToGrayscale(BufferedImage image) {

    if (image == null) {
        return null;
    }/*from  w w  w.j a va2s  . c  o  m*/

    BufferedImage returnImage = new BufferedImage(image.getWidth(), image.getHeight(),
            pickBestTransparency(image));

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {

            int encodedPixel = image.getRGB(x, y);

            int alpha = (encodedPixel >> 24) & 0xff;
            int red = (encodedPixel >> 16) & 0xff;
            int green = (encodedPixel >> 8) & 0xff;
            int blue = (encodedPixel) & 0xff;

            int average = (int) ((red + blue + green) / 3.0);

            // y = 0.3R + 0.59G + 0.11B luminance formula
            int value = (alpha << 24) + (average << 16) + (average << 8) + average;
            returnImage.setRGB(x, y, value);
        }
    }

    return returnImage;
}

From source file:com.ckfinder.connector.utils.ImageUtils.java

/**
 * create thumb file.// ww  w. j a v  a2s.  co  m
 *
 * @param orginFile orgin image file.
 * @param file file to save thumb
 * @param conf connector configuration
 * @throws IOException when error occurs.
 */
public static void createThumb(final File orginFile, final File file, final IConfiguration conf)
        throws IOException {
    BufferedImage image = ImageIO.read(orginFile);
    if (image != null) {
        Dimension dimension = createThumbDimension(image, conf.getMaxThumbWidth(), conf.getMaxThumbHeight());
        FileUtils.createPath(file, conf, true);
        if (image.getHeight() == dimension.height && image.getWidth() == dimension.width) {
            writeUntouchedImage(orginFile, file);
        } else {
            resizeImage(image, dimension.width, dimension.height, conf.getThumbsQuality(), file);
        }
    } else {
        if (conf.isDebugMode()) {
            throw new IOException("Wrong image file");
        }
    }

}

From source file:forge.ImageCache.java

private static BufferedImage scaleImage(String key, final int width, final int height,
        boolean useDefaultImage) {
    if (StringUtils.isEmpty(key) || (3 > width && -1 != width) || (3 > height && -1 != height)) {
        // picture too small or key not defined; return a blank
        return null;
    }/*  w  w  w  . jav a 2 s.c o  m*/

    String resizedKey = String.format("%s#%dx%d", key, width, height);

    final BufferedImage cached = _CACHE.getIfPresent(resizedKey);
    if (null != cached) {
        //System.out.println("found cached image: " + resizedKey);
        return cached;
    }

    BufferedImage original = getOriginalImage(key, useDefaultImage);
    if (original == null) {
        return null;
    }

    // Calculate the scale required to best fit the image into the requested
    // (width x height) dimensions whilst retaining aspect ratio.
    double scaleX = (-1 == width ? 1 : (double) width / original.getWidth());
    double scaleY = (-1 == height ? 1 : (double) height / original.getHeight());
    double bestFitScale = Math.min(scaleX, scaleY);
    if ((bestFitScale > 1) && !FModel.getPreferences().getPrefBoolean(FPref.UI_SCALE_LARGER)) {
        bestFitScale = 1;
    }

    BufferedImage result;
    if (1 == bestFitScale) {
        result = original;
    } else {

        int destWidth = (int) (original.getWidth() * bestFitScale);
        int destHeight = (int) (original.getHeight() * bestFitScale);

        ResampleOp resampler = new ResampleOp(destWidth, destHeight);
        result = resampler.filter(original, null);
    }

    //System.out.println("caching image: " + resizedKey);
    _CACHE.put(resizedKey, result);
    return result;
}

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage flipImage(BufferedImage image, FlipOption flipOption) {

    if (flipOption == FlipOption.FLIP_NONE)
        return image;

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    BufferedImage flippedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = flippedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    int startX = 0;
    int startY = 0;
    int endX = imageWidth;
    int endY = imageHeight;
    if ((flipOption == FlipOption.FLIP_HORIZONTAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startX = imageWidth;//from   w  ww  .jav a 2 s .c o  m
        endX = -imageWidth;
    }
    if ((flipOption == FlipOption.FLIP_VERTICAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startY = imageHeight;
        endY = -imageHeight;
    }

    g.drawImage(image, startX, startY, endX, endY, null);
    g.dispose();
    return flippedImage;
}