Example usage for java.awt.image BufferedImage getRaster

List of usage examples for java.awt.image BufferedImage getRaster

Introduction

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

Prototype

public WritableRaster getRaster() 

Source Link

Document

Returns the WritableRaster .

Usage

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./*from   www. ja  v  a 2  s . c  om*/
 * 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;
}

From source file:pl.dp.bz.poid.fouriertest.FourierProc.java

public static BufferedImage changeImageQuadrants(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();
    //Tutaj zamieniamy miejscami kolejne wiartki
    int[][] quarters = new int[w][h];
    //wiartka pierwsza w miejsce czwartej
    for (int x = w / 2; x < w; x++) {
        for (int y = h / 2; y < h; y++) {
            int[] t = new int[1];
            t = image.getRaster().getPixel(x - (w / 2), y - (h / 2), t);
            quarters[x][y] = t[0];/*from  w  w w.  j  av  a 2  s .c  o  m*/
        }
    }

    //wiartka czwarta w miejsce pierwszej
    for (int x = 0; x < w / 2; x++) {
        for (int y = 0; y < h / 2; y++) {
            int[] t = new int[1];
            t = image.getRaster().getPixel(x + (w / 2), y + (h / 2), t);
            quarters[x][y] = t[0];
        }
    }

    //wiartka druga w miejsce trzeciej
    for (int x = 0; x < w / 2; x++) {
        for (int y = h / 2; y < h; y++) {
            int[] t = new int[1];
            t = image.getRaster().getPixel(x + (w / 2), y - (h / 2), t);
            quarters[x][y] = t[0];
        }
    }

    //wiartka trzecia w miejsce drugiej
    for (int x = w / 2; x < w; x++) {
        for (int y = 0; y < h / 2; y++) {
            int[] t = new int[1];
            t = image.getRaster().getPixel(x - (w / 2), y + (h / 2), t);
            quarters[x][y] = t[0];
        }
    }

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            int[] t = new int[1];
            t[0] = quarters[x][y];
            image.getRaster().setPixel(x, y, t);
        }
    }

    return image;
}

From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java

/**
 * Utility method for scaling thumbnails. Scales byte[] image by a scale factor.
 * Optionally crops images to square./*from  w  ww .  j  a v  a 2 s. c  o  m*/
 * @param src RenderedImage containing the input image
 * @param scale Scale amount
 * @param square Boolean flag to crop to a square image
 * @return RenderedImage containing the transformed image
 * @throws IOException
 */
public static BufferedImage scaleImage(BufferedImage image, double scale, boolean square) throws IOException {
    int sx = 0, sy = 0;
    int swidth = image.getWidth();
    int sheight = image.getHeight();

    if (square) {
        if (image.getHeight() > image.getWidth()) {
            sy = (int) ((image.getHeight() - image.getWidth()) / 2.0);
            sheight = swidth;
        } else if (image.getHeight() < image.getWidth()) {
            sx = (int) ((image.getWidth() - image.getHeight()) / 2.0);
            swidth = sheight;
        }
    }
    int width = (int) (swidth * scale);
    int height = (int) (sheight * scale);

    BufferedImage scaled = new BufferedImage(image.getColorModel(),
            image.getRaster().createCompatibleWritableRaster(width, height), image.isAlphaPremultiplied(),
            null);

    Graphics g = scaled.getGraphics();
    g.drawImage(image, 0, 0, width, height, sx, sy, sx + swidth, sy + sheight, null);
    g.dispose();

    return scaled;
}

From source file:edu.csun.ecs.cs.multitouchj.ui.control.Canvas.java

protected Image getBufferedImageImage(BufferedImage bufferedImage) {
    byte[] bytes = (byte[]) bufferedImage.getRaster().getDataElements(0, 0, bufferedImage.getWidth(),
            bufferedImage.getHeight(), null);

    return new Image(bufferedImage.getWidth(), bufferedImage.getHeight(), bytes, true);
}

From source file:com.occamlab.te.parsers.ImageParser.java

