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

前端 未结 4 1896
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 01:03

our web app let users download dynamically generated images in different formats (bmp, png and jpeg). Some of our users download the images for printing, thus we would like

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 01:51

    To convert RGB image to CMYK image by Java, one of the easiest way is to use JAI (Java Advanced Image).

    Download JAI: http://download.java.net/media/jai/builds/release/1_1_3/

    DownLoad JAI ImageIO: http://download.java.net/media/jai-imageio/builds/release/1.1/

    Here is the code:

    public static void rgbToCmyk() throws IOException{
    
        BufferedImage rgbImage = ImageIO.read(new File("C://Users//Public//Pictures//Sample Pictures//RGB_IMAGE.jpg"));
        BufferedImage cmykImage = null;
        ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(RbgToCmyk.class.getClassLoader().getResourceAsStream("ISOcoated.icc")));
        ColorConvertOp op = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), cpace, null);       
        cmykImage = op.filter(rgbImage, null);
    
        JAI.create("filestore", cmykImage, "c:/tmp/CMYK_IMAGE.TIF", "TIFF");
    }
    

    NOTE: "ISOcoated.icc" is my ICC profile. You can get it from your printer or somewhere else.

提交回复
热议问题