简体   繁体   English

如何在Java中将getRGB(x,y)整数像素转换为Color(r,g,b,a)?

[英]How to convert getRGB(x,y) integer pixel to Color(r,g,b,a) in Java?

I have the integer pixel I got from getRGB(x,y) , but I don't have any clue about how to convert it to RGBA format. 我有从getRGB(x,y)获得的整数像素,但是我对如何将其转换为RGBA格式一无所知。 For example, -16726016 should be Color(0,200,0,255) . 例如-16726016应该是Color(0,200,0,255) Any tips? 有小费吗?

If I'm guessing right, what you get back is an unsigned integer of the form 0xAARRGGBB , so 如果我猜对了,您得到的是形式为0xAARRGGBB的无符号整数,因此

int b = (argb)&0xFF;
int g = (argb>>8)&0xFF;
int r = (argb>>16)&0xFF;
int a = (argb>>24)&0xFF;

would extract the color components. 将提取颜色分量。 However, a quick look at the docs says that you can just do 但是,快速浏览一下文档说,您可以

Color c = new Color(argb);

or 要么

Color c = new Color(argb, true);

if you want the alpha component in the Color as well. 如果您也想要颜色中的alpha分量。

UPDATE 更新

Red and Blue components are inverted in original answer, so the right answer will be: 红色和蓝色成分在原始答案中是相反的,因此正确答案将是:

int r = (argb>>16)&0xFF;
int g = (argb>>8)&0xFF;
int b = (argb>>0)&0xFF;

updated also in the first piece of code 在第一段代码中也进行了更新

    Color c = new Color(-16726016, true);
    System.out.println(c.getRed());
    System.out.println(c.getGreen());
    System.out.println(c.getBlue());
    System.out.println(c.getAlpha());

prints out: 打印出:

0
200
0
255

Is that what you mean? 你是这个意思吗?

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

相关问题 Java awt 中的 getRGB(x,y) 为每个像素返回相同的值 - getRGB(x,y) in Java awt returns for each pixel same value 如何使用image.getRGB(x,y)检查像素是否为黑色 - How to check if a pixel is black using image.getRGB(x, y) java:将十六进制颜色#RRGGBB 转换为 rgb rgb? - java : convert Hex color #RRGGBB to rgb r g b? Java Android:如何在 GLSurfaceView 的位置(X,Y)处获取像素颜色? - Java Android: how to get pixel color at position (X, Y) of GLSurfaceView? 如何使用 Java 在 Android 中获取颜色的 R、G、B 值? - How to get R,G,B values of a color in Android with Java? 将r,g,b值转换为一个像素值 - Convert r,g,b value to one pixel value java-image.getRGB(x,y)的二进制AND(&)操作; - java - Binary AND (&) operation for image.getRGB(x,y); 如何使用getRGB获取Java中图像的像素值数组 - How to get the array of pixel values for an image in java using getRGB 我需要了解java如何实现Color.RGBtoHSB(r,g,b,hsb)函数的详细信息。 他们是否将r,g,b归一化 - I need to know details of how java implements the function Color.RGBtoHSB(r, g, b, hsb). Does they normalize r,g,b 如何在JavaFX中将X,Y整数坐标转换为正确的像素坐标 - How to turn X, Y integer coordinates into the correct pixel coordinates in JavaFX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM