简体   繁体   English

将RGB PNG转换为CMYK JPEG(使用ICC颜色配置文件)

[英]Convert RGB PNG to CMYK JPEG (using ICC Color Profiles)

I need to convert a PNG-File into a CMYK JPEG. 我需要将PNG文件转换为CMYK JPEG。

During my research i've found multiple articles on SO decribing that problem. 在我的研究过程中,我发现了多篇关于SO的文章来描述这个问题。 I've copied this answer using BufferedImage and ColorConvertOp . 我使用BufferedImageColorConvertOp复制了这个答案

I came up with this little example: 我想出了这个小例子:

public static void main(final String[] args) throws IOException
{
    final String imageFile = "/tmp/page0.png";

    final BufferedImage pngImage = ImageIO.read(new File(imageFile));

    // convert PNG to JPEG
    // http://www.mkyong.com/java/convert-png-to-jpeg-image-file-in-java/
    final BufferedImage rgbImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_INT_RGB);
    rgbImage.createGraphics().drawImage(pngImage, 0, 0, Color.WHITE, null);

    // RGB to CMYK using ColorConvertOp
    // https://stackoverflow.com/questions/380678/how-to-set-icc-color-profile-in-java-and-change-colorspace/2804370#2804370
    final ICC_Profile ip = ICC_Profile.getInstance("icc/ISOcoated_v2_300_eci.icc");
    // final ICC_Profile ip = ICC_Profile.getInstance("icc/CoatedFOGRA27.icc");
    // final ICC_Profile ip = ICC_Profile.getInstance("icc/USWebUncoated.icc");

    final ColorConvertOp cco = new ColorConvertOp(new ICC_ColorSpace(ip), null);
    final BufferedImage cmykImage = cco.filter(rgbImage, null);

    // Write the result into an bytearray
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(cmykImage, "jpg", baos);
    baos.flush();

    final byte[] imageInByte = baos.toByteArray();
}

The problem is, that it leads me into this exception: 问题是,它引导我进入这个例外:

Exception in thread "main" javax.imageio.IIOException: Invalid argument to native writeImage
    at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1058)
    at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:360)
    at javax.imageio.ImageWriter.write(ImageWriter.java:615)
    at javax.imageio.ImageIO.doWrite(ImageIO.java:1612)
    at javax.imageio.ImageIO.write(ImageIO.java:1578)
    at ... .pdf.ReportGeneratorPublicContentTest.main(ReportGeneratorPublicContentTest.java:69)

The message of the Exception doesn't help me. Exception的消息对我没有帮助。 On this thread they say that sun jdk or JAI will fix the problem. 这个帖子中,他们说sun jdk或JAI将解决这个问题。

I tried apt-get install libjai-core-java and the oracle JDK jdk1.7.0_51 . 我尝试了apt-get install libjai-core-java和oracle JDK jdk1.7.0_51 The error still persists. 错误仍然存​​在。

@Christian Schneider : After i download your image file with link of CMYK JPEG, i open file's property. @Christian Schneider:在我下载了带有CMYK JPEG链接的图像文件后,我打开了文件的属性。 I see color space of image is still RGB. 我看到图像的色彩空间仍然是RGB。 This picture isn't converted to CMYK color. 此图片未转换为CMYK颜色。 Please see the below link : 请看以下链接:

how can I convert an RGB image to CMYK and vice versa in Java? 如何在Java中将RGB图像转换为CMYK,反之亦然?

lovelywib 's answer solved this. lovelywib的回答解决了这个问题。

The problem was solved by using TYPE_3BYTE_BGR instead of TYPE_INT_RGB . 通过使用TYPE_3BYTE_BGR而不是TYPE_INT_RGB解决了该问题。

public static void main(String[] args) throws Exception
{
    final String imageFile = "/tmp/page0.png";

    final BufferedImage pngImage = ImageIO.read(new File(imageFile));

    // convert PNG to JPEG
    // http://www.mkyong.com/java/convert-png-to-jpeg-image-file-in-java/
    final BufferedImage rgbImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    rgbImage.createGraphics().drawImage(pngImage, 0, 0, Color.WHITE, null);

    // RGB to CMYK using ColorConvertOp
    // http://stackoverflow.com/questions/380678/how-to-set-icc-color-profile-in-java-and-change-colorspace/2804370#2804370
    final ICC_Profile ip = ICC_Profile.getInstance("icc/USWebUncoated.icc");

    final ColorConvertOp cco = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), new ICC_ColorSpace(ip), null);

    final BufferedImage cmykImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);

    cco.filter(rgbImage, cmykImage);

    // Write the result into an bytearray
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(cmykImage, "JPEG", baos);
    baos.flush();
}

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

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