简体   繁体   English

在JAVA中将像素从int转换为无符号字节

[英]Converting Pixels from int to unsigned byte in JAVA

Im trying to convert the pixels that i get from the getRGB function of a black and white image to unsigned byte, I have read and, correct if I am wrong, the getRGB function in java returns integer values, so what I want to do is to see values from 0-255 not the negative integer values Im getting with the function. 我试图将我从黑白图像的getRGB函数获得的像素转换为无符号字节,我已阅读,并且如果我错了,请更正,java中的getRGB函数返回整数值,所以我要做的是看到0-255之间的值,而不是该函数得到的负整数Im。 I have tried this with no sucess: 我没有成功尝试过:

   int width;
   int height;
   byte extract;
   int asign;
   int matrix[][];

 for ( i = 0; i < height; i++)
     {
        for ( j = 0; j < width; j++) 
        {
            extract =(byte)(xray.getRGB(j, i));
            asign = (int)extract;
            //asign = asign & 0xff;
            asign = asign + 128;
            matrix[i][j] = asign;


        }
     }

Notice that xray is a buferred image. 请注意,x射线是有缓冲的图像。 Hope you can help me with this issue guys. 希望您能帮我解决这个问题。 As you can see I've tried with mask, also I 've tried trying to add 128 to the value after the cast. 如您所见,我尝试使用mask,也尝试将强制转换后的值加128。

Well, getRGB() returns a signed int , but it's a 32-bit int representing the alpha and R, G, B values. 好吧, getRGB()返回一个有符号的int ,但这是一个32位int表示alpha和R,G,B值。 The most significant byte is alpha, and if it's fully opaque then that will be 255, which means you'll end up with a negative int overall. 最高有效字节是alpha,如果它是完全不透明的,则为255,这意味着您最终将得到一个负int

If you want to mask out the alpha value, you can do it with 如果要掩盖alpha值,可以使用

matrix[i][j] = xray.getRGB(j,i) & 0xFFFFFF;

which will get rid of the high-order byte. 这将摆脱高位字节。 This will leave you with a single positive value containing the RGB values for the pixel. 这将使您剩下一个包含像素RGB值的正值。

Careful what you do with it afterwards: if you then try to use it to set the colour of a pixel later, you might end up setting the alpha value to 0 (now that you've masked it off), and so you'll end up with something fully transparent (aka invisible). 以后要小心处理:如果以后尝试使用它来设置像素的颜色,则最终可能会将alpha值设置为0(现在已经将其屏蔽掉了),因此您将最终得到完全透明的东西(又名不可见)。

If it's a black and white image and for some reason you're getting just values from -128 to 127, then more or less the same trick applies to make it unsigned. 如果是黑白图像,并且由于某种原因您只能得到-128到127之间的值,那么或多或少都可以使用相同的技巧来使它变为无符号。 In fact exactly the same line as above would do it, but you could also shorten the mask: 实际上,可以使用与上述完全相同的代码行,但是您也可以缩短掩码:

matrix[i][j] = xray.getRGB(j,i) & 0xFF;

This means you don't need any if - else in your code to get it working, and you don't need your extract or asign variables. 这意味着您不需要任何if -代码中的else也可以使它正常工作,并且您不需要extractasign变量。

Try this 尝试这个

    extract =(byte)(xray.getRGB(j, i));
    if(extract < 0){
       asign = extract + 256;  // Add the total number of values in byte if extract is negative
    }
    else{
       asign = extract;        // You don't need to type cast it to int explicitly
    }
    matrix[i][j] = asign;

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

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