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.