Java Code Examples for java.awt.image.BufferedImage.getData()

The following are Jave code examples for showing how to use getData() of the java.awt.image.BufferedImage class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: RemoteAWTService.java   View Source Code Vote up 6 votes
static Snapshot[] getGUISnapshots() {
    List snapshots = new ArrayList();   //System.err.println("gGUI: thread = "+Thread.currentThread());
    Window[] windows = Window.getWindows(); //System.err.println("gGUI: windows = "+windows.length);
    for (int wi = 0; wi < windows.length; wi++) {
        Window w = windows[wi]; //System.err.println("gGUI: w["+wi+"] = "+w+", is visible = "+w.isVisible());
        if (!w.isVisible()) {
            continue;
        }
        Dimension d = w.getSize();  //System.err.println("gGUI:  size = "+d);
        if (d.width == 0 || d.height == 0) {
            continue;
        }
        BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
        w.paint(g);
        Raster raster = bi.getData();
        Object data = raster.getDataElements(0, 0, d.width, d.height, null);
        int[] dataArr;  //System.err.println("gGUI: data = "+data);
        if (data instanceof int[]) {
            dataArr = (int[]) data;
        } else {
            continue;
        }
        String title = null;
        if (w instanceof Frame) {
            title = ((Frame) w).getTitle();
        } else if (w instanceof Dialog) {
            title = ((Dialog) w).getTitle();
        }   //System.err.println("gGUI: title = "+title);
        snapshots.add(new Snapshot(w, title, d.width, d.height, dataArr));
    }
    Snapshot[] snapshotArr = (Snapshot[]) snapshots.toArray(new Snapshot[] {});
    lastGUISnapshots = snapshotArr;
    return snapshotArr;
}
 
Example 2
Project: jdk8u-jdk   File: ShortHistogramTest.java   View Source Code Vote up 6 votes
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 3
Project: openjdk-jdk10   File: ShortHistogramTest.java   View Source Code Vote up 6 votes
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 4
Project: sumo   File: MaskGeometries.java   View Source Code Vote up 5 votes
/**
 * rasterize the mask clipped with the Rectangle scaled back to full size with an offset onto a BufferedImage
 */
public int[] getRasterDataMask(int x,int y,int w,int h,  int offsetX, int offsetY, double scalingFactor) {
	Rectangle rect=new Rectangle(x,y,w,h);
    //BufferedImage bi = this.rasterize(rect, offsetX, offsetY, scalingFactor);
    BufferedImage bi = this.rasterize(rect, offsetX, offsetY,scalingFactor);
    //this.saveRaster(bi, new File("E:/tmp/raster.bmp"));
    Raster rastermask=bi.getData();
	int[] maskdata=rastermask.getSamples(0, 0, rastermask.getWidth(), rastermask.getHeight(),0, (int[])null);
	return maskdata;
}
 
Example 5
Project: zooracle   File: ImgUtils.java   View Source Code Vote up 5 votes
public static Image getImageFromArray(int[] pixels, int width, int height) {
    BufferedImage image = new BufferedImage(width, height+1, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = (WritableRaster) image.getData();
    System.out.println("px;" + pixels.length + " as opposed to " + (height*width));
    raster.setPixels(0,0,width,height-1,pixels);
    
    return image;
}
 
Example 6
Project: arabic-characters-recognition   File: TestModel.java   View Source Code Vote up 5 votes
private static double[][] imageToMat(BufferedImage img) {
    int width = img.getWidth();
    int height = img.getHeight();
    double[][] imgArr = new double[width][height];
    Raster raster = img.getData();
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            imgArr[i][j] = raster.getSample(i, j, 0);
        }
    }
    return imgArr;
}
 
Example 7
Project: OpenJSharp   File: WBMPImageReader.java   View Source Code Vote up 4 votes
public Raster readRaster(int imageIndex,
                         ImageReadParam param) throws IOException {
    BufferedImage bi = read(imageIndex, param);
    return bi.getData();
}
 
Example 8
Project: OpenJSharp   File: BMPImageReader.java   View Source Code Vote up 4 votes
public Raster readRaster(int imageIndex,
                         ImageReadParam param) throws IOException {
    BufferedImage bi = read(imageIndex, param);
    return bi.getData();
}
 
Example 9
Project: jdk8u-jdk   File: WBMPImageReader.java   View Source Code Vote up 4 votes
public Raster readRaster(int imageIndex,
                         ImageReadParam param) throws IOException {
    BufferedImage bi = read(imageIndex, param);
    return bi.getData();
}
 
Example 10
Project: jdk8u-jdk   File: BMPImageReader.java   View Source Code Vote up 4 votes
public Raster readRaster(int imageIndex,
                         ImageReadParam param) throws IOException {
    BufferedImage bi = read(imageIndex, param);
    return bi.getData();
}
 
Example 11
Project: openjdk-jdk10   File: WBMPImageReader.java   View Source Code Vote up 4 votes
public Raster readRaster(int imageIndex,
                         ImageReadParam param) throws IOException {
    BufferedImage bi = read(imageIndex, param);
    return bi.getData();
}
 
Example 12
Project: openjdk-jdk10   File: BMPImageReader.java   View Source Code Vote up 4 votes
public Raster readRaster(int imageIndex,
                         ImageReadParam param) throws IOException {
    BufferedImage bi = read(imageIndex, param);
    return bi.getData();
}
 
