I need to convert a PNG file to CMYK JPEG.
During my research, I found several articles on SO describing this problem. I copied this answer with BufferedImageand ColorConvertOp.
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));
final BufferedImage rgbImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_INT_RGB);
rgbImage.createGraphics().drawImage(pngImage, 0, 0, Color.WHITE, null);
final ICC_Profile ip = ICC_Profile.getInstance("icc/ISOcoated_v2_300_eci.icc");
final ColorConvertOp cco = new ColorConvertOp(new ICC_ColorSpace(ip), null);
final BufferedImage cmykImage = cco.filter(rgbImage, null);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(cmykImage, "jpg", baos);
baos.flush();
final byte[] imageInByte = baos.toByteArray();
}
The problem is that it leads me to 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 exception message does not help me. In this thread, they say that a problem with sun jdk or JAI will fix the problem.
I tried apt-get install libjai-core-javaand oracle JDK jdk1.7.0_51. The error still persists.
source
share