You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Tilman Hausherr (JIRA)" <ji...@apache.org> on 2016/11/11 16:48:58 UTC
[jira] [Commented] (PDFBOX-3566) ClassCastException in
JPEGFactory.createFromImage()
[ https://issues.apache.org/jira/browse/PDFBOX-3566?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15657495#comment-15657495 ]
Tilman Hausherr commented on PDFBOX-3566:
-----------------------------------------
No we won't use "JPEGImageWriter" because this would bring a dependency on com.sun.*. There is no mandatory use of that. For example, I use twelvemonkeys.
What we should do is to find a way to avoid that CCE early, e.g. by getting a plugin that delivers the correct type instead of just using the first possible writer. We already do something in ImageIOUtil so I'm optimistic that this will be fixed very soon.
> ClassCastException in JPEGFactory.createFromImage()
> ---------------------------------------------------
>
> Key: PDFBOX-3566
> URL: https://issues.apache.org/jira/browse/PDFBOX-3566
> Project: PDFBox
> Issue Type: Bug
> Affects Versions: 2.0.3
> Environment: Linux OS, Java 1.8, Oracle Java Advanced Imaging (JAI) installed.
> Reporter: ceakki
>
> I was trying to save all the pages from a PDF as images and adding them to a newly created PDF.
> {code}
> PDDocument newDoc = new PDDocument();
> PDFRenderer pdfRenderer = new PDFRenderer(oldDoc);
> floag width = ...
> float height = ...
> page.setMediaBox(new PDRectangle(width, height));
> newDoc.addPage(page);
> PDPageContentStream contents = new PDPageContentStream(newDoc, page);
> BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 200, ImageType.RGB);
> PDImageXObject imageXObject = JPEGFactory.createFromImage(newDoc, bufferedImage, 0.75);
> contents.drawImage(imageXObject, 0, 0, width, height);
> contents.close();
> ...
> {code}
> Sometimes it's working just fine, sometimes it's trowing a ClassCastException in JPEGFactory.createFromImage:
> {noformat}
> java.lang.ClassCastException: com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriteParam cannot be cast to javax.imageio.plugins.jpeg.JPEGImageWriteParam
> at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.encodeImageToJPEGStream(JPEGFactory.java:244)
> at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createJPEG(JPEGFactory.java:212)
> at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createFromImage(JPEGFactory.java:175)
> at org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.createFromImage(JPEGFactory.java:160)
> at com.mimecast.ttpservice.app.jpati.ConvertPDF.saveDoc(ConvertPDF.java:1390)
> {noformat}
> This is happening in the following code of above mentioned class:
> {code}
> imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
> ...
> JPEGImageWriteParam jpegParam = (JPEGImageWriteParam)imageWriter.getDefaultWriteParam();
> {code}
> In my case I can get more than one ImageWritter when calling *ImageIO.getImageWritersBySuffix("jpeg")* as follows:
> {noformat}
> com.sun.imageio.plugins.jpeg.JPEGImageWriter
> com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriter
> {noformat}
> I suspect that sometimes the *CLibJPEGImageWriter* is returned before *JPEGImageWriter*, which produces the ClassCastException a bit later.
> As I need to keep the JAI installed, is it possible to change the above code to loop through all ImageWriters found in order to pick the one needed by PdfBox?
> I would recommend doing something like this:
> {code}
> Iterator<ImageWriter> iterator = ImageIO.getImageWritersBySuffix("jpeg");
> while (iterator.hasNext()) {
> ImageWriter foundWriter = iterator.next();
> if (foundWriter instanceof JPEGImageWriter) {
> imageWriter = foundWriter;
> break;
> }
> }
> if (imageWriter == null) {
> throw new InvalidStateException("No image writer found of JPEGImageWriter type");
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org