[英]Is there an equivalence of MapRGB for a BufferedImage in Java?
I'm trying to color individual pixels in a BufferedImage
(TYPE_INT_RGB) using setRGB()
, but I'm not sure how to format the RGB values. 我正在尝试使用
setRGB()
为BufferedImage
(TYPE_INT_RGB)中的单个像素着色,但是我不确定如何格式化RGB值。 I want the result as a single integer. 我希望结果是一个整数。 Is there a method that will take three
int
values (red, green, blue) and return a correctly formatted integer for setRGB()
? 有没有一种方法将采用三个
int
值(红色,绿色,蓝色)并为setRGB()
返回正确格式的整数?
new Color(red, green, blue).getRGB();
Assuming you have ints r
, g
, and b
, you can do: 假设您有整数
r
, g
和b
,则可以执行以下操作:
int pixel = (r << 16) | (g << 8) | b;
This is because pixels in a BufferedImage
are 4-byte ints. 这是因为
BufferedImage
中的像素为4字节int。 The 4-bytes represent Alpha, Red, Green, and Blue, in that order. 4个字节依次表示Alpha,Red,Green和Blue。 So, if you shift red left by two bytes and green left by one byte, then bitwise-or
r
, g
, and b
, you will get a valid pixel to use with setRGB()
. 因此,如果将红色左移两个字节,将绿色左移一个字节,然后按位或
r
, g
和b
,将获得一个有效像素,可与setRGB()
一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.