如何在TYPE_3BYTE_BGR中获取jpeg图像的rgb值? [英] How to get the rgb values of a jpeg image in TYPE_3BYTE_BGR?

查看:159
本文介绍了如何在TYPE_3BYTE_BGR中获取jpeg图像的rgb值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张图片:

我想在int[]中提取此图像的RGB值.到目前为止,这是我对PNG图片所做的:

I'd like to extract the RGB values of this image in an int[]. This is what I've done so far for PNG images:

File f = new File("t.jpg");
BufferedImage img = ImageIO.read(f);
int[] ib = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
Color c = new Color(ib[0]);
System.out.println(c.getRed() + " " + c.getGreen() + " " + c.getBlue());

但是在这里我得到以下输出:255 128 128这是不期望的,因为我清楚地看到(并在多个图像编辑器中进行了验证)(0,0)处的像素具有这些值255 255 255.

But here I get this output: 255 128 128 which is not expected since I clearly see (and have verified in several image editors) that the pixel at (0,0) has these values 255 255 255.

我注意到img.getType()返回的类型等于

I noticed that the type returned by img.getType() is equal to TYPE_3BYTE_BGR so I guess it's a decoding issue happening behind the scene but I can't figure out how to workaround it (or get a clearer understanding of what's happening).

有人会建议如何正确解码此类型吗?

Does anyone would have a suggestion on how to decode this type properly?

推荐答案

在对各种解决方案进行了广泛的测试之后,我发现了com.sun.imageio.plugins.jpeg.JPEGImageReader类(ImageIO用于解码JPEG的类)的一个错误.课).

After extensive testing of different solutions, I came out with a bug for the class com.sun.imageio.plugins.jpeg.JPEGImageReader (the class used for the decoding of JPEGs by the ImageIO class).

如果可以的话(Oracle建议这样做,因为它应该适用于所有JVM和SO中的许多其他线程):

If I do (recommended by Oracle as this should work with all JVM and by many other threads in SO):

BufferedImage bi = ImageIO.read(new FileInputStream("t.jpg"));

然后我得到淡红色的图像.

then I get the redish image.

另一方面,如果我这样做:

In the other hand, if I do:

JPEGImageDecoder jpegDec = JPEGCodec.createJPEGDecoder(new FileInputStream("t.jpg"));
BufferedImage bi = jpegDec.decodeAsBufferedImage();

然后我得到正确解码的图像. (请注意,com.sun.image.codec.jpeg.JPEGCodec是Sun/Oracle JVM的特定类,因此该解决方案不可移植.)

then I get the image correctly decoded. (note that com.sun.image.codec.jpeg.JPEGCodec is a specific class of the Sun/Oracle JVM and thus this solution is not portable).

最后,我尝试过的另一种可行的方法是使用Toolkit.getDefaultToolkit().createImage().该图像是异步加载的(我认为这是一个小缺点),但至少它可以正确加载我的图像.我不确定100%是否可以确保最后一种解决方案可在所有平台/JVM上移植.

Finally, the other approach that I tried which works is using Toolkit.getDefaultToolkit().createImage(). The image is loaded asynchronously (small disadvantage in my opinion) but at least it could load my image properly. I'm not 100% sure that this last solution is portable on all platforms/JVMs though.

这篇关于如何在TYPE_3BYTE_BGR中获取jpeg图像的rgb值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