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

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from   www  .j  a  v a 2 s . c  om*/

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = true;

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:ConvolveIt.java

public ConvolveIt() {
    int width = image.getWidth(this);
    int height = image.getHeight(this);
    bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bufferedImage.createGraphics();
    AffineTransform affineTransform = new AffineTransform();
    big.drawImage(image, affineTransform, this);
    Kernel kernel = new Kernel(3, 3, SHARP);
    convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
}

From source file:DoubleBufferWithBufferedImage.java

public DoubleBufferWithBufferedImage() {
    setSize(300, 300);/*from w w w .j  av a  2 s .c  om*/
    Dimension d = getSize();
    w = d.width;
    h = d.height;
    buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}

From source file:de.mfo.jsurf.grid.RotationGrid.java

public static BufferedImage renderAnimGrid(int xAngleMin, int xAngleMax, int xSteps, int yAngleMin,
        int yAngleMax, int ySteps) {
    BufferedImage grid = new BufferedImage(ySteps * size, xSteps * size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) grid.getGraphics();
    for (int x = 0; x < xSteps; ++x) {
        double xAngle = xAngleMin + (xAngleMax - xAngleMin) * (xSteps == 1 ? 0.5 : (x / (double) (xSteps - 1)));
        Matrix4d matRotX = new Matrix4d();
        matRotX.setIdentity();/*from w  w w.j a  v a 2  s.c om*/
        matRotX.rotX(Math.toRadians(xAngle));
        for (int y = 0; y < ySteps; ++y) {
            double yAngle = yAngleMin
                    + (yAngleMax - yAngleMin) * (ySteps == 1 ? 0.5 : (y / (double) (ySteps - 1)));
            Matrix4d matRotY = new Matrix4d();
            matRotY.setIdentity();
            matRotY.rotY(Math.toRadians(yAngle));
            additional_rotation.mul(matRotY, matRotX);
            BufferedImage bi = createBufferedImageFromRGB(draw(size, size, aam, aap));
            g2.drawImage(bi,
                    new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
                    (ySteps - 1 - y) * size, x * size);
        }
    }
    return grid;
}

From source file:ImageDuplicity.java

private void createOffscreenImage() {
    Dimension d = getSize();/* w  w  w .j  a v  a 2  s .  c  o  m*/
    int width = d.width;
    int height = d.height;
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = image.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    try {
        String filename = "largeJava2sLogo.jpg";
        InputStream in = getClass().getResourceAsStream(filename);
        JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
        BufferedImage image = decoder.decodeAsBufferedImage();
        in.close();
        g2.drawImage(image, 0, 0, width, height, null);
    } catch (Exception e) {
        System.out.print(e);
    }

    g2.setStroke(new BasicStroke(2));
    Color[] colors = { Color.red, Color.blue, Color.green };
    for (int i = -32; i < 40; i += 8) {
        g2.setPaint(colors[Math.abs(i) % 3]);
        g2.drawOval(i, i, width - i * 2, height - i * 2);
    }
}

From source file:ImageUtil.java

public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException {
    int x = src.getWidth() / 2 - width / 2;
    int y = src.getHeight() / 2 - height / 2;

    //        System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y);

    BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType());  
    Graphics2D area = (Graphics2D) clipping.getGraphics().create();
    area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(),
            y + clipping.getHeight(), null);
    area.dispose();/*from w w  w  .j a  va 2 s  . com*/

    return clipping;
}

From source file:ImageProc.java

public static void imagesc(File file, int[][] classificationMat, int nClass, int imgDim1, int imgDim2) {
    BufferedImage image = new BufferedImage(imgDim2, imgDim1, BufferedImage.TYPE_INT_RGB);
    int index, rgb;
    float[][] jet = colormapJet(nClass);
    for (int i = 0; i < imgDim1; i++) {
        for (int j = 0; j < imgDim2; j++) {
            index = classificationMat[i][j];
            if (index == -1) {
                image.setRGB(j, i, Color.BLACK.getRGB());
            } else {
                rgb = new Color(jet[index][0], jet[index][1], jet[index][2]).getRGB();
                image.setRGB(j, i, rgb);
            }/*w w  w. j  ava  2  s.c om*/
        }
    }
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public MyPanel() {
    this.setLayout(new GridLayout());
    this.setPreferredSize(new Dimension(W, H));
    int w = W / 2;
    int h = H / 2;
    int r = w / 5;
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(Color.gray);//from   w w  w  .j  a v  a 2  s  .  c  om
    g.fillRect(0, 0, w, h);
    g.setColor(Color.blue);
    g.fillRect(w / 2 - r, h / 2 - r / 2, 2 * r, r);
    g.dispose();
    this.add(new JLabel(new ImageIcon(bi), JLabel.CENTER));
}

From source file:com.taunova.app.libview.components.ImageHelpers.java

public static Image getScaledImage(BufferedImage image, int width) {
    Dimension d = getScaledDimension(image, width);
    Image scaledImage = image.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH);

    BufferedImage resizedImage = null;

    final boolean OPTIMIZE_SCALE = true;

    if (OPTIMIZE_SCALE) {
        ResampleOp resampleOp = new ResampleOp(100, 200);
        resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal);
        resizedImage = resampleOp.filter(image, null);
    } else {//  w  ww. j a  v a  2 s  . c o m
        resizedImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawImage(scaledImage, 0, 0, d.width, d.height, null);
        g.dispose();
    }

    return resizedImage;
}

From source file:app.utils.ImageUtilities.java

public static void saveImageToFile(Image image, String filterName) throws IOException {
    //TODO logger
    File imgFile = new File(DEFAULT_FILE_SAVE_PATH + image.getName().split("\\.")[0] + "_" + filterName + "."
            + DEFAULT_FILE_EXTENSION);/*from  www .  ja  va2  s . com*/
    BufferedImage bufferedImage = convertImageToBufferedImage(image, BufferedImage.TYPE_INT_RGB);
    ImageIO.write(bufferedImage, DEFAULT_FILE_EXTENSION, imgFile);
}