Example usage for java.awt.image BufferedImage TYPE_INT_RGB

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

Introduction

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

Prototype

int TYPE_INT_RGB

To view the source code for java.awt.image BufferedImage TYPE_INT_RGB.

Click Source Link

Document

Represents an image with 8-bit RGB color components packed into integer pixels.

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.
 *///from ww  w.  ja  v  a 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: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 ww  w .  jav  a2s. com*/
        }
    }
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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 w w  .  j a  v a 2s  .com
        }
    }
    return oi;
}

From source file:Main.java

/**
 * Gets a "spacer" pixel for use./*from  w  ww . j  a  va  2  s. c om*/
 * @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: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  va  2s  . 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

/**
 * Resize image to specified dimensions//ww w .  j ava  2s.c  o  m
 * @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;
}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage normalizeImage(BufferedImage image) {
    int height = image.getHeight();
    int width = image.getWidth();

    int r, g, b, minr = 255, ming = 255, minb = 255, maxr = 0, maxg = 0, maxb = 0;
    Color c;//from ww  w.  j  a va  2s.c  om

    BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            if (minr > c.getRed()) {
                minr = c.getRed();
            }
            if (ming > c.getGreen()) {
                ming = c.getGreen();
            }
            if (minb > c.getBlue()) {
                minb = c.getBlue();
            }
            if (maxr < c.getRed()) {
                maxr = c.getRed();
            }
            if (maxg < c.getGreen()) {
                maxg = c.getGreen();
            }
            if (maxb < c.getBlue()) {
                maxb = c.getBlue();
            }
        }
    }

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) ((c.getRed() - minr) * 255 / (maxr - minr));
            g = (int) ((c.getGreen() - ming) * 255 / (maxg - ming));
            b = (int) ((c.getBlue() - minb) * 255 / (maxb - minb));
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

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);/*from w ww .ja v a2s . c o m*/
    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

/**
 * Determine the appropriate {@link WritablePixelFormat} type that can
 * be used to transfer data into the indicated BufferedImage.
 * /*w w  w. j a  v  a  2s . c  o  m*/
 * @param bimg the BufferedImage that will be used as a destination for
 *             a {@code PixelReader<IntBuffer>#getPixels()} operation.
 * @return 
 */
private static WritablePixelFormat<IntBuffer> getAssociatedPixelFormat(BufferedImage bimg) {
    switch (bimg.getType()) {
    // We lie here for xRGB, but we vetted that the src data was opaque
    // so we can ignore the alpha.  We use ArgbPre instead of Argb
    // just to get a loop that does not have divides in it if the
    // PixelReader happens to not know the data is opaque.
    case BufferedImage.TYPE_INT_RGB:
    case BufferedImage.TYPE_INT_ARGB_PRE:
        return PixelFormat.getIntArgbPreInstance();
    case BufferedImage.TYPE_INT_ARGB:
        return PixelFormat.getIntArgbInstance();
    default:
        // Should not happen...
        throw new InternalError("Failed to validate BufferedImage type");
    }
}

From source file:Main.java

public void createThumbnail(File file) throws Exception {
    BufferedImage img = ImageIO.read(file);
    BufferedImage thumb = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = (Graphics2D) thumb.getGraphics();
    g2d.drawImage(img, 0, 0, thumb.getWidth() - 1, thumb.getHeight() - 1, 0, 0, img.getWidth() - 1,
            img.getHeight() - 1, null);// ww  w  .  ja v a2  s  . c o  m
    g2d.dispose();
    ImageIO.write(thumb, "PNG", new File("thumb.png"));
}