1 Reply Latest reply: Aug 3, 2010 7:00 AM by 843802 RSS

    How is bitsPerRate set for PDF to TIFF conversion?

    843802
      I need to convert a PDF to a group 4 TIFF. I found certain objects I need to set to accomplish this (code is below). I am getting the exception below when performing the "write sequence" methon on the TIFF writer. I am wondering if anyone can offer any insight on how the bits per sample property is set and how can i change it?<br>:<br>

      Thanks:<br>:<br>

      Brian<br>

      EXCEPTION:<br>
      <br>
      javax.imageio.IIOException: Bits per sample must be 1 for T4 compression!<br>

      at com.sun.media.imageioimpl.plugins.tiff.TIFFT4Compressor.encode(TIFFT4Compressor.java:219)<br>

      at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.writeTile(TIFFImageWriter.java:1256).<br>

      at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.write(TIFFImageWriter.java:1555)<br>

      at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.writeInsert(TIFFImageWriter.java:1683)<br>

      at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.writeToSequence(TIFFImageWriter.java:1598)<br>
      <br><br>

      CODE:<br>
      <br>

      public static byte[] PDFToImage(byte[] bai, String imageType) <br>
      {<br>
      byte[] out = null; <br>
      try <br>
      {<br>
           PdfDecoder decoder = new PdfDecoder();<br>
           decoder.openPdfArray(bai);<br>

           if(decoder.isFileViewable()) <br>
           {<br>
                ByteArrayOutputStream baos = new ByteArrayOutputStream();<br>
                ImageOutputStream ios = ImageIO.createImageOutputStream(baos);<br>

                boolean foundWriter = false;<br>                    
                Iterator writerIter = ImageIO.getImageWritersByMIMEType("image/" + imageType);<br>
                while (writerIter.hasNext() && !foundWriter) <br>
                {<br>
                     foundWriter = true;<br>
                     TIFFImageWriter writer = (TIFFImageWriter)writerIter.next();<br>
                     writer.setOutput(ios);<br>
                     TIFFImageWriteParam param = new TIFFImageWriteParam(new Locale("en"));<br>
                     param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);<br>
                     param.setCompressionType("CCITT T.4");<br>
                     param.setCompressionQuality(1);<br>

                writer.prepareWriteSequence(null);<br>
                     for (int i = 0; i < decoder.getPageCount(); i++) <br>
                     {<br>
                          int pageNumber = i + 1;<br>
                          BufferedImage image = decoder.getPageAsImage(pageNumber);<br>
                          IIOImage iioImage = new IIOImage(image, null, null);<br>

                          try{<br>
                               writer.writeToSequence(iioImage, param);<br>

                          }catch(Exception e){<br>
                               throw e;<br>
                          }<br>

                          if(i==2)<br>
                          break;<br>
                     }<br>
                     writer.endWriteSequence();<br>
                     ios.flush();<br>
                     writer.dispose();<br>
                     ios.close();<br>
                     out = baos.toByteArray();<br>
                }<br>
                if (!foundWriter) <br>
                {<br>
                     throw new RuntimeException("Error: no writer found for image type '"
                                         + imageType
                                         + "'");<br>
                }<br>
           }<br>
           decoder.closePdfFile();<br>
      } <br>
      catch (Exception e) <br>
      {<br>
           e.printStackTrace();<br>
      }<br>
      return out;<br>
      }<br>

      Message was edited by:
      breaknine