Example usage for java.awt.image PixelGrabber grabPixels

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

Introduction

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

Prototype

public boolean grabPixels() throws InterruptedException 

Source Link

Document

Request the Image or ImageProducer to start delivering pixels and wait for all of the pixels in the rectangle of interest to be delivered.

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 {//from w  w  w . java 2  s. c  om
            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().getNumColorComponents());
    }// w  w w  .ja  v a2s .co  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

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.  j  a v a 2 s . co  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[] args) throws Exception {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

    Graphics g = img.getGraphics();
    g.setColor(Color.red);/*  w ww.  ja v a  2 s.  c  o  m*/
    g.setFont(new Font("Arial", Font.BOLD, 14));
    g.drawString("Reference", 10, 80);

    int w = 100;
    int h = 100;
    int x = 1;
    int y = 1;

    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);

    pg.grabPixels();
    BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            bimg.setRGB(x + i, y + j, pixels[j * w + i]);
        }
    }

    FileOutputStream fout = new FileOutputStream("jpg.jpg");

    JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(fout);
    JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(bimg);

    enParam.setQuality(1.0F, true);
    jencoder.setJPEGEncodeParam(enParam);
    jencoder.encode(bimg);

    fout.close();

}

From source file:Main.java

private static int[] makeGradientPallet() {
    BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    Point2D start = new Point2D.Float(0f, 0f);
    Point2D end = new Point2D.Float(99f, 0f);
    float[] dist = { 0.0f, 0.5f, 1.0f };
    Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
    g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
    g2.fillRect(0, 0, 100, 1);//from ww w .  j a  va2 s  . c  om
    g2.dispose();

    int width = image.getWidth(null);
    int[] pallet = new int[width];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
    try {
        pg.grabPixels();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pallet;
}

From source file:Main.java

/**
 * This method returns true if the specified image has transparent pixels
 *
 * @param image// w  w  w  . j  ava2  s .  c om
 * @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:Util.java

/**
 * Converts a java.awt.Image into an array of pixels
 *///ww  w  .ja  va 2  s. co  m
public static int[] convertToPixels(Image img) {
    int width = img.getWidth(null);
    int height = img.getHeight(null);
    int[] pixel = new int[width * height];

    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        throw new IllegalStateException("Error: Interrupted Waiting for Pixels");
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        throw new IllegalStateException("Error: Image Fetch Aborted");
    }
    return pixel;
}

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 {//w  w w  .  j av  a2s .  c  om
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    return pg.getColorModel().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();
    }//  w ww .j  a  v a 2 s  .  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:de.laures.cewolf.util.ImageHelper.java

public static boolean hasAlpha(Image image) {
    if (image instanceof BufferedImage) {
        return ((BufferedImage) image).getColorModel().hasAlpha();
    }//  w w  w  .  j av a  2  s  . c o  m
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    ColorModel cm = pg.getColorModel();
    if (cm == null) {
        return false;
    }
    return cm.hasAlpha();
}