Java Buforedimage staje się czerwony, zielony i niebieski indywidualnie

Metoda getRGB Zwraca pojedynczy int. Jak Mogę uzyskać kolory czerwony, zielony i niebieski jako wartości od 0 do 255?

Author: deltanovember, 2010-04-11

4 answers

Klasa Java Color może wykonać konwersję:

Color c = new Color(image.getRGB());
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
 70
Author: Michael Mrozek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-11 00:05:57

Piksel jest reprezentowany przez 4-bajtową (32-bitową) liczbę całkowitą.]}

00000000 00000000 00000000 11111111
^ Alpha  ^Red     ^Green   ^Blue

Więc, aby uzyskać poszczególne składniki koloru, wystarczy trochę arytmetyki binarnej:

int rgb = getRGB(...);
int red = (rgb >> 16) & 0x000000FF;
int green = (rgb >>8 ) & 0x000000FF;
int blue = (rgb) & 0x000000FF;

To jest rzeczywiście to, co java.awt.Color metody klasy do:

  553       /**
  554        * Returns the red component in the range 0-255 in the default sRGB
  555        * space.
  556        * @return the red component.
  557        * @see #getRGB
  558        */
  559       public int getRed() {
  560           return (getRGB() >> 16) & 0xFF;
  561       }
  562   
  563       /**
  564        * Returns the green component in the range 0-255 in the default sRGB
  565        * space.
  566        * @return the green component.
  567        * @see #getRGB
  568        */
  569       public int getGreen() {
  570           return (getRGB() >> 8) & 0xFF;
  571       }
  572   
  573       /**
  574        * Returns the blue component in the range 0-255 in the default sRGB
  575        * space.
  576        * @return the blue component.
  577        * @see #getRGB
  578        */
  579       public int getBlue() {
  580           return (getRGB() >> 0) & 0xFF;
  581       }
 110
Author: João Silva,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-11 00:17:55

Będziesz potrzebował podstawowej arytmetyki binarnej, aby ją podzielić:

int blue = rgb & 0xFF;
int green = (rgb >> 8) & 0xFF;
int red = (rgb >> 16) & 0xFF;

(A może odwrotnie, szczerze nie pamiętam, a dokumentacja nie daje mi natychmiastowej odpowiedzi)

 8
Author: Matti Virkkunen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-11 00:06:18

Do prostych manipulacji kolorami można użyć

bufImg.getRaster().getPixel(x,y,outputChannels)

OutputChannels jest tablicą do przechowywania pobranego piksela. Jego długość zależy od rzeczywistej liczby kanałów obrazu. Na przykład obraz RGB ma 3 kanały, a obraz RGBA ma 4 kanały.

Ta metoda ma 3 typy wyjściowe: int, float i double. Aby uzyskać wartość koloru w zakresie od 0~255, rzeczywiste parametry outputChannels powinny być tablicą int [].

 6
Author: Defd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-11 03:25:51