Example usage for java.awt.image PixelGrabber getPixels

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

Introduction

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

Prototype

public synchronized Object getPixels() 

Source Link

Document

Get the pixel buffer.

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.j a  v a2  s.c o  m*/
            int[] data = (int[]) grabber.getPixels();
        }
    }
}

From source file:Main.java

static boolean isGreyscaleImage(PixelGrabber pg) {
    return pg.getPixels() instanceof byte[];
}

From source file:FontAlgo.java

private static TextualChar getTextualChar(char a_char) throws Throwable {
    BufferedImage bImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bImg.getGraphics();
    g.setColor(Color.green);/*from w  w w.j  a  v a 2 s.  co m*/
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setFont(appliedFont);
    g.setColor(Color.black);
    g.drawString(new String(new char[] { a_char }), 10, g.getFontMetrics().getHeight());
    PixelGrabber p = new PixelGrabber(bImg, 0, 0, WIDTH, HEIGHT, true);

    if (p.grabPixels()) {
        char[][] pattern = new char[WIDTH][HEIGHT];
        int baseColourPixel = 0, contrastColourPixel = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0;
        int[] pixels = (int[]) p.getPixels();
        baseColourPixel = pixels[0];
        // System.out.println("base: " + base);
        int xCounter = 0, yCounter = 0;
        for (int iPixel : pixels) {
            // System.out.println(iX + " - " + iY);
            if (isReverse) {
                pattern[xCounter][yCounter] = iPixel == baseColourPixel ? CHAR_TO_PATTERN : ' ';
            } else {
                pattern[xCounter][yCounter] = iPixel != baseColourPixel ? CHAR_TO_PATTERN : ' ';
            }

            yCounter++;
            if (yCounter > 49) {
                xCounter++;
                yCounter = 0;
            }

            if (contrastColourPixel == 0 && iPixel != baseColourPixel) {
                contrastColourPixel = iPixel;
                x1 = xCounter - 2;
                y1 = yCounter - 3;
                y2 = yCounter + 3;
            }

            if (contrastColourPixel == iPixel) {
                x2 = xCounter + 3;

                if (y1 > (yCounter - 3)) {
                    y1 = yCounter - 3;
                }

                if (y2 < (yCounter + 3)) {
                    y2 = yCounter + 3;
                }
            }
        }
        return new TextualChar(x1, x2, y1, y2, pattern);
    }
    return null;
}

From source file:com.xebia.visualreview.PixelComparator.java

public static DiffReport processImage(File beforeFile, File afterFile, java.util.Map maskInfo,
        String compareSettings) {
    try {//from  w w w  .ja v a2  s.co  m
        int precision = getPrecision(compareSettings);

        PixelGrabber beforeGrab = grabImage(beforeFile);
        PixelGrabber afterGrab = grabImage(afterFile);

        int[] beforeData = null;
        int[] afterData = null;
        int beforeWidth = 0;
        int afterWidth = 0;
        int y1 = 0;
        int y2 = 0;

        if (beforeGrab.grabPixels()) {
            beforeWidth = beforeGrab.getWidth();
            y1 = beforeGrab.getHeight();
            beforeData = (int[]) beforeGrab.getPixels();
        }

        if (afterGrab.grabPixels()) {
            afterWidth = afterGrab.getWidth();
            y2 = afterGrab.getHeight();
            afterData = (int[]) afterGrab.getPixels();
        }

        int minX = Math.min(beforeWidth, afterWidth);
        int minY = Math.min(y1, y2);
        int diffWidth = Math.max(beforeWidth, afterWidth);
        int diffHeight = Math.max(y1, y2);
        int[] diffData = new int[diffWidth * diffHeight];
        int differentPixels = 0;
        boolean hasMask = (maskInfo != null);
        BufferedImage maskImage = generateMask(maskInfo, diffWidth, diffHeight);
        for (int y = 0; y < diffHeight; y++) {
            for (int x = 0; x < diffWidth; x++) {
                if (maskImage.getRGB(x, y) != DIFFCOLOUR) {
                    if (x >= minX || y >= minY || hasDifference(beforeData[y * beforeWidth + x],
                            afterData[y * afterWidth + x], precision)) {
                        diffData[y * diffWidth + x] = DIFFCOLOUR;
                        differentPixels++;
                    }
                }
            }
        }

        BufferedImage diffImage = new BufferedImage(diffWidth, diffHeight, BufferedImage.TYPE_INT_ARGB);
        diffImage.setRGB(0, 0, diffWidth, diffHeight, diffData, 0, diffWidth);

        DiffReport report = new DiffReport(beforeFile, afterFile, diffImage, differentPixels);
        if (hasMask) {
            report.setMaskImage(maskImage);
        }
        return report;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:GrabandFadewithRasters.java

public void init() {
    URL url;/*from   w  ww  . j a v a2  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:GrabandFade.java

public void init() {
    URL url;//  w w  w .  j a  v a 2s . c o m
    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:GifEncoder.java

/** Constructs a new GifEncoder using an 8-bit AWT Image.
 The image is assumed to be fully loaded. */
public GifEncoder(Image img) {
    width = img.getWidth(null);/*from w ww . ja  va 2  s . c om*/
    height = img.getHeight(null);
    pixels = new byte[width * height];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println(e);
    }
    ColorModel cm = pg.getColorModel();
    if (cm instanceof IndexColorModel) {
        pixels = (byte[]) (pg.getPixels());
        // hpm
        IndexColorModel icm = (IndexColorModel) cm;
        setTransparentPixel(icm.getTransparentPixel());
    } else
        throw new IllegalArgumentException("IMAGE_ERROR");
    IndexColorModel m = (IndexColorModel) cm;
    int mapSize = m.getMapSize();
    r = new byte[mapSize];
    g = new byte[mapSize];
    b = new byte[mapSize];
    m.getReds(r);
    m.getGreens(g);
    m.getBlues(b);
    interlace = false;
    pixelIndex = 0;
    numPixels = width * height;
}