bufferedImage.getRGB(x, y) does not yield alpha

17,476

Solution 1

The single-parameter Color constructor you're using discards alpha information. Use the two-parameter version instead and pass in true for hasalpha:

Color c = new Color(i.getRGB(x, y), true);

The relevant Javadoc:

Color(int rgb)

Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color(int rgba, boolean hasalpha)

Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Solution 2

The Alpha is not lost.

You must use either the (int,boolean) constructor which takes the pixel information and specify if it has alpha values with the boolean or the constructor that takes 4 values, Red,Green,Blue, and Alpha.

the (int,int,int,int) constructor information from the JavaDoc

To shorten code, you can always replace all of the following code with Color color = new Color(i.getRGB(x, y), true); which tells the color constructor that the pixel information does contain Alpha value.

the (int,boolean) constructor information from the JavaDoc

Note that sometimes when retrieving alpha the following way might return -1, in which case this means it loops back to 255, so you must perform 256-1 to get the actual alpha value. this snippet demonstrates how to load an image and get the color of that image on certain coordinates, in this case, 0,0. The following example can show you how to retrieve each color value including alpha, and reconstructing it to a new Color.

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

public class main {

    public static void main(String [] args){
        BufferedImage image = null;
        try {
            image = ImageIO.read(new URL("image.png"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        int pixel = image.getRGB(0, 0);

        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel >> 0) & 0xff;

        Color color1 = new Color(red, green, blue, alpha);

        //Or use
        Color color2 = new Color(image .getRGB(0, 0), true);

    }
   }

BufferedImages getRGB(int,int) JavaDoc as you can see as it says:

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace. Color conversion takes place if this default model does not match the image ColorModel. There are only 8-bits of precision for each color component in the returned data when using this method.

which means the Alpha value is also retrieved.

Share:
17,476
Rapti
Author by

Rapti

SOreadytohelp

Updated on June 09, 2022

Comments

  • Rapti
    Rapti almost 2 years

    I've got a BufferedImage i and I'd like to get the Color from a certain pixel of that image including the alpha value. The pixel is identified using x and y coordinates.

    Here's what I tried:

    Color c = new Color(i.getRGB(x, y));
    

    For some reason, the new color object contains the correct RGB but the alpha gets lost.

    What am I doing wrong?

    Thanks in advance