I have a PNG file with transparency that is loaded and stored in a BufferedImage
. I need this BufferedImage
to be of TYPE_INT_ARGB
. However, when I use getType()
the returned value is 0 (TYPE_CUSTOM
) instead of 2 (TYPE_INT_ARGB
).
This is how I load the .png
:
public File img = new File("imagen.png");
public BufferedImage buffImg =
new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);
try {
buffImg = ImageIO.read(img );
}
catch (IOException e) { }
System.out.Println(buffImg.getType()); //Prints 0 instead of 2
How can I load the .png, save in the BufferedImage
and make it TYPE_INT_ARGB
?
List, the difference between Image and BufferedImage is the same as the difference between List and LinkedList. Image is a generic concept and BufferedImage is the concrete implementation of the generic concept; kind of like BMW is a make of a Car. Show activity on this post. Image is an abstract class.
imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP. Image I/O is also extensible so that developers or administrators can "plug-in" support for additional formats. For example, plug-ins for TIFF and JPEG 2000 are separately available.
A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).
BufferedImage in = ImageIO.read(img);
BufferedImage newImage = new BufferedImage(
in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(in, 0, 0, null);
g.dispose();
try {
File img = new File("somefile.png");
BufferedImage image = ImageIO.read(img );
System.out.println(image);
} catch (IOException e) {
e.printStackTrace();
}
Example output for my image file:
BufferedImage@5d391d: type = 5 ColorModel: #pixelBits = 24
numComponents = 3 color
space = java.awt.color.ICC_ColorSpace@50a649
transparency = 1
has alpha = false
isAlphaPre = false
ByteInterleavedRaster:
width = 800
height = 600
#numDataElements 3
dataOff[0] = 2
You can run System.out.println(object); on just about any object and get some information about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With