简体   繁体   English

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

[英]how can I convert an RGB image to CMYK and vice versa in Java?

our web app let users download dynamically generated images in different formats (bmp, png and jpeg). 我们的网络应用程序允许用户以不同的格式(bmp,png和jpeg)下载动态生成的图像。 Some of our users download the images for printing, thus we would like to allow them to choose between RGB or CMYK. 我们的一些用户下载图像进行打印,因此我们希望允许他们在RGB或CMYK之间进行选择。 Is there a way to specify the color model when creating a RenderedImage/BufferedImage? 有没有办法在创建RenderedImage / BufferedImage时指定颜色模型? If not, what is the default color model and how can I change it to another? 如果没有,默认颜色模型是什么?如何将其更改为另一个? Code snippets are welcome :) 欢迎使用代码片段:)

Thanks, 谢谢,

Olivier. 奥利维尔。

Some image formats doesn't allow CMYK color spaces (PNG, JPEG/JFIF, GIF...) and for normal users printing in RGB is desirable. 某些图像格式不允许CMYK色彩空间(PNG,JPEG / JFIF,GIF ...),而普通用户则需要以RGB格式打印。

What are the reasons you need to provide CMYK images to your customers? 您需要向客户提供CMYK图像的原因是什么?

To convert RGB image to CMYK image by Java, one of the easiest way is to use JAI (Java Advanced Image). 要通过Java将RGB图像转换为CMYK图像,最简单的方法之一是使用JAI(Java高级图像)。

Download JAI: http://download.java.net/media/jai/builds/release/1_1_3/ 下载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/ 下载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. 注意:“ISOcoated.icc”是我的ICC配置文件。 You can get it from your printer or somewhere else. 您可以从打印机或其他地方获取。

Suggest using fromRGB() - see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html 建议使用fromRGB() - 参见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) public abstract float [] fromRGB(float [] rgbvalue)

Transforms a color value assumed to be in the default CS_sRGB color space into this ColorSpace. 将假定为默认CS_sRGB颜色空间的颜色值转换为此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. 为了进行颜色值的比色转换,您应该使用CS_sRGB颜色空间的toCIEXYZ方法首先从输入颜色空间转换为CS_CIEXYZ颜色空间,然后使用此颜色空间的fromCIEXYZ方法从CS_CIEXYZ转换为输出颜色空间。 See toCIEXYZ and fromCIEXYZ for further information. 有关详细信息,请参阅CIEXYZ和fromCIEXYZ。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM