Java Utililty Methods BufferedImage to RGB

List of utility methods to do BufferedImage to RGB

Description

The list of methods to do BufferedImage to RGB are organized into topic(s).

Method

int[]getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels)
A convenience method for getting ARGB pixels from an image.
int type = image.getType();
if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
    return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels);
return image.getRGB(x, y, width, height, pixels, 0, width);
int[]getRGB(BufferedImage img)
get RGB
int[] rgb = new int[img.getWidth() * img.getHeight()];
img.getRGB(0, 0, img.getWidth(), img.getHeight(), rgb, 0, img.getWidth());
return rgb;
ListgetRgbFromImage(BufferedImage image)
Get RGB data array from given image
List<int[][]> rgb = new ArrayList<int[][]>();
int[][] r = null;
int[][] g = null;
int[][] b = null;
int width = 0;
int height = 0;
width = image.getWidth();
height = image.getHeight();
...
int[][]getRGBPixels(BufferedImage img)
Returns all the pixels of the image as an int array (row-wise) with the RGB(A) components as second dimension.
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++) {
...
int[][]imageToMatrix(BufferedImage image)
image To Matrix
int w = image.getWidth();
int h = image.getHeight();
int[][] imageMatrix = new int[w][h];
for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
        imageMatrix[i][j] = image.getRGB(i, j);
return imageMatrix;
int[][]imageToPixels(BufferedImage image)
image To Pixels
if (image == null) {
    return null;
int w = image.getWidth();
int h = image.getHeight();
int[][] pixels = new int[w][h];
Raster raster = image.getRaster();
if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
...
ByteBufferimageToRGBABuffer(BufferedImage img)
image To RGBA Buffer
ByteBuffer buf = null;
buf = ByteBuffer.allocateDirect(4 * img.getWidth() * img.getHeight());
byte[] pixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
boolean hasAlpha = img.getAlphaRaster() != null;
for (int pixel = 0; pixel < pixels.length; pixel += (hasAlpha ? 4 : 3)) {
    int offset = hasAlpha ? 1 : 0;
    buf.put(pixels[pixel + 2 + offset]);
    buf.put(pixels[pixel + 1 + offset]);
...
BufferedImageimageToRgbaBufferedImage(final Image image)
Convert Image to BufferedImage.
final BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_4BYTE_ABGR);
final Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;