Java 从文件创建一个BuffereImage,并将其类型设置为\u INT\u ARGB

Java 从文件创建一个BuffereImage,并将其类型设置为\u INT\u ARGB,java,image,image-manipulation,bufferedimage,argb,Java,Image,Image Manipulation,Bufferedimage,Argb,我有一个透明的PNG文件,它被加载并存储在buffereImage中。我需要此buffereImage为类型\u INT\u ARGB。但是,当我使用getType()时,返回的值是0(TYPE\u CUSTOM),而不是2(TYPE\u INT\u ARGB) 这就是我加载.png的方式: public File img = new File("imagen.png"); public BufferedImage buffImg = new BufferedImage(240, 2

我有一个透明的PNG文件,它被加载并存储在
buffereImage
中。我需要此
buffereImage
类型\u INT\u ARGB
。但是,当我使用
getType()
时,返回的值是0(
TYPE\u CUSTOM
),而不是2(
TYPE\u INT\u 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,保存在
BufferedImage
中并使其
键入\u INT\u ARGB

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();
我的图像文件的输出示例:

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(对象);在任何对象上创建一个BuffereImage文件,并使其键入\u INT\u RGB

import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class Main{
    public static void main(String args[]){
        try{
            BufferedImage img = new BufferedImage( 
                500, 500, BufferedImage.TYPE_INT_RGB );
            File f = new File("MyFile.png");
            int r = 5;
            int g = 25; 
            int b = 255;
            int col = (r << 16) | (g << 8) | b;
            for(int x = 0; x < 500; x++){
                for(int y = 20; y < 300; y++){
                    img.setRGB(x, y, col);
                }
            }
            ImageIO.write(img, "PNG", f); 
        }
        catch(Exception e){ 
            e.printStackTrace();
        }
    }
}
import java.io.*;
导入java.awt.image.*;
导入javax.imageio.*;
公共班机{
公共静态void main(字符串参数[]){
试一试{
BuffereImage img=新的BuffereImage(
500,500,BufferedImage.TYPE_INT_RGB);
文件f=新文件(“MyFile.png”);
int r=5;
int g=25;
int b=255;

int col=(r将
public BufferedImage buffImg=new BufferedImage(240240,BufferedImage.TYPE_int_ARGB)
更改为
public BufferedImage buffImg;
捕获(IOException e){}
catch(IOException e){e.printStackTrace()}
。报告新的输出。
System.Out.Println
无法编译。为了更快地获得更好的帮助,请发布一个。这是非常低效的,并且随着图像大小的增加而变得越来越不可行。呃,@alexantd,为什么它如此低效?是的,它需要O(宽x高)内存,可以使用恒定内存完成,但它与任何其他方法具有相同的时间复杂性,任何其他方法都会非常混乱。@tucuxi由于读者首先创建正确类型的BuffereImage,因此它的效率低于根本不需要任何转换方法
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class Main{
    public static void main(String args[]){
        try{
            BufferedImage img = new BufferedImage( 
                500, 500, BufferedImage.TYPE_INT_RGB );
            File f = new File("MyFile.png");
            int r = 5;
            int g = 25; 
            int b = 255;
            int col = (r << 16) | (g << 8) | b;
            for(int x = 0; x < 500; x++){
                for(int y = 20; y < 300; y++){
                    img.setRGB(x, y, col);
                }
            }
            ImageIO.write(img, "PNG", f); 
        }
        catch(Exception e){ 
            e.printStackTrace();
        }
    }
}
    try{
        BufferedImage img = new BufferedImage( 
            500, 500, BufferedImage.TYPE_INT_ARGB );
        File f = new File("MyFile.png");
        int r = 255;
        int g = 10;
        int b = 57;
        int alpha = 255;
        int col = (alpha << 24) | (r << 16) | (g << 8) | b;
        for(int x = 0; x < 500; x++){
            for(int y = 20; y < 30; y++){
                img.setRGB(x, y, col);
            }
        }
        ImageIO.write(img, "PNG", f);
    }
    catch(Exception e){
        e.printStackTrace();
    }