Example 13
Project: openjdk-jdk10   File: CrashNaNTest.java   View Source Code Vote up 4 votes
private static void testFillDefaultAt() {
    final int width = 400;
    final int height = 400;

    final BufferedImage image = new BufferedImage(width, height,
                                        BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g2d = (Graphics2D) image.getGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, width, height);

        final Path2D.Double path = new Path2D.Double();
        path.moveTo(100, 100);

        for (int i = 0; i < 20000; i++) {
            path.lineTo(110 + 0.01 * i, 110);
            path.lineTo(111 + 0.01 * i, 100);
        }

        path.lineTo(NaN, 200);
        path.lineTo(200, 200);
        path.lineTo(200, NaN);
        path.lineTo(300, 300);
        path.lineTo(NaN, NaN);
        path.lineTo(100, 200);
        path.closePath();

        final Path2D.Double path2 = new Path2D.Double();
        path2.moveTo(0, 0);
        path2.lineTo(100, height);
        path2.lineTo(0, 200);
        path2.closePath();

        g2d.setColor(Color.BLUE);
        g2d.fill(path);
        g2d.setColor(Color.GREEN);
        g2d.fill(path2);

        g2d.setColor(Color.BLACK);
        g2d.draw(path);
        g2d.draw(path2);

        if (SAVE_IMAGE) {
            try {
                final File file = new File("CrashNaNTest-fill.png");
                System.out.println("Writing file: "
                                   + file.getAbsolutePath());
                ImageIO.write(image, "PNG", file);
            } catch (IOException ex) {
                System.out.println("Writing file failure:");
                ex.printStackTrace();
            }
        }

        // Check image on few pixels:
        final Raster raster = image.getData();

        checkPixel(raster, 200, 195, Color.BLUE.getRGB());
        checkPixel(raster, 105, 195, Color.BLUE.getRGB());
        checkPixel(raster, 286, 290, Color.BLUE.getRGB());

        checkPixel(raster, 108, 105, Color.WHITE.getRGB());
        checkPixel(raster, 205, 200, Color.WHITE.getRGB());

        checkPixel(raster, 5, 200, Color.GREEN.getRGB());

    } finally {
        g2d.dispose();
    }
}
 
Example 14
Project: openjdk-jdk10   File: CrashNaNTest.java   View Source Code Vote up 4 votes
private static void testDrawComplexAt() {
    final int width = 400;
    final int height = 400;

    final BufferedImage image = new BufferedImage(width, height,
                                        BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g2d = (Graphics2D) image.getGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
                             RenderingHints.VALUE_STROKE_PURE);

        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, width, height);

        final Path2D.Double path = new Path2D.Double();
        path.moveTo(100, 100);

        for (int i = 0; i < 20000; i++) {
            path.lineTo(110 + 0.01 * i, 110);
            path.lineTo(111 + 0.01 * i, 100);
        }

        path.lineTo(NaN, 200);
        path.lineTo(200, 200);
        path.lineTo(200, NaN);
        path.lineTo(300, 300);
        path.lineTo(NaN, NaN);
        path.lineTo(100, 200);
        path.closePath();

        final Path2D.Double path2 = new Path2D.Double();
        path2.moveTo(0, 0);
        path2.lineTo(100, height);
        path2.lineTo(0, 200);
        path2.closePath();

        // Define an non-uniform transform:
        g2d.scale(0.5, 1.0);
        g2d.rotate(Math.PI / 31);

        g2d.setColor(Color.BLACK);
        g2d.draw(path);
        g2d.draw(path2);

        if (SAVE_IMAGE) {
            try {
                final File file = new File("CrashNaNTest-draw.png");
                System.out.println("Writing file: "
                                   + file.getAbsolutePath());
                ImageIO.write(image, "PNG", file);
            } catch (IOException ex) {
                System.out.println("Writing file failure:");
                ex.printStackTrace();
            }
        }

        // Check image on few pixels:
        final Raster raster = image.getData();

        checkPixelNotWhite(raster, 40, 210);
        checkPixelNotWhite(raster, 44, 110);
        checkPixelNotWhite(raster, 60, 120);
        checkPixelNotWhite(raster, 89, 219);
        checkPixelNotWhite(raster, 28, 399);
        checkPixelNotWhite(raster, 134, 329);

    } finally {
        g2d.dispose();
    }
}
 
Example 15
Project: openjdk-jdk10   File: CrashNaNTest.java   View Source Code Vote up 4 votes
private static void testPathClosed() {
    final int width = 100;
    final int height = 100;

    final BufferedImage image = new BufferedImage(width, height,
                                        BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g2d = (Graphics2D) image.getGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, width, height);

        final Path2D.Double path = new Path2D.Double();
        path.moveTo(40, 40);
        path.lineTo(0,   0);
        path.lineTo(80, 0);
        path.closePath();
        path.lineTo(80, 80);
        path.lineTo(0, 80);
        path.closePath();

        g2d.setColor(Color.BLUE);
        g2d.fill(path);

        g2d.setColor(Color.BLACK);
        g2d.draw(path);

        if (SAVE_IMAGE) {
            try {
                final File file = new File("CrashNaNTest-path-closed.png");
                System.out.println("Writing file: "
                                   + file.getAbsolutePath());
                ImageIO.write(image, "PNG", file);
            } catch (IOException ex) {
                System.out.println("Writing file failure:");
                ex.printStackTrace();
            }
        }

        // Check image on few pixels:
        final Raster raster = image.getData();

        checkPixel(raster, 10, 05, Color.BLUE.getRGB());
        checkPixel(raster, 70, 05, Color.BLUE.getRGB());
        checkPixel(raster, 40, 35, Color.BLUE.getRGB());

        checkPixel(raster, 10, 75, Color.BLUE.getRGB());
        checkPixel(raster, 70, 75, Color.BLUE.getRGB());
        checkPixel(raster, 40, 45, Color.BLUE.getRGB());

    } finally {
        g2d.dispose();
    }
}