0

Im trying to apply the code posted in this post: How to convert from CMYK to RGB in Java correctly? The Answer from the guy named Codo works for me so far, but my source is not a file, its an object that gets converted into a BufferedImage with

stream = (PRStream)object;
PdfImageObject image = new PdfImageObject(stream);
//this does not work
BufferedImage bi = image.getBufferedImage();

The guy has a method that returns a BufferedImage from a file like so

public BufferedImage readImage(File file) throws IOException, ImageReadException

but i want to use

BufferedImage bi = readImage(image.getBufferedImage());

instead of

File f = new File("/Users/adlib/Documents/projekte/pdf_compress/envirement/eclipse_luna/WORKSPACE/PDFCompression/src/Bild.jpg");
BufferedImage bi = readImage(f);

cause im ectracting all the images from a pdf file using iText. I messed around with the code (changed file to BufferedImage and added streams) but a just dont get it to work. The File as Input image works fine, but not really what i need. What do i need to change to get This guys code to work with BufferedImage as input for the readImage() method?

Here is the complete code of this guy https://stackoverflow.com/a/12132630/4944643

He uses Sanselan / Apache Commons Imaging

Community
  • 1
  • 1
Alex P
  • 11
  • 5

1 Answers1

0

I'm not sure how iText extracts images, but chances are good it's using ImageIO. If so, you can just install (or depend on, using Maven) the TwelveMonkeys JPEG ImageIO plugin, and

BufferedImage bi = image.getBufferedImage();

...should just work.

The above mentioned plugin does support CMYK (and Adobe YCCK) JPEGs.

If iText doesn't use ImageIO, the above won't work (ie. when you already have a BufferedImage, it's too late to make the correct conversion). You will instead need to get to the bytes of the underlying PDF (using the getImageAsBytes() method), and use ImageIO (via the TwelveMonkeys JPEG plugin) to decode it:

byte[] imgBytes = image.getImageAsBytes();
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(imgBytes));
Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Did that allready. But i ended up getting an Image with Inverted colors. – Alex P Jun 03 '15 at 13:03
  • @AlexP Ok.. As I'm the author of the plugin, I'm interested in investigating this if you like. Can you save the bytes from the `getImageAsBytes()` method to disk, and attach it to a new [issue](https://github.com/haraldk/TwelveMonkeys/issues)? – Harald K Jun 03 '15 at 13:07
  • This would be cool. But i dont know how to write the bytes from the getImageAsBytes to disk. Can you give me a little help? never did that kind of stuff – Alex P Jun 03 '15 at 13:14
  • @AlexP Try `File file = new File(yourPath); OutputStream out = new FileOutputStream(file); try { out.write(image.getImageAsBytes()); } finally { out.close(); }` (sorry about the formatting, SO comments aren't great for code). – Harald K Jun 03 '15 at 13:26
  • byte[] imgByte = image.getImageAsBytes(); Files.write(Paths.get("bytes.txt"), imgByte); BufferedImage bi = ImageIO.read(new ByteArrayInputStream(imgByte)); like so? – Alex P Jun 03 '15 at 13:26
  • Ok i did it your way. gonna attach that in a new isuue. – Alex P Jun 03 '15 at 13:30
  • I'm sure your way works too. :-) Anyway, you might want to give the file a `.jpg` extension. Just because that is what the GitHub issue tracker likes. :-) – Harald K Jun 03 '15 at 14:07
  • 1
    Done. Also uploaded an another jpeg with adobe cmyk but that one works fine and the colors turn out right in the resulting file – Alex P Jun 03 '15 at 14:22