Java将CMYK图像保存到文件
我正在尝试在 CMYK 色彩空间中创建图像,并在使用它之后(例如,绘画线条等),将其保存到文件中。不幸的是,互联网上没有很多关于 java 中的 CMYK 的信息。我只找到一篇文章 http://carback.us/rick/blog/?p=58 。但图像正在使用 iText 库保存为 Pdf。但我需要将其保存为 png 或 jpeg 文件。这是代码:
public BufferedImage createCMYKBufferedImage(double l_width, double l_height) {
ColorSpace colorSpace = SimpleCMYKColorSpace.getInstance();
ComponentColorModel l_ccm = new ComponentColorModel(colorSpace, false, false,
1, DataBuffer.TYPE_FLOAT);
int[] l_bandoff = {0, 1, 2, 3}; //Index for each color (C, is index 0, etc)
PixelInterleavedSampleModel l_sm = new PixelInterleavedSampleModel(
DataBuffer.TYPE_FLOAT,
(int)l_width, (int)l_height,
4,(int)l_width*4, l_bandoff);
WritableRaster l_raster = WritableRaster.createWritableRaster(l_sm,
new Point(0, 0));
return new BufferedImage(l_ccm, l_raster, false, null);
}
当我尝试保存图像时,我只是调用
ImageIO.write(图像、格式、文件);
有人可以帮助我吗?
I'm trying to create image in CMYK Colorspace and after working with it, for example, painting lines etc., save it to file. Unfortunately, there isn't a lot of information in the internet about CMYK in java. I have found only an article http://carback.us/rick/blog/?p=58. But there the Image is being saved to Pdf, using iText library. But I need it to be savet to png or jpeg file. Here is the code:
public BufferedImage createCMYKBufferedImage(double l_width, double l_height) {
ColorSpace colorSpace = SimpleCMYKColorSpace.getInstance();
ComponentColorModel l_ccm = new ComponentColorModel(colorSpace, false, false,
1, DataBuffer.TYPE_FLOAT);
int[] l_bandoff = {0, 1, 2, 3}; //Index for each color (C, is index 0, etc)
PixelInterleavedSampleModel l_sm = new PixelInterleavedSampleModel(
DataBuffer.TYPE_FLOAT,
(int)l_width, (int)l_height,
4,(int)l_width*4, l_bandoff);
WritableRaster l_raster = WritableRaster.createWritableRaster(l_sm,
new Point(0, 0));
return new BufferedImage(l_ccm, l_raster, false, null);
}
When I'm trying to save an image, i'm just calling
ImageIO.write(image, format, file);
Can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将 BufferedImage 写入 Jpeg:
首先,将 BufferedImage 转换为 Jpeg 字节数组。
接下来,将字节数组写入文件。
To write a BufferedImage as a Jpeg:
First, convert the BufferedImage to a Jpeg byte array.
Next, write the byte array to a file.
看来你的问题有争议。 Jpeg 和 PNG 具有 RGB 图像格式。例如,看
http://forums.adobe.com/message/2704225。
所以,你必须将源图片直接放入png/jpeg或打印CMYK到pdf。 CMYK 是一种印刷格式,而不是屏幕格式。
It seems, you have a controversy in the question. Jpeg and PNG have RGB image format. For example, look
http://forums.adobe.com/message/2704225.
So, you have to put the source picture directly into png/jpeg or print CMYK to pdf. CMYK is a printing format, not a screen one.