Konwertuj wartości RGB na liczbę całkowitą

Więc w BufferedImage otrzymujesz pojedynczą liczbę całkowitą, która ma reprezentowane wartości RGB. Do tej pory używam następujących wartości RGB z niego:

// rgbs is an array of integers, every single integer represents the
// RGB values combined in some way
int r = (int) ((Math.pow(256,3) + rgbs[k]) / 65536);
int g = (int) (((Math.pow(256,3) + rgbs[k]) / 256 ) % 256 );
int b = (int) ((Math.pow(256,3) + rgbs[k]) % 256);
I jak na razie działa.

To, co muszę zrobić, to dowiedzieć się, jak uzyskać liczbę całkowitą, abym mógł użyć BufferedImage.setRGB(), ponieważ to wymaga tego samego typu danych, które mi dał.

Author: Marcus Mangelsdorf, 2011-01-26

5 answers

Myślę, że kod jest coś w stylu:

int rgb = red;
rgb = (rgb << 8) + green;
rgb = (rgb << 8) + blue;

Również, wierzę, że można uzyskać indywidualne wartości za pomocą:

int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
 95
Author: camickr,
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
2011-01-26 04:33:28
int rgb = ((r&0x0ff)<<16)|((g&0x0ff)<<8)|(b&0x0ff);

Jeśli wiesz, że twoje wartości r, g i b nigdy nie są > 255 lub

Additionaly

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

Nie ma potrzeby mnożenia.

 32
Author: KitsuneYMG,
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
2011-01-26 04:34:00

Jeśli r, g, b = 3 wartości całkowite od 0 do 255 dla każdego koloru

Then

rgb = 65536 * r + 256 * g + b;

Pojedyncza wartość rgb to złożona wartość r, g, b łącznie dla 16777216 możliwych odcieni.

 24
Author: Jay,
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
2011-05-11 20:03:37
int rgb = new Color(r, g, b).getRGB();
 21
Author: Nicholas,
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
2011-05-11 20:01:42

Aby uzyskać indywidualne wartości kolorów, możesz użyć koloru w następujący sposób dla pikseli(x,y).

import java.awt.Color;
import java.awt.image.BufferedImage;

Color c = new Color(buffOriginalImage.getRGB(x,y));
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();

Powyższe daje wartości całkowite Czerwony, zielony i niebieski w zakresie od 0 do 255.

Aby ustawić wartości z RGB można to zrobić przez:

Color myColour = new Color(red, green, blue);
int rgb = myColour.getRGB();

//Change the pixel at (x,y) ti rgb value
image.setRGB(x, y, rgb);

Należy pamiętać, że powyższe zmiany wartości pojedynczego piksela. Więc jeśli chcesz zmienić wartość całego obrazu, może być konieczne iterację nad obrazem za pomocą dwóch pętli for.

 5
Author: Ajay Sant,
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
2017-08-28 16:40:26