private static String checkTransparentNodata(BufferedImage buffImage, Node node) throws Exception {
    String transparentNodata = "NA";
    boolean noData = false;
    boolean transparent = true;
    int[] bandIndexes = new int[4];
    bandIndexes[0] = 3; // A
    bandIndexes[1] = 2; // B
    bandIndexes[2] = 1; // G
    bandIndexes[3] = 0; // R
    Raster raster = buffImage.getRaster();
    int minx = raster.getMinX();
    int maxx = minx + raster.getWidth();
    int miny = raster.getMinY();
    int maxy = miny + raster.getHeight();
    int bands[][] = new int[bandIndexes.length][raster.getWidth()];
    for (int y = miny; y < maxy; y++) {
        for (int i = 0; i < bandIndexes.length; i++) {
            raster.getSamples(minx, y, maxx, 1, bandIndexes[i], bands[i]);
        }//from   w ww  . j  av  a 2  s  . c  o  m
        for (int x = minx; x < maxx; x++) {
            int a = bands[0][x];
            int b = bands[1][x];
            int g = bands[2][x];
            int r = bands[3][x];
            if (b == 0 && g == 0 && r == 0) {
                noData = true;
                if (a != 0) {
                    transparent = false;
                }
            }
        }
    }
    transparentNodata = (noData) ? (transparent) ? "true" : "false" : "NA";
    return transparentNodata;
}

From source file:RasterImageTest.java

/**
 * Makes the Mandelbrot image.//w  w  w  . ja  v  a2  s  .co  m
 * @param width the width
 * @parah height the height
 * @return the image
 */
public BufferedImage makeMandelbrot(int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = image.getRaster();
    ColorModel model = image.getColorModel();

    Color fractalColor = Color.red;
    int argb = fractalColor.getRGB();
    Object colorData = model.getDataElements(argb, null);

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            double a = XMIN + i * (XMAX - XMIN) / width;
            double b = YMIN + j * (YMAX - YMIN) / height;
            if (!escapesToInfinity(a, b))
                raster.setDataElements(i, j, colorData);
        }
    return image;
}

From source file:client.ServiceRequester.java

public HttpEntity<byte[]> createRequestHeaders(BufferedImage image) {
    byte[] imageBytes = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();

    headers.add("imageType", String.valueOf(image.getType()));
    headers.add("imageWidth", String.valueOf(image.getWidth()));
    headers.add("imageHeight", String.valueOf(image.getHeight()));

    return new HttpEntity<byte[]>(imageBytes, headers);
}

From source file:org.jboss.test.selenium.ScreenshotInterceptor.java

public String calculateHash(BufferedImage image) {
    WritableRaster raster = image.getRaster();
    DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
    messageDigest.update(buffer.getData());
    return new BigInteger(1, messageDigest.digest()).toString(16);
}

From source file:org.springframework.cloud.stream.app.pose.estimation.processor.PoseEstimationTensorflowInputConverter.java

private int[] toIntArray(BufferedImage image) {
    BufferedImage imgToRecognition = GraphicsUtils.toBufferedImage(image);
    return ((DataBufferInt) imgToRecognition.getRaster().getDataBuffer()).getData();
}

From source file:brut.androlib.res.decoder.Res9patchStreamDecoder.java

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {/*from ww w. j  a  v a  2  s  . c om*/
        byte[] data = IOUtils.toByteArray(in);

        BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
        int w = im.getWidth(), h = im.getHeight();

        ImageTypeSpecifier its = ImageTypeSpecifier.createFromRenderedImage(im);
        BufferedImage im2 = its.createBufferedImage(w + 2, h + 2);

        im2.getRaster().setRect(1, 1, im.getRaster());
        NinePatch np = getNinePatch(data);
        drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
        drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);

        int[] xDivs = np.xDivs;
        for (int i = 0; i < xDivs.length; i += 2) {
            drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
        }

        int[] yDivs = np.yDivs;
        for (int i = 0; i < yDivs.length; i += 2) {
            drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
        }

        ImageIO.write(im2, "png", out);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    } catch (NullPointerException ex) {
        // In my case this was triggered because a .png file was
        // containing a html document instead of an image.
        // This could be more verbose and try to MIME ?
        throw new AndrolibException(ex);
    }
}