有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java读取图像像素时,像素Alpha始终为255

在下面的代码示例中,我将图像像素Alpha值设置为70,但当我再次提取它时,得到255。我是提取错误还是设置错误?我遗漏了什么或不理解什么

public void createImage()
{


    BufferedImage img = new BufferedImage(2, 2,
            BufferedImage.TYPE_INT_RGB);

    //Set Pixel 

    int red = 50;
    int green = 10;
    int blue = 100;
    int alpha = 70;


    int col = (alpha << 24) | (red << 16) | (green << 8) | blue;

    img.setRGB(0, 0, col);  //Set pixel 0,0


    //Read Pixel

    int colint = img.getRGB(0, 0); //Get pixel 0,0


    Color newCol = new Color(colint,true);

    int alphaToPrint = newCol.getAlpha();
    int redToPrint = newCol.getRed();
    int greenToPrint = newCol.getGreen();
    int blueToPrint = newCol.getBlue();


    System.out.println("redToPrint :" +String.valueOf(redToPrint));
    System.out.println("greenToPrint :" +String.valueOf(greenToPrint));
    System.out.println("blueToPrint :" +String.valueOf(blueToPrint));
    System.out.println("alphaToPrint :" +String.valueOf(alphaToPrint));



}

运行代码时的结果:

enter image description here

我期望在读取alpha值时得到70:

int alphaToPrint = newCol.getAlpha();

请帮忙


共 (1) 个答案

  1. # 1 楼答案

    你的BufferedImage没有阿尔法通道(TYPE_INT_RGB

    改用BufferedImage.TYPE_INT_ARGB(注意“A”)