简体   繁体   English

如何使用Java获取RGB值

[英]How to get RGB values using Java

I'm trying to get the pixels in RGB format of an image. 我正在尝试获取图像的RGB格式的像素。 I have the following code: 我有以下代码:

public void writeColorImageValueToFile(BufferedImage in, String fileName)   
    {   
    int width = in.getWidth();   
    int height = in.getHeight();   

    try   
    {   
        FileWriter fstream = new FileWriter(fileName + ".txt");   
        BufferedWriter out = new BufferedWriter(fstream);   

        int [] data = new int[width * height];      
        in.getRGB(0, 0, width, height, data, 0, width);   

        for(int i = 0; i < (height * width); i++)   
        {                        
            out.write(((data[i] >> 16) & 0xff) + ", ");    
            out.write(((data[i] >> 8) & 0xff) + ", ");   
            out.write((data[i] & 0xff) + "\n");   
        }   

        out.close();   
    } catch (Exception e)   
    {   
        System.err.println("Error: " + e.getMessage());   
    }   
}   

I created an image in photoshop and I filled it with the color RGB(86, 136, 152), however I got from my java method the value of RGB(44, 139, 154). 我在photoshop中创建了一个图像,并用RGB(86,136,152)填充了颜色,但是从java方法中我得到了RGB(44,139,154)的值。

I load the image using: 我使用以下方法加载图像:

BufferedImage img = ImageIO.read(new File("blue.jpg"))    

I don't know what I'm doing wrong. 我不知道我在做什么错。

Any suggestion? 有什么建议吗?

尝试将图像保存为PNG格式,然后重试!

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

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