Example usage for java.awt.image BufferedImage BufferedImage

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

Introduction

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

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:Main.java

public static BufferedImage highlightRegions(Image img, int[][] regions, int regionId, Color fgColour) {
    BufferedImage canvas = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = canvas.createGraphics();
    g2d.drawImage(img, 0, 0, null);/*from   ww  w  .j  av a 2  s. com*/
    g2d.setColor(fgColour);
    for (int y = 0; y < regions.length; y++) {
        for (int x = 0; x < regions[y].length; x++) {
            if (regions[y][x] == regionId) {
                g2d.drawRect(x, y, 1, 1);
            }
        }
    }

    return canvas;
}

From source file:Main.java

/**
 * Creates a static rectangular image with given color, width and height.
 *//*from ww  w . j  a  v a2s  . c o m*/
public static Icon buildStaticImage(Color color, int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            image.setRGB(x, y, color.getRGB());
        }
    }
    return new ImageIcon(image);
}

From source file:Main.java

/**
 * Resizes an image using bilinear filtering.
 * @param source   the source image.//  w  w  w.j a va  2s  . c  om
 * @param newWidth   the new image width.
 * @param newHeight   the new image height.
 */
public static BufferedImage performResizeBilinear(BufferedImage source, int newWidth, int newHeight) {
    BufferedImage out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = out.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(source, 0, 0, newWidth, newHeight, null);
    g2d.dispose();
    return out;
}

From source file:Main.java

/**
 * Resizes an image using trilinear filtering.
 * @param source   the source image./*from w ww.j  av a 2 s.c o  m*/
 * @param newWidth   the new image width.
 * @param newHeight   the new image height.
 */
public static BufferedImage performResizeTrilinear(BufferedImage source, int newWidth, int newHeight) {
    BufferedImage out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = out.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(source, 0, 0, newWidth, newHeight, null);
    g2d.dispose();
    return out;
}

From source file:Main.java

/**
 * Gets a "spacer" pixel for use./* w w w  .ja va2s  . co m*/
 * @return
 */
public static BufferedImage create1x1Pixel() {
    BufferedImage b = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = b.getGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, 2, 2);
    g.dispose();
    return b;
}

From source file:Main.java

private static BufferedImage renderRotatedObject(Object src, double angle, int width, int height, double tx,
        double ty) {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = (Graphics2D) dest.getGraphics();
    g2d.setColor(Color.black);//from   www  . ja  v a 2  s. c o m
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AffineTransform at = AffineTransform.getRotateInstance(angle);
    at.translate(tx, ty);
    g2d.setTransform(at);

    if (src instanceof TextLayout) {
        TextLayout tl = (TextLayout) src;
        tl.draw(g2d, 0, tl.getAscent());
    } else if (src instanceof Image) {
        g2d.drawImage((Image) src, 0, 0, null);
    }
    g2d.dispose();

    return dest;
}

From source file:BufferedImageConverter.java

static public BufferedImage createBufferedImage(Image imageIn, int imageType, Component comp) {
    MediaTracker mt = new MediaTracker(comp);
    mt.addImage(imageIn, 0);//from   w  w  w  . j ava  2s. com
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }
    BufferedImage bufferedImageOut = new BufferedImage(imageIn.getWidth(null), imageIn.getHeight(null),
            imageType);
    Graphics g = bufferedImageOut.getGraphics();
    g.drawImage(imageIn, 0, 0, null);

    return bufferedImageOut;
}

From source file:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);//from  w w w  .  ja v  a  2 s.  c  o  m
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:Main.java

/**
 * Quickly copies an image.//from   w ww.j ava2  s.  co  m
 * @param src The source image.
 * @return The replicated image.
 */
public static BufferedImage imgUtilFastCopy(BufferedImage src) {
    if (src == null)
        return null;
    BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    b.setData(src.getRaster());
    return b;
}

From source file:Main.java

/**
 * Resize image to specified dimensions//  w  w  w  . java2  s .  com
 * @param image image to resize
 * @param width new image width
 * @param height new image height
 * @return resized image
 */
public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();

    return resizedImage;
}