缓冲图像和Java 中的颜色模型
我使用 java 中的图像处理库来操作图像。我要做的第一步是读取图像并创建一个 java.awt.Image.BufferedImage 对象。我这样做,
BufferedImage sourceImage = ImageIO.read( new File( filePath ) );
上面的代码创建了一个带有 DirectColorModel
的 BufferedImage
对象:
rmask=ff0000
gmask=ff00
bmask=ff
掩码=0。
这就是我在 MacBook 上运行上述代码时发生的情况。
但是当我在 Linux 机器(托管服务器)上运行相同的代码时,这会创建一个带有 ColorModel
的 BufferedImage
对象:
像素位 = 24
组件数 = 3
颜色空间 = java.awt.color.ICC_ColorSpace@c39a20
透明度 = 1 alpha = false
isAlphaPre = false。
我在这两种情况下都使用了相同的 jpg 图像。我不知道为什么同一张图片的ColorModel
在mac和linux上运行时不同。 Mac 的 ColorModel 有 4 个组件,Linux 的 ColorModel 有 3 个组件。
因此出现了一个问题,我使用的图像处理库总是假设传递的图像的 ColorModel 中总是有 4 个组件,并且在 Linux 机器上运行时会抛出数组越界异常。但在 macbook 上却运行得很好。
添加更多信息,读取图像后,我打印出 image.getType()
- On mac ->它返回
TYPE_INT_RGB
(值为1
) - 在 linux 上 ->它返回
TYPE_3BYTE_BGR
(值为5
)
我不确定我是否做错了什么或者库有问题。请让我知道您的想法。如果我有不懂的地方也可以问我!
I am using a image processing library in java to manipulate images.The first step I do is I read an image and create a java.awt.Image.BufferedImage
object. I do it in this way,
BufferedImage sourceImage = ImageIO.read( new File( filePath ) );
The above code creates a BufferedImage
object with a DirectColorModel
:
rmask=ff0000
gmask=ff00
bmask=ff
amask=0.
This is what happens when I run the above code on my macbook.
But when I run this same code on a linux machine (hosted server), this creates a BufferedImage
object with ColorModel
:
pixelBits = 24
numComponents = 3
color space = java.awt.color.ICC_ColorSpace@c39a20
transparency = 1
has alpha = false
isAlphaPre = false.
I used the same jpg image in both the cases. I don't know why the ColorModel
of the same image is different when run on mac and linux. The ColorModel
for mac has 4 components and the colormodel for linux has 3 components.
There is a problem arising because of this, the image processing library that I use always assumes that there are always 4 components in the ColorModel of the image passed, and it throws an array out of bounds exception when run on the linux box. But on macbook, it runs fine.
Adding a little more info, Once the image is read, I printed out image.getType()
- On mac -> it returns
TYPE_INT_RGB
(a value of1
) - On linux -> it returns
TYPE_3BYTE_BGR
(a value of5
)
I am not sure if I am doing something wrong or there is a problem with the library. Please let me know your thoughts. Also ask me any questions if I am not making sense!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么你会得到两种不同颜色的模型,尽管我相信它们是完全相同的。 DirectColorModel 有 4 个组件,但 alpha mask 为 0,因此实际上它只有 3 个组件,就像另一个组件一样。
我建议编写一个简单的辅助函数,以确保图像在传递到该图像库之前具有正确的颜色模型。辅助函数可以使用 http: //java.sun.com/j2se/1.4.2/docs/api/java/awt/image/ColorConvertOp.html 或使用类似以下代码(未经测试)的内容:
假设该库能够处理BufferedImage.TYPE_4BYTE_ABGR。否则你将不得不在这里放置其他东西。当然,您可以在转换之前检查原始图像是否已经具有正确的格式。
I don't know exactly why you're getting two different color models although I believe they're quite the same. The DirectColorModel has 4 components but the alpha mask is 0, so in fact it only has 3 components just like the other one.
I suggest to write a simple helper function which makes sure the image has the right color model before you pass it to this image library. The helper function could make use of http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/ColorConvertOp.html or use something like the following code (untested):
Assuming that the library is able to handle BufferedImage.TYPE_4BYTE_ABGR. Otherwise you will have to put something else here. And of course, you could check if the original image already has the right format before converting it.