Example usage for java.awt.image PixelGrabber PixelGrabber

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

Introduction

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

Prototype

public PixelGrabber(Image img, int x, int y, int w, int h, boolean forceRGB) 

Source Link

Document

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the specified image.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Image image = Toolkit.getDefaultToolkit().getImage("inFile.png");

    PixelGrabber grabber = new PixelGrabber(image, 0, 0, -1, -1, false);

    if (grabber.grabPixels()) {
        int width = grabber.getWidth();
        int height = grabber.getHeight();
        if (isGreyscaleImage(grabber)) {
            byte[] data = (byte[]) grabber.getPixels();
        } else {/* w w  w. ja v  a2 s  . co m*/
            int[] data = (int[]) grabber.getPixels();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Image image = new ImageIcon("a.png").getImage();
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        System.out.println(bimage.getColorModel().hasAlpha());
    }// w  w w  . ja va2  s.  c o  m

    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    ColorModel cm = pg.getColorModel();
    System.out.println(cm.hasAlpha());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Image image = new ImageIcon("a.png").getImage();
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        System.out.println(bimage.getColorModel().getNumColorComponents());
    }/*from   ww w . ja v a2s.  c  o  m*/

    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }
    ColorModel cm = pg.getColorModel();
    System.out.println(cm.getNumColorComponents());
}

From source file:Main.java

/**
 * This method returns true if the specified image has transparent pixels
 *
 * @param image/* w  w w . jav  a 2  s .  com*/
 * @return
 */
public static boolean hasAlpha(Image image) {
    // If buffered image, the colour model is readily available
    if (image instanceof BufferedImage) {
        return ((BufferedImage) image).getColorModel().hasAlpha();
    }

    // Use a pixel grabber to retrieve the image's colour model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's colour model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:Main.java

public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }/*from   w  w  w . ja  v  a 2s .c  om*/

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:MainClass.java

public void init() {
    i = getImage(getDocumentBase(), "ora-icon.gif");
    pg = new PixelGrabber(i, 0, 0, -1, -1, false);
    pg.startGrabbing();/* ww  w.  ja  v a 2s  . c  o  m*/
    enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}

From source file:ImageUtils.java

/**
 * This method returns true if the specified image has transparent pixels
 * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html
 * @param image//from   w w w  .  j  a v a 2  s .  c o m
 * @return True if the image has any transparency
 */
public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:GrabandFade.java

public void init() {
    URL url;/*  ww w. j  a  v  a2  s  . c om*/
    try {
        // set imageURLString here
        url = new URL(imageURLString);
        originalImage = getImage(url);
    } catch (MalformedURLException me) {
        showStatus("Malformed URL: " + me.getMessage());
    }

    /*
     * Create PixelGrabber and use it to fill originalPixelArray with image
     * pixel data. This array will then by used by the MemoryImageSource.
     */
    try {
        PixelGrabber grabber = new PixelGrabber(originalImage, 0, 0, -1, -1, true);
        if (grabber.grabPixels()) {
            width = grabber.getWidth();
            height = grabber.getHeight();
            originalPixelArray = (int[]) grabber.getPixels();

            mis = new MemoryImageSource(width, height, originalPixelArray, 0, width);
            mis.setAnimated(true);
            newImage = createImage(mis);
        } else {
            System.err.println("Grabbing Failed");
        }
    } catch (InterruptedException ie) {
        System.err.println("Pixel Grabbing Interrupted");
    }
}

From source file:GrabandFadewithRasters.java

public void init() {
    URL url;//from   w w  w. j  a  v a 2 s .co m
    try {
        url = new URL(imageURLString);
        originalImage = getImage(url);
    } catch (MalformedURLException me) {
        showStatus("Malformed URL: " + me.getMessage());
    }

    try {
        PixelGrabber grabber = new PixelGrabber(originalImage, 0, 0, -1, -1, true);
        if (grabber.grabPixels()) {
            width = grabber.getWidth();
            height = grabber.getHeight();
            originalPixelArray = (int[]) grabber.getPixels();

            mis = new MemoryImageSource(width, height, originalPixelArray, 0, width);
            mis.setAnimated(true);
            newImage = createImage(mis);
        } else {
            System.err.println("Grabbing Failed");
        }
    } catch (InterruptedException ie) {
        System.err.println("Pixel Grabbing Interrupted");
    }

    DataBufferInt dbi = new DataBufferInt(originalPixelArray, width * height);

    int bandmasks[] = { 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff };
    SampleModel sm;
    sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, bandmasks);

    raster = Raster.createWritableRaster(sm, dbi, null);
}

From source file:ImageUtil.java

public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage)
        return ((BufferedImage) image).getColorModel().hasAlpha();

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {/*from w w w.j  a  v  a2s .  c o  m*/
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    return pg.getColorModel().hasAlpha();
}