如何在 Java 中将 RGB 图像转换为 CMYK 图像,反之亦然?

发布于 2024-10-08 07:08:29 字数 206 浏览 15 评论 0原文

我们的网络应用程序允许用户下载动态生成的不同格式的图像(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 技术交流群。

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

发布评论

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

评论(4

调妓 2024-10-15 07:08:29

某些图像格式不允许 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?

居里长安 2024-10-15 07:08:29

要通过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/

这是代码:

public static void rgbToCmyk() throws IOException{

    BufferedImage rgbImage = ImageIO.read(new File("C://Users//Public//Pictures//Sample Pictures//RGB_IMAGE.jpg"));
    BufferedImage cmykImage = null;
    ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(RbgToCmyk.class.getClassLoader().getResourceAsStream("ISOcoated.icc")));
    ColorConvertOp op = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), cpace, null);       
    cmykImage = op.filter(rgbImage, null);

    JAI.create("filestore", cmykImage, "c:/tmp/CMYK_IMAGE.TIF", "TIFF");
}

注意:“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:

public static void rgbToCmyk() throws IOException{

    BufferedImage rgbImage = ImageIO.read(new File("C://Users//Public//Pictures//Sample Pictures//RGB_IMAGE.jpg"));
    BufferedImage cmykImage = null;
    ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(RbgToCmyk.class.getClassLoader().getResourceAsStream("ISOcoated.icc")));
    ColorConvertOp op = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), cpace, null);       
    cmykImage = op.filter(rgbImage, null);

    JAI.create("filestore", cmykImage, "c:/tmp/CMYK_IMAGE.TIF", "TIFF");
}

NOTE: "ISOcoated.icc" is my ICC profile. You can get it from your printer or somewhere else.

浮生面具三千个 2024-10-15 07:08:29

建议使用 fromRGB() - 请参阅 http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html

示例代码:

java.awt.color.ColorSpace

ColorSpace cmyk = new ColorSpace(ColorSpace.TYPE_CMYK, 4);
float[] values = cmyk.fromRGB(rgbFloatArray);

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.html

Sample code:

java.awt.color.ColorSpace

ColorSpace cmyk = new ColorSpace(ColorSpace.TYPE_CMYK, 4);
float[] values = cmyk.fromRGB(rgbFloatArray);

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.

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