Example usage for javafx.scene.image PixelReader getPixels

List of usage examples for javafx.scene.image PixelReader getPixels

Introduction

In this page you can find the example usage for javafx.scene.image PixelReader getPixels.

Prototype

public void getPixels(int x, int y, int w, int h, WritablePixelFormat<IntBuffer> pixelformat, int buffer[],
        int offset, int scanlineStride);

Source Link

Document

Reads pixel data from a rectangular region of the surface into the specified int array.

Usage

From source file:net.rptools.tokentool.util.ImageUtil.java

public static Image resizeCanvas(Image imageSource, int newWidth, int newHeight, int offsetX, int offsetY) {
    int sourceWidth = (int) imageSource.getWidth();
    int sourceHeight = (int) imageSource.getHeight();

    // No work needed here...
    if (sourceWidth == newWidth && sourceHeight == newHeight)
        return imageSource;

    WritableImage outputImage = new WritableImage(newWidth, newHeight);
    PixelReader pixelReader = imageSource.getPixelReader();
    PixelWriter pixelWriter = outputImage.getPixelWriter();
    WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();

    int[] buffer = new int[sourceWidth * sourceHeight];
    pixelReader.getPixels(0, 0, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth);
    pixelWriter.setPixels(offsetX, offsetY, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth);

    return outputImage;
}

From source file:Main.java

/**
 * Snapshots the specified JavaFX {@link Image} object and stores a
 * copy of its pixels into a {@link BufferedImage} object, creating
 * a new object if needed.//  w  ww  .  ja  v a 2 s.  co m
 * The method will only convert a JavaFX {@code Image} that is readable
 * as per the conditions on the
 * {@link Image#getPixelReader() Image.getPixelReader()}
 * method.
 * If the {@code Image} is not readable, as determined by its
 * {@code getPixelReader()} method, then this method will return null.
 * If the {@code Image} is a writable, or other dynamic image, then
 * the {@code BufferedImage} will only be set to the current state of
 * the pixels in the image as determined by its {@link PixelReader}.
 * Further changes to the pixels of the {@code Image} will not be
 * reflected in the returned {@code BufferedImage}.
 * <p>
 * The optional {@code BufferedImage} parameter may be reused to store
 * the copy of the pixels.
 * A new {@code BufferedImage} will be created if the supplied object
 * is null, is too small or of a type which the image pixels cannot
 * be easily converted into.
 * 
 * @param img the JavaFX {@code Image} to be converted
 * @param bimg an optional {@code BufferedImage} object that may be
 *        used to store the returned pixel data
 * @return a {@code BufferedImage} containing a snapshot of the JavaFX
 *         {@code Image}, or null if the {@code Image} is not readable.
 * @since JavaFX 2.2
 */
public static BufferedImage fromFXImage(Image img, BufferedImage bimg) {
    PixelReader pr = img.getPixelReader();
    if (pr == null) {
        return null;
    }
    int iw = (int) img.getWidth();
    int ih = (int) img.getHeight();
    int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg);
    if (bimg != null) {
        int bw = bimg.getWidth();
        int bh = bimg.getHeight();
        if (bw < iw || bh < ih || bimg.getType() != prefBimgType) {
            bimg = null;
        } else if (iw < bw || ih < bh) {
            Graphics2D g2d = bimg.createGraphics();
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, 0, bw, bh);
            g2d.dispose();
        }
    }
    if (bimg == null) {
        bimg = new BufferedImage(iw, ih, prefBimgType);
    }
    IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
    int offset = icr.getDataOffset(0);
    int scan = icr.getScanlineStride();
    int data[] = icr.getDataStorage();
    WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg);
    pr.getPixels(0, 0, iw, ih, pf, data, offset, scan);
    return bimg;
}