You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "adam brin (JIRA)" <ji...@apache.org> on 2014/01/13 02:36:53 UTC

[jira] [Commented] (PDFBOX-1799) NullPointerException when constructing a PDJPeg using a BufferedImage

    [ https://issues.apache.org/jira/browse/PDFBOX-1799?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13869229#comment-13869229 ] 

adam brin commented on PDFBOX-1799:
-----------------------------------

[~puhong.you@gmail.com] [~tilman] is probably right that your issue and mine [PDFBOX-1841] are related. I'm going to try and deregister the CLibJPEGImageWriter from the registr. Have you tried this, or is it something that would work for you?
 
 IIORegistry.getDefaultInstance().deregisterServiceProvider(CLibJPEGImageWriterSpi.class);

> NullPointerException when constructing a PDJPeg using a BufferedImage
> ---------------------------------------------------------------------
>
>                 Key: PDFBOX-1799
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-1799
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 1.8.3
>         Environment: Windows 2008 R2 Standard
>            Reporter: Puhong You
>
> In pdfbox 1.8.2 and 1.8.3, the constructor of PDJPeg class that takes a BufferedImage throws java.lang.NullPointerException:
> 	    BufferedImage bi = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("images/icon.png"));
> 	    
> 	    PDJpeg icon = new PDJpeg(pdf, bi);
> The stack trace is:
> java.lang.NullPointerException
> 	at org.apache.pdfbox.util.ImageIOUtil.addResolution(ImageIOUtil.java:211)
> 	at org.apache.pdfbox.util.ImageIOUtil.createMetadata(ImageIOUtil.java:204)
> 	at org.apache.pdfbox.util.ImageIOUtil.writeImage(ImageIOUtil.java:158)
> 	at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.createImageStream(PDJpeg.java:171)
> 	at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.<init>(PDJpeg.java:133)
> The culprit is in this method in the ImageIOUtil:
>     private static IIOMetadata createMetadata(RenderedImage image, ImageWriter imageWriter,
>             ImageWriteParam writerParams, int resolution)
>     {
>         .....
>         IIOMetadata meta = imageWriter.getDefaultImageMetadata( type, writerParams );
>         return (addResolution(meta, resolution) ? meta : null);
>     } 
> One of the JPG image writer in our environment is com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriter, its implementation of getDefaultImageMetadata() returns null:
> public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, ImageWriteParam param) {
>         return null;
>     }
> this causes the NullPointerException at the first line of the addResolution() method:
>        if (!meta.isReadOnly() && meta.isStandardMetadataFormatSupported())
> I suggest that null checks be added at the following places:
> 1. ImageIOUtil.addResolution():
>        if (!meta.isReadOnly() && meta.isStandardMetadataFormatSupported())
> ==>
>        if (meta != null && meta.isReadOnly() && meta.isStandardMetadataFormatSupported())
> 2. ImageIOUtil.writeImage():
>                    IIOMetadata meta = createMetadata( image, imageWriter, writerParams, resolution);
> 	           imageWriter.setOutput( output );
> 	           imageWriter.write( null, new IIOImage( image, null, meta ), writerParams );
> 	           foundWriter = true;
> ==>
>                    IIOMetadata meta = createMetadata( image, imageWriter, writerParams, resolution);
>                     if (meta != null) {
> 	                    imageWriter.setOutput( output );
> 	                    imageWriter.write( null, new IIOImage( image, null, meta ), writerParams );
> 	                    foundWriter = true;
>                     }



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)