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

时间:2016-05-11 13:23:55

标签: java

在下面的代码示例中,我将Image像素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 个答案:

答案 0 :(得分:3)

您的<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.elyeproj.phoneinfo.MainActivity"> <TextView android:id="@+id/resolution_width" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/resolution_height" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/resolution_width_dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/resolution_height_dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/resolution_density" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/resolution_density_str" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/phone_os" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="10dp" /> <Button android:layout_width="match_parent" android:layout_height="20dp" /> <Button android:layout_width="match_parent" android:layout_height="30dp" /> <Button android:layout_width="match_parent" android:layout_height="40dp" /> <Button android:layout_width="match_parent" android:layout_height="50dp" /> <ImageView android:layout_width="match_parent" android:layout_height="10dp" android:layout_marginBottom="5dp" android:background="@android:color/holo_blue_dark"/> <ImageView android:layout_width="match_parent" android:layout_height="20dp" android:layout_marginBottom="5dp" android:background="@android:color/holo_blue_dark"/> <ImageView android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginBottom="5dp" android:background="@android:color/holo_blue_dark"/> <ImageView android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginBottom="5dp" android:background="@android:color/holo_blue_dark"/> <ImageView android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/holo_blue_dark"/> </LinearLayout> 没有Alpha通道BufferedImage

改为使用TYPE_INT_RGB(注意'A')

相关问题