Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将CMYK图像保存到文件_Java_Image_File_Jai_Cmyk - Fatal编程技术网

Java将CMYK图像保存到文件

Java将CMYK图像保存到文件,java,image,file,jai,cmyk,Java,Image,File,Jai,Cmyk,我正在尝试在CMYK颜色空间中创建图像,并在使用它之后,例如,绘制线条等,将其保存到文件中。不幸的是,互联网上没有很多关于java中CMYK的信息。我只找到一篇文章。但在那里,图像正在使用iText库保存为Pdf格式。但我需要它被保存到png或jpeg文件。代码如下: public BufferedImage createCMYKBufferedImage(double l_width, double l_height) { ColorSpace colorSpace = SimpleC

我正在尝试在CMYK颜色空间中创建图像,并在使用它之后,例如,绘制线条等,将其保存到文件中。不幸的是,互联网上没有很多关于java中CMYK的信息。我只找到一篇文章。但在那里,图像正在使用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.writeimage、格式、文件


有人能帮我吗?

要以Jpeg格式编写缓冲区图像:

首先,将BuffereImage转换为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();
    }
}

看来,你在这个问题上有争议。Jpeg和PNG具有RGB图像格式。例如,看 .
因此,您必须将源图片直接放入png/jpeg或将CMYK打印为pdf。CMYK是一种打印格式,而不是屏幕格式

谢谢,这很有帮助,但正在生成的图像是黑色的。有什么想法吗?@Vladimir:我从一个正在运行的应用程序中提取了这个代码。制作一个简单的BuffereImage,并确保从简单的BuffereImage中获取有效的Jpeg。然后添加更多的BuffereImage代码,一次添加一点,直到获得所需的图像。JPEG可以使用CMYK颜色空间,并且可以用于屏幕和打印。