如何在 Java 中将 RGB 图像转换为 CMYK 图像,反之亦然?
我们的网络应用程序允许用户下载动态生成的不同格式的图像(bmp、png 和 jpeg)。我们的一些用户下载图像进行打印,因此我们希望允许他们在 RGB 或 CMYK 之间进行选择。 有没有办法在创建 RenderedImage/BufferedImage 时指定颜色模型?如果没有,默认颜色模型是什么?如何将其更改为另一个? 欢迎使用代码片段:)
谢谢,
奥利维尔。
our web app let users download dynamically generated images in different formats (bmp, png and jpeg). Some of our users download the images for printing, thus we would like to allow them to choose between RGB or CMYK.
Is there a way to specify the color model when creating a RenderedImage/BufferedImage? If not, what is the default color model and how can I change it to another?
Code snippets are welcome :)
Thanks,
Olivier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来这并不简单,您必须加载颜色配置文件才能做到这一点,或者扩展色彩空间以支持 CYMK。
It appears that it's not simple and you'll have to either load up a color profile to do it or extend the colorspace to support CYMK.
某些图像格式不允许 CMYK 色彩空间(PNG、JPEG/JFIF、GIF...),对于普通用户来说,需要以 RGB 进行打印。
您需要向客户提供 CMYK 图像的原因是什么?
Some image formats doesn't allow CMYK color spaces (PNG, JPEG/JFIF, GIF...) and for normal users printing in RGB is desirable.
What are the reasons you need to provide CMYK images to your customers?
要通过Java将RGB图像转换为CMYK图像,最简单的方法之一是使用JAI(Java高级图像)。
下载 JAI: http://download.java.net/media/jai/builds /release/1_1_3/
下载 JAI ImageIO:http:// /download.java.net/media/jai-imageio/builds/release/1.1/
这是代码:
注意:“ISOcoated.icc”是我的 ICC 配置文件。您可以从打印机或其他地方获取它。
To convert RGB image to CMYK image by Java, one of the easiest way is to use JAI (Java Advanced Image).
Download JAI: http://download.java.net/media/jai/builds/release/1_1_3/
DownLoad JAI ImageIO: http://download.java.net/media/jai-imageio/builds/release/1.1/
Here is the code:
NOTE: "ISOcoated.icc" is my ICC profile. You can get it from your printer or somewhere else.
建议使用
fromRGB()
- 请参阅 http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html示例代码:
public abstract float[] fromRGB (float[] rgbvalue)
将假定位于默认 CS_sRGB 颜色空间中的颜色值转换到此 ColorSpace 中。
此方法使用旨在在输入和输出颜色之间产生最佳感知匹配的算法来转换颜色值。为了进行颜色值的比色转换,您应该使用 CS_sRGB 颜色空间的 toCIEXYZ 方法首先从输入颜色空间转换为 CS_CIEXYZ 颜色空间,然后使用该颜色空间的 fromCIEXYZ 方法从 CS_CIEXYZ 转换为输出色彩空间。请参阅 toCIEXYZ 和 fromCIEXYZ 了解更多信息。
Suggest using
fromRGB()
- see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.htmlSample code:
public abstract float[] fromRGB(float[] rgbvalue)
Transforms a color value assumed to be in the default CS_sRGB color space into this ColorSpace.
This method transforms color values using algorithms designed to produce the best perceptual match between input and output colors. In order to do colorimetric conversion of color values, you should use the toCIEXYZ method of the CS_sRGB color space to first convert from the input color space to the CS_CIEXYZ color space, and then use the fromCIEXYZ method of this color space to convert from CS_CIEXYZ to the output color space. See toCIEXYZ and fromCIEXYZ for further information.