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

北慕城南 提交于 2019-12-05 14:16:39
Han Kun

@Christian Schneider : After i download your image file with link of CMYK JPEG, i open file's property. I see color space of image is still RGB. This picture isn't converted to CMYK color. Please see the below link :

how can I convert an RGB image to CMYK and vice versa in Java?

lovelywib 's answer solved this.

The problem was solved by using TYPE_3BYTE_BGR instead of 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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!