Java BufferedImage to RGB getRGBPixels(BufferedImage img)

Here you can find the source of getRGBPixels(BufferedImage img)

Description

Returns all the pixels of the image as an int array (row-wise) with the RGB(A) components as second dimension.

License

Open Source License

Parameter

Parameter Description
img the image to get the pixels from

Return

the pixel array

Declaration

public static int[][] getRGBPixels(BufferedImage img) 

Method Source Code

//package com.java2s;
/*//from   w ww. j a  v  a2s .  c  o m
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Returns all the pixels of the image as an int array (row-wise) with the 
     * RGB(A) components as second dimension.
     *
     * @param img      the image to get the pixels from
     * @return      the pixel array
     * @see      BufferedImage#getRGB(int, int)
     */
    public static int[][] getRGBPixels(BufferedImage img) {
        int[][] result;
        int y;
        int x;
        int i;
        int pixel;

        result = new int[img.getWidth() * img.getHeight()][4];
        i = 0;
        for (y = 0; y < img.getHeight(); y++) {
            for (x = 0; x < img.getWidth(); x++) {
                pixel = img.getRGB(x, y);
                result[i][0] = (pixel >> 16) & 0xFF; // R
                result[i][1] = (pixel >> 8) & 0xFF; // G
                result[i][2] = (pixel >> 0) & 0xFF; // B
                result[i][3] = (pixel >> 24) & 0xFF; // A
                i++;
            }
        }

        return result;
    }
}

Related

  1. getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels)
  2. getRGB(BufferedImage img)
  3. getRgbFromImage(BufferedImage image)
  4. imageToMatrix(BufferedImage image)
  5. imageToPixels(BufferedImage image)
  6. imageToRGBABuffer(BufferedImage img)
  7. imageToRgbaBufferedImage(final Image image)