【问题标题】:Java save CMYK image to fileJava将CMYK图像保存到文件
【发布时间】:2011-11-17 23:07:08
【问题描述】:

我正在尝试在 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(图像、格式、文件);

谁能帮帮我?

【问题讨论】:

    标签: java image file jai cmyk


    【解决方案1】:

    将 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();
        }
    }
    

    【讨论】:

    • 谢谢,这很有帮助,但生成的图像是黑色的。有什么想法吗?
    • @Vladimir:我从一个工作应用程序中提取了这段代码。制作一个简单的 BufferedImage,并确保从简单的 BufferedImage 中获得有效的 Jpeg。然后添加更多的 BufferedImage 代码,一次一点,直到你得到你想要的图像。
    【解决方案2】:

    看来,你的问题有争议。 Jpeg 和 PNG 具有 RGB 图像格式。例如,看 http://forums.adobe.com/message/2704225。 因此,您必须将源图片直接放入 png/jpeg 或将 CMYK 打印为 pdf。 CMYK 是一种打印格式,而不是丝网格式。

    【讨论】:

    • Jpeg 可以使用 CMYK 颜色空间,并且可以用于屏幕和打印。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2011-05-08
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多