【问题标题】:Create a BufferedImage from file and make it TYPE_INT_ARGB从文件创建一个 BufferedImage 并将其设为 TYPE_INT_ARGB
【发布时间】:2012-05-10 15:07:56
【问题描述】:

我有一个透明的 PNG 文件,它被加载并存储在 BufferedImage 中。我需要这个BufferedImageTYPE_INT_ARGB。但是,当我使用 getType() 时,返回值为 0 (TYPE_CUSTOM) 而不是 2 (TYPE_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,保存在 BufferedImage 并使其成为 TYPE_INT_ARGB

【问题讨论】:

  • public BufferedImage buffImg = new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB); 更改为public BufferedImage buffImg;catch (IOException e) { } 更改为catch (IOException e) { e.printStackTrace(); }。报告新的输出。
  • System.Out.Println 那将无法编译。为了尽快获得更好的帮助,请发布SSCCE

标签: java image image-manipulation bufferedimage argb


【解决方案1】:

从文件创建一个 BufferedImage 并将其设为 TYPE_INT_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();
        }
    }
}

这会在顶部画出一条大的蓝色条纹。

如果你想要 ARGB,可以这样操作:

    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();
    }

打开 MyFile.png,顶部有一条红色条纹。

【讨论】:

    【解决方案2】:
    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();
    

    【讨论】:

    • 这是非常低效的,并且随着图像大小的增加变得越来越不可行。
    • 呃,@alexantd,为什么效率这么低?是的,它需要 O(width x height) 内存,并且可以使用恒定内存来完成,但它与任何其他方法具有相同的时间复杂度,并且任何其他方法都会更多很多凌乱。
    • @tucuxi 效率低于根本不需要任何转换方法,因为读者首先创建了正确类型的 BufferedImage
    【解决方案3】:
    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);在几乎任何对象上并获取有关它的一些信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 2013-08-08
      • 2017-06-03
      • 1970-01-01
      • 2018-04-29
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多