缓冲图像和Java 中的颜色模型

发布于 2024-08-27 22:15:30 字数 1173 浏览 4 评论 0原文

我使用 java 中的图像处理库来操作图像。我要做的第一步是读取图像并创建一个 java.awt.Image.BufferedImage 对象。我这样做,

BufferedImage sourceImage = ImageIO.read( new File( filePath ) );

上面的代码创建了一个带有 DirectColorModelBufferedImage 对象:

rmask=ff0000
gmask=ff00
bmask=ff
掩码=0。

这就是我在 MacBook 上运行上述代码时发生的情况。

但是当我在 Linux 机器(托管服务器)上运行相同的代码时,这会创建一个带有 ColorModelBufferedImage 对象:

像素位 = 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 of 1)
  • On linux -> it returns TYPE_3BYTE_BGR (a value of 5)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-09-03 22:15:30

我不知道为什么你会得到两种不同颜色的模型,尽管我相信它们是完全相同的。 DirectColorModel 有 4 个组件,但 alpha mask 为 0,因此实际上它只有 3 个组件,就像另一个组件一样。

我建议编写一个简单的辅助函数,以确保图像在传递到该图像库之前具有正确的颜色模型。辅助函数可以使用 http: //java.sun.com/j2se/1.4.2/docs/api/java/awt/image/ColorConvertOp.html 或使用类似以下代码(未经测试)的内容:

private static BufferedImage makeCompatible(BufferedImage image) {
  int w = image.getWidth();
  int h = image.getHeight();

  BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
  Graphics2D g = result.createGraphics();
  g.drawRenderedImage(image, new AffineTransform()); //or some other drawImage function
  g.dispose();

  return result;
}

假设该库能够处理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):

private static BufferedImage makeCompatible(BufferedImage image) {
  int w = image.getWidth();
  int h = image.getHeight();

  BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
  Graphics2D g = result.createGraphics();
  g.drawRenderedImage(image, new AffineTransform()); //or some other drawImage function
  g.dispose();

  return result;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文