从文件创建缓冲图像并使其TYPE_INT_ARGB

我有一个具有透明度的PNG文件,该文件已加载并存储在.我需要这个是.但是,当我使用返回值时,返回值为0()而不是2()。BufferedImageBufferedImageTYPE_INT_ARGBgetType()TYPE_CUSTOMTYPE_INT_ARGB

这就是我加载的方式:.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

如何加载.png,保存并制作它?BufferedImageTYPE_INT_ARGB


答案 1
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();

答案 2
try {
    File img = new File("somefile.png");
    BufferedImage image = ImageIO.read(img ); 
    System.out.println(image);
} catch (IOException e) { 
    e.printStackTrace(); 
}

我的图像文件的示例输出:

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

你可以运行System.out.println(object);几乎任何对象,并获得有关它的一些信息。


推荐