简体   繁体   English

确实与Java像素操作混淆

[英]Really confused with java pixel manipulation

THIS part i'm confused with what these 3 hexadecimals are doing such as 00ff0000, and why they're bitshifted 16 or 8 places 这部分我对这3个十六进制在做什么(例如00ff0000)以及为什么将它们移位16或8位感到困惑

 // Getting pixel color by position x=100 and y=40 
    int clr=  image.getRGB(100,40); 
    int  red   = (clr & 0x00ff0000) >> 16;
    int  green = (clr & 0x0000ff00) >> 8;
    int  blue  =  clr & 0x000000ff;

The pixel's color information is encoded in a single 32 bit integer. 像素的颜色信息以单个32位整数编码。 The lowest eight bits store the blue color information, bits 8 to 15 store green and 16 to 23 store red. 最低的8位存储蓝色信息,位8至15存储绿色,而16至23存储红色。 Bits 24 to 31 store the alpha value. 位24至31存储alpha值。 The code that you show first selects the right bits by masking them using the and operation. 您首先显示的代码通过使用和操作将其屏蔽来选择正确的位。 In order to do calculations with them they are moved to represent their actual values. 为了对其进行计算,将它们移动以代表其实际值。

clr & 0x0000ff00

selects the bits at position 8 to 15, 选择位置8至15的位

(clr & 0x0000ff00) >> 8

moves the result by 8 positions to the right. 将结果向右移动8个位置。

In

int  red   = (clr & 0x00ff0000) >> 16;

The & will zero out all bits except those that are wanted: &将将除所需位以外的所有位清零:

0x00123456 & 0x00ff0000 == 0x00120000

The bit shift will place these bits at the desired position: 位移会将这些位放置在所需位置:

0x00120000 >> 16 == 0x00000012 == 0x12

Similarly for the other two channels. 对于其他两个通道类似。

0x00123456 & 0x0000ff00 == 0x00003400
0x00003400 >> 16 == 0x34

0x00123456 & 0x000000ff == 0x56

The reason for this is that the ARGB format stuffs four bytes (alpha, red, green, blue) into one int: 0xAaRrGgBb . 原因是ARGB格式将四个字节(alpha,红色,绿色,蓝色) 0xAaRrGgBb为一个int: 0xAaRrGgBb The RGB format is similar except it doesn't use the alpha (opacity) channel. RGB格式相似,但不使用alpha(不透明度)通道。 The whole point behind the bit-shifting is to separate those bytes: clr == 0x123456 to red == 0x12 green == 0x34 blue == 0x56 移位后的全部要点是将这些字节分开: clr == 0x123456red == 0x12 green == 0x34 blue == 0x56

Note that each byte (8 bits) is represented by two hexadecimal digits (4 bits each) in the hexadecimal notation, so shifting by 16 bits shifts by 4*4 bits = 4 hexadecimal digits = 2 bytes. 请注意,每个字节(8位)用两个十六进制数字(每个4位)表示,因此移位16位将移位4 * 4位= 4个十六进制数字= 2个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM