Java ImageIcon grabRGB(ImageIcon icon)

Here you can find the source of grabRGB(ImageIcon icon)

Description

Grabs a RGB values of the given image icon

License

Open Source License

Parameter

Parameter Description
icon the image icon

Return

an array of RGB values

Declaration

public static int[] grabRGB(ImageIcon icon) 

Method Source Code

//package com.java2s;
//       Distributed under the GNU Lesser General Public License

import java.awt.image.*;

import javax.swing.*;

public class Main {
    /**//from  w  ww. j a va  2  s.com
     * Grabs a RGB values of the given image icon
     *
     * @param icon the image icon
     * @return an array of RGB values
     */
    public static int[] grabRGB(ImageIcon icon) {

        int width = icon.getIconWidth();
        int height = icon.getIconHeight();

        int[] rgb = new int[width * height];
        PixelGrabber pg = new PixelGrabber(icon.getImage(), 0, 0, width, height, rgb, 0, width);

        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted waiting for pixels!");
        }

        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            throw new RuntimeException("Image fetch aborted or errored");
        }

        for (int i = 0; i < rgb.length; i++)
            rgb[i] &= 0xffffff;

        return rgb;
    }
}

Related

  1. getOpenSwingImage(String name, ImageIcon defaultIcon)
  2. getRenderedImage(ImageIcon icon)
  3. getSizeOfImage(ImageIcon imgIn)
  4. getThumbImage(ImageIcon myLoadedImageIcon, int thumbHeight, int thumbWidth)
  5. getThumbnail(ImageIcon src, int maxWidth)
  6. iconDefaultSize(ImageIcon imag)
  7. imageFlip(int w, int h, ImageIcon... icons)
  8. imageToBytes(ImageIcon icon)
  9. imageWithBackground(ImageIcon icon, Color colorBg)