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

private static ByteBuffer convertImageData(BufferedImage bufferedImage) {
    ByteBuffer imageBuffer;/*  ww w .  j av a 2  s. co m*/
    WritableRaster raster;
    BufferedImage texImage;

    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>());
    } else {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 3, null);
        texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>());
    }

    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    g.drawImage(bufferedImage, 0, 0, null);

    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();

    return imageBuffer;
}

From source file:exemplos.PegandoPixelsSemJAI.java

public void pegaPixel() throws IOException {

    File f = new File("D:\\ProjetosNetBeans\\PDI\\src\\imagens\\ebola.png"); //seleciona o arquivo
    BufferedImage image = ImageIO.read(f); //le o arquivo
    System.out.println("Dimenses: " + image.getWidth() + "x" + image.getHeight() + "pixels"); //exibe suas dimenses

    Raster raster = image.getRaster(); //pega os valores dos pixels
    double pixel[] = new double[3]; //cria um array com 3 posies
    int brancos = 0; //cria uma varivel para contar os pixels brancos
    for (int h = 0; h < image.getHeight(); h++)
        for (int w = 0; w < image.getWidth(); w++) {
            raster.getPixel(w, h, pixel);
            if ((pixel[0] == 255) && (pixel[1] == 255) && (pixel[2] == 255))//confirma se o RGB  igual a 255, ou seja, branco
                brancos++;/*  ww  w .j a  v  a2s.c  o m*/

        }

}

From source file:Java2DExample.java

public BufferedImage processImage(BufferedImage image) {
    float[][] colorMatrix = { { 1f, 0f, 0f }, { 0.5f, 1.0f, 0.5f }, { 0.2f, 0.4f, 0.6f } };
    BandCombineOp changeColors = new BandCombineOp(colorMatrix, null);
    Raster sourceRaster = image.getRaster();
    WritableRaster displayRaster = sourceRaster.createCompatibleWritableRaster();
    changeColors.filter(sourceRaster, displayRaster);
    return new BufferedImage(image.getColorModel(), displayRaster, true, null);

}

From source file:com.dianping.imcaptcha.strategy.WaveFilter.java

public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();

    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);/*from www  . j  a va  2 s.  c o  m*/

    if (dst == null) {
        ColorModel dstCM = src.getColorModel();
        dst = new BufferedImage(dstCM,
                dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height),
                dstCM.isAlphaPremultiplied(), null);
    }
    WritableRaster dstRaster = dst.getRaster();

    int[] inPixels = getRGB(src, 0, 0, width, height, null);

    if (interpolation == NEAREST_NEIGHBOUR)
        return filterPixelsNN(dst, width, height, inPixels, transformedSpace);

    int srcWidth = width;
    int srcHeight = height;
    int srcWidth1 = width - 1;
    int srcHeight1 = height - 1;
    int outWidth = transformedSpace.width;
    int outHeight = transformedSpace.height;
    int outX, outY;
    int index = 0;
    int[] outPixels = new int[outWidth];
    float radius = srcHeight * 1.0f / 2 / (float) Math.PI;
    outX = transformedSpace.x;
    outY = transformedSpace.y;
    float[] out = new float[2];

    for (int y = 0; y < outHeight; y++) {
        for (int x = 0; x < outWidth; x++) {
            transformInverse(outX + x, outY + y, out, radius);
            int srcX = (int) Math.floor(out[0]);
            int srcY = (int) Math.floor(out[1]);
            float xWeight = out[0] - srcX;
            float yWeight = out[1] - srcY;
            int nw, ne, sw, se;

            if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) {
                // Easy case, all corners are in the image
                int i = srcWidth * srcY + srcX;
                nw = inPixels[i];
                ne = inPixels[i + 1];
                sw = inPixels[i + srcWidth];
                se = inPixels[i + srcWidth + 1];
            } else {
                // Some of the corners are off the image
                nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight);
                ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight);
                sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight);
                se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight);
            }
            outPixels[x] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
        }
        setRGB(dst, 0, y, transformedSpace.width, 1, outPixels);
    }
    return dst;
}

From source file:br.gov.jfrj.itextpdf.Documento.java

public static java.awt.Image createQRCodeImage(String url) {
    Qrcode x = new Qrcode();

    x.setQrcodeErrorCorrect('M'); // 15%
    x.setQrcodeEncodeMode('B'); // Bynary
    boolean[][] matrix = x.calQrcode(url.getBytes());

    // Canvas canvas = new Canvas();
    // java.awt.Image img = canvas.createImage(matrix.length,
    // matrix.length);
    // Graphics g = img.getGraphics();
    // g.setColor(Color.BLACK);
    // img.getGraphics().clearRect(0, 0, matrix.length, matrix.length);
    byte ab[] = new byte[matrix.length * matrix.length];
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            if (matrix[j][i]) {
                // img.getGraphics().drawLine(j, i, j, i);
                ab[i * matrix.length + j] = 0;
            } else {
                ab[i * matrix.length + j] = -1;
            }/*from   ww  w  .j ava  2 s. co m*/
        }
    }
    BufferedImage img = new BufferedImage(matrix.length, matrix.length, BufferedImage.TYPE_BYTE_GRAY);
    WritableRaster wr = img.getRaster();
    wr.setDataElements(0, 0, matrix.length, matrix.length, ab);

    // buffered_image.setRGB (0, 0, matrix.length, matrix.length, ab, 0,
    // matrix.length);

    // java.awt.Image img = Toolkit.getDefaultToolkit().createImage(ab,
    // matrix.length, matrix.length);

    return img;
}

From source file:javafx1.Testpics.java

private void bildSchleife() {
    File f = new File("F:/NetBeansProjekte/pictures");
    File[] fileArray = f.listFiles();
    java.util.Arrays.sort(fileArray);
    for (File file : fileArray) {
        try {/*from ww  w  .java  2s  .  c om*/
            System.out.println("file: " + file.getCanonicalPath());
            File outputfile = new File(file.getCanonicalPath() + ".jpg");
            BufferedImage bi = ImageIO.read(file);
            System.out.println(" enc" + encodeToString(bi, "jpg"));
            ImageIO.write(bi, "jpg", outputfile);
            bi = ImageIO.read(outputfile);
            System.out.println(" w" + bi.getWidth() + " h" + bi.getHeight());
            byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
            // System.out.println(new String(data));
            byte[] x = Base64.encodeBase64URLSafe(data);
            System.out.println("x" + new String(x));
            String b64 = Base64.encodeBase64String(data);
            System.out.println("64" + b64);

            //                byte[] backToBytes = Base64.decodeBase64(base64String);
            //                InputStream in = new ByteArrayInputStream(backToBytes);
            //                BufferedImage bi;
            //                bi = ImageIO.read(in);
            //byte[] xx = base64String.getBytes(StandardCharsets.UTF_8);
            // bild, wie es von http kommt
            //                RunPic rp = new RunPic(this, data);
            //                Platform.runLater(rp);
            try {
                Thread.sleep(50);
                break;
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaFX1.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (IOException ex) {
            Logger.getLogger(JavaFX1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:qupath.lib.gui.panels.classify.RandomTrainingRegionSelector.java

public static Map<Integer, List<PathObject>> objectClusterer(final Collection<PathObject> pathObjects,
        final BufferedImage imgThumbnail, final double thumbScaleX, final double thumbScaleY,
        final int nClusters) {

    Map<Integer, List<PathObject>> map = new HashMap<>();
    if (pathObjects.isEmpty())
        return map;

    if (nClusters <= 1 || pathObjects.size() == 1) {
        map.put(Integer.valueOf(0), new ArrayList<>(pathObjects));
        return map;
    }/*ww  w.  j  av a 2  s . c  o m*/

    //      int maxIterations = 100;

    KMeansPlusPlusClusterer<ClusterableObject> km = new KMeansPlusPlusClusterer<>(nClusters);
    List<ClusterableObject> clusterableObjects = new ArrayList<>();
    WritableRaster raster = imgThumbnail.getRaster();
    int nChannels = raster.getNumBands();
    double[] valueBuffer = new double[nChannels];
    int w = imgThumbnail.getWidth();
    int h = imgThumbnail.getHeight();
    boolean isRGB = imgThumbnail.getSampleModel().getNumBands() == 3
            && imgThumbnail.getSampleModel().getSampleSize(0) == 8;

    for (PathObject pathObject : pathObjects) {
        // Get pixel values for the ROI centroid
        // CIE LAB is used rather than RGB where possible, due to better suitability for Euclidean distances
        ROI roi = pathObject.getROI();
        if (roi == null)
            continue;
        int x = (int) (roi.getCentroidX() * thumbScaleX + 0.5);
        int y = (int) (roi.getCentroidY() * thumbScaleY + 0.5);
        if (x < 0 || x >= w || y < 0 || y >= h)
            continue;

        if (isRGB)
            valueBuffer = makeCIELAB(imgThumbnail.getRGB(x, y), valueBuffer);
        else {
            for (int c = 0; c < nChannels; c++)
                valueBuffer[c] = raster.getSampleDouble(x, y, c);
        }

        clusterableObjects.add(new ClusterableObject(pathObject, valueBuffer));
    }
    List<CentroidCluster<ClusterableObject>> results = km.cluster(clusterableObjects);

    int i = 0;
    for (CentroidCluster<ClusterableObject> centroidCluster : results) {
        Integer label = Integer.valueOf(i);
        List<PathObject> objects = new ArrayList<>();
        for (ClusterableObject co : centroidCluster.getPoints())
            objects.add(co.getPathObject());
        map.put(label, objects);
        i++;
    }

    return map;
}

From source file:ImageOpByRomain.java

/**
 * <p>//from  w w  w.  ja  v  a2 s .co m
 * Writes a rectangular area of pixels in the destination
 * <code>BufferedImage</code>. Calling this method on an image of type
 * different from <code>BufferedImage.TYPE_INT_ARGB</code> and
 * <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.
 * </p>
 * 
 * @param img
 *            the destination image
 * @param x
 *            the x location at which to start storing pixels
 * @param y
 *            the y location at which to start storing pixels
 * @param w
 *            the width of the rectangle of pixels to store
 * @param h
 *            the height of the rectangle of pixels to store
 * @param pixels
 *            an array of pixels, stored as integers
 * @throws IllegalArgumentException
 *             is <code>pixels</code> is non-null and of length &lt; w*h
 */
public static void setPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" + " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}

From source file:org.apache.batik.transcoder.image.AbstractImageTranscoderTest.java

protected BufferedImage getImage(InputStream is) throws IOException {
    ImageTagRegistry reg = ImageTagRegistry.getRegistry();
    Filter filt = reg.readStream(is);
    if (filt == null)
        throw new IOException("Couldn't read Stream");

    RenderedImage red = filt.createDefaultRendering();
    if (red == null)
        throw new IOException("Couldn't render Stream");

    BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);
    red.copyData(img.getRaster());
    return img;/* ww  w  .  j av a 2 s. co  m*/
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage invert(BufferedImage inImg) {
    if (inImg == null) {
        return null;
    }/* ww w.ja v  a2 s.  c  o m*/

    int width = inImg.getWidth();
    int height = inImg.getHeight();
    BufferedImage outImg = new BufferedImage(width, height, inImg.getType());
    WritableRaster outRaster = outImg.getRaster();
    WritableRaster inRaster = inImg.getRaster();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            for (int i = 0; i < outRaster.getNumBands(); i++) {
                outRaster.setSample(x, y, i, 255 - inRaster.getSample(x, y, i));
            }
        }
    }

    return outImg;
}