Java将CMYK图像保存到文件

发布于 2024-10-21 03:50:59 字数 1154 浏览 4 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(2

秋凉 2024-10-28 03:50:59

要将 BufferedImage 写入 Jpeg:

首先,将 BufferedImage 转换为 Jpeg 字节数组。

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public static byte[] jpegToBytes(BufferedImage image) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
    JPEGEncodeParam jparm = encoder.getDefaultJPEGEncodeParam(image);
    jparm.setQuality(1F, false);

    try {
        encoder.encode(image, jparm);
        os.close();
    } catch (IOException e) {
        EclipseLogging.logError(RabidPhotoPlugin.getDefault(),
                RabidPhotoPlugin.PLUGIN_ID, e);
        return new byte[0];
    }
    return os.toByteArray();
}

接下来,将字节数组写入文件。

public static void writePhoto(byte[] photo) {
    try {
        OutputStream os = new FileOutputStream('file name');
        os.write(photo);
        os.flush();
        os.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

To write a BufferedImage as a Jpeg:

First, convert the BufferedImage to a Jpeg byte array.

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public static byte[] jpegToBytes(BufferedImage image) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
    JPEGEncodeParam jparm = encoder.getDefaultJPEGEncodeParam(image);
    jparm.setQuality(1F, false);

    try {
        encoder.encode(image, jparm);
        os.close();
    } catch (IOException e) {
        EclipseLogging.logError(RabidPhotoPlugin.getDefault(),
                RabidPhotoPlugin.PLUGIN_ID, e);
        return new byte[0];
    }
    return os.toByteArray();
}

Next, write the byte array to a file.

public static void writePhoto(byte[] photo) {
    try {
        OutputStream os = new FileOutputStream('file name');
        os.write(photo);
        os.flush();
        os.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
孤单情人 2024-10-28 03:50:59

看来你的问题有争议。 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.

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