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

/**
 * Creates a buffered image for the given parameters. If there is not enough
 * memory to create the image then a OutOfMemoryError is thrown.
 *///  w w  w  . j  ava 2s .  c  o m
public static BufferedImage createBufferedImage(int w, int h, Color background) {
    BufferedImage result = null;

    if (w > 0 && h > 0) {
        int type = (background != null) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
        result = new BufferedImage(w, h, type);

        // Clears background
        if (background != null) {
            Graphics2D g2 = result.createGraphics();
            clearRect(g2, new Rectangle(w, h), background);
            g2.dispose();
        }
    }

    return result;
}

From source file:Main.java

private static BufferedImage getPooledImage(int width, int height, int index) {
    String key = String.format("%dx%d#%d",
            new Object[] { Integer.valueOf(width), Integer.valueOf(height), Integer.valueOf(index) });
    Reference ref = (Reference) imagePool.get(key);
    BufferedImage image = ref == null ? null : (BufferedImage) ref.get();

    if (image == null) {
        image = new BufferedImage(width, height, 2);
        imagePool.put(key, new SoftReference(image));
    }//w  ww  .  j  ava  2 s  . c  o  m

    return image;
}

From source file:ImageProc.java

public static void imwrite(File file, int[][] classificationMat, int imgDim1, int imgDim2) {
    BufferedImage image = new BufferedImage(imgDim2, imgDim1, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < imgDim1; i++) {
        for (int j = 0; j < imgDim2; j++) {

            switch (classificationMat[i][j]) {
            case 1:
                image.setRGB(j, i, Color.BLUE.getRGB());
                break;
            case 2:
                image.setRGB(j, i, Color.CYAN.getRGB());
                break;
            case 3:
                image.setRGB(j, i, Color.GREEN.getRGB());
                break;
            case 4:
                image.setRGB(j, i, Color.ORANGE.getRGB());
                break;
            case 5:
                image.setRGB(j, i, Color.RED.getRGB());
                break;
            }//from   w  w w  . java  2  s  .  c  o  m
        }
    }
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static BufferedImage joinBufferedImage(BufferedImage img1, BufferedImage img2) {
    int offset = 2;
    int width = img1.getWidth() + img2.getWidth() + offset;
    int height = Math.max(img1.getHeight(), img2.getHeight()) + offset;
    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = newImage.createGraphics();
    Color oldColor = g2.getColor();
    g2.setPaint(Color.BLACK);/* w  w w  . ja  v  a 2 s.c om*/
    g2.fillRect(0, 0, width, height);
    g2.setColor(oldColor);
    g2.drawImage(img1, null, 0, 0);
    g2.drawImage(img2, null, img1.getWidth() + offset, 0);
    g2.dispose();
    return newImage;
}

From source file:BufferedImages.java

/**
 * Returns a {@link BufferedImage} with the specified image type, where the
 * graphical content is a copy of the specified image.
 * /*from  ww  w. j  a v a 2  s . c  o  m*/
 * @param img      The image to copy.
 * @param imageType   The image type for the image to return.
 * @return         A copy of the specified image.
 */
public static BufferedImage copy(BufferedImage img, int imageType) {
    int width = img.getWidth();
    int height = img.getHeight();

    BufferedImage newImage = new BufferedImage(width, height, imageType);
    Graphics g = newImage.createGraphics();

    g.drawImage(img, 0, 0, null);

    g.dispose();

    return newImage;
}

From source file:Main.java

public static Cursor buildCursorByTrimming(Toolkit toolkit, Image image, int x, int y, String name,
        Cursor defaultCursor) {//from   w w w. j  a va2  s  .  c om
    Dimension d = toolkit.getBestCursorSize(image.getWidth(null), image.getHeight(null));
    if (d == null || d.getWidth() <= 0 || d.getHeight() <= 0)
        return defaultCursor;
    BufferedImage out = new BufferedImage((int) d.getWidth(), (int) d.getHeight(), BufferedImage.TYPE_INT_ARGB);
    out.getGraphics().drawImage(image, 0, 0, null);
    return toolkit.createCustomCursor(out, new Point(x, y), name);
}

From source file:Main.java

public static BufferedImage convert2grey(BufferedImage image) {
    BufferedImage grey = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Graphics g = grey.getGraphics();
    g.drawImage(image, 0, 0, null);// w w  w.j  a  va  2  s  . com
    g.dispose();
    BufferedImage rgb = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = rgb.getGraphics();
    g.drawImage(grey, 0, 0, null);
    g.dispose();
    return rgb;
}

From source file:Main.java

private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
    int IMG_WIDTH = 512;
    int IMG_CLAHEIGHT = 512;
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_CLAHEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_CLAHEIGHT, null);
    g.dispose();// www .ja va 2 s .c  om
    return resizedImage;
}

From source file:contactangle.ImageControl.java

public static BufferedImage copyByPixel(BufferedImage ii) {
    BufferedImage oi = new BufferedImage(ii.getWidth(), ii.getHeight(), BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < ii.getWidth(); x++) {
        for (int y = 0; y < ii.getHeight(); y++) {
            int pixelData = ii.getRGB(x, y);
            oi.setRGB(x, y, pixelData);/*from   w  ww .  java  2s.  c  o  m*/
        }
    }
    return oi;
}

From source file:Main.java

public static BufferedImage takeScreenShot(Component component, String... watermarks) {

    Dimension size = component.getSize();

    BufferedImage screenshot = new BufferedImage(size.width, size.height, Transparency.OPAQUE);
    Graphics2D g = screenshot.createGraphics();
    g.setClip(0, 0, size.width - 1, size.height - 1);

    component.update(g);//ww w .ja v a2  s. c  o  m

    FontMetrics fm = g.getFontMetrics();
    int y = fm.getDescent();
    for (String watermark : watermarks)
        if (watermark != null) {
            int x = size.width - SwingUtilities.computeStringWidth(fm, watermark);

            g.setColor(Color.black);
            g.drawString(watermark, x, y);

            g.setColor(Color.white);
            g.drawString(watermark, x - 1, y - 1);

            y -= fm.getHeight();
        }

    g.dispose();

    return screenshot;
}