質問

I use class PixelGrabber to get a two-value bitmap's pixel array. But one problem comes out, a two-value image(white or black), I stored these pixels of the bitmap in a int array, these elements of the array shouldn't be 1(white) or 0(black) but in my code I find white is -1 and black is -16777216. Anyone know why?

Now I would like to just simply use a for loop to change -1 and -16777216 to 1 and 0.

役に立ちましたか?

解決

I'm assuming that you're using the PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize) constructor, in which case what you're getting back is an integer array of the RGB value of each pixel.

Because it's coming back as an RGB value - you need to seperate out the values to give you a true RGB value. For example RGB(255,255,255) is White and RGB(0,0,0) is black. The API Reference gives a good example of how to do this properly. With the -16777216 number in your question, I performed a simple test which shows that it is actually black:

public class Main {

    public static void main(String[] args) {
        int pixel = -16777216;

        int alpha = (pixel >> 24) & 0xff;
        int red   = (pixel >> 16) & 0xff;
        int green = (pixel >>  8) & 0xff;
        int blue  = (pixel      ) & 0xff;

        System.out.println(alpha);
        System.out.println(red);
        System.out.println(green);
        System.out.println(blue);


    }
}

Prints out: 255, 0, 0, 0

Follow the API reference linked above for example code of how to handle the pixel data correctly.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top