pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: How can I write JPEG2000 encoded images in PDF?
Date Tue, 29 Mar 2016 16:11:22 GMT
Am 29.03.2016 um 09:14 schrieb Costas Stergiou:
>> >I wrote "upload", because attachments don't pass.
> Got it, here is a public link:
> https://onedrive.live.com/redir?resid=C5DCD865E294FE55!2348&authkey=!ABpDMjo
> pfUpNRR8&ithint=file%2czip


Thanks; your code works fine. I was able to produce a PDF:
http://www.filedropper.com/huhu

I suspect that the problem is either with your calling code, or with 
your JAI version. I was able to extract the image from your file, and it 
looked weird.

Here's the complete code I used:


import com.sun.media.imageio.plugins.jpeg2000.J2KImageWriteParam;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageOutputStream;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceCMYK;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

/**
  *
  * @author Costas Stergiou
  */
public class Jpeg2000Factory
{
     public static void main(String[] args) throws IOException
     {
         BufferedImage awtColorImage = ImageIO.read(new 
File("XXXXXX/testimagefk9.jpg"));

         PDDocument doc = new PDDocument();
         PDPage page = new PDPage();
         doc.addPage(page);
         PDPageContentStream contentStream = new 
PDPageContentStream(doc, page);

         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         encodeImageToJPEGStream(awtColorImage, 1, baos);

         PDImageXObject pdImage = new PDImageXObject(doc, new 
ByteArrayInputStream(baos.toByteArray()),
                 COSName.JPX_DECODE, awtColorImage.getWidth(),
                 awtColorImage.getHeight(),
                 awtColorImage.getColorModel().getComponentSize(0),
                 getColorSpaceFromAWT(awtColorImage));

         contentStream.drawImage(pdImage, 100, 600);

         contentStream.close();
         doc.save("XXXXX/huhu.pdf");
         doc.close();
     }

     // returns a PDColorSpace for a given BufferedImage
     private static PDColorSpace getColorSpaceFromAWT(BufferedImage 
awtImage)
     {
         if (awtImage.getColorModel().getNumComponents() == 1)
         {
             // 256 color (gray) JPEG
             return PDDeviceGray.INSTANCE;
         }

         ColorSpace awtColorSpace = 
awtImage.getColorModel().getColorSpace();
         if (awtColorSpace instanceof ICC_ColorSpace && 
!awtColorSpace.isCS_sRGB())
         {
             throw new UnsupportedOperationException("ICC color spaces 
not implemented");
         }

         switch (awtColorSpace.getType())
         {
             case ColorSpace.TYPE_RGB:
                 return PDDeviceRGB.INSTANCE;
             case ColorSpace.TYPE_GRAY:
                 return PDDeviceGray.INSTANCE;
             case ColorSpace.TYPE_CMYK:
                 return PDDeviceCMYK.INSTANCE;
             default:
                 throw new UnsupportedOperationException("color space 
not implemented: "
                         + awtColorSpace.getType());
         }
     }

     private static void encodeImageToJPEGStream(BufferedImage image, 
float quality, OutputStream out) throws IOException
     {
         // encode to JPEG
         ImageOutputStream ios = null;
         ImageWriter imageWriter = null;
         try
         {
             // find JAI writer
             imageWriter = ImageIO.getImageWritersBySuffix("jp2").next();
             ios = ImageIO.createImageOutputStream(out);
             imageWriter.setOutput(ios);
             // add compression
             J2KImageWriteParam param = (J2KImageWriteParam) 
imageWriter.getDefaultWriteParam();
             param.setSOP(true);
             param.setEPH(true);
             param.setWriteCodeStreamOnly(true);
             if (quality == 1.0f)
             {
                 param.setLossless(true);
                 //param.setFilter(J2KImageWriteParam.FILTER_53);
             }
             else
             {
                 param.setProgressionType("res");
param.setCompressionMode(J2KImageWriteParam.MODE_EXPLICIT);
                 param.setCompressionType("JPEG2000");
                 param.setLossless(false);
                 param.setCompressionQuality(quality);
                 param.setEncodingRate(1.01);
                 param.setFilter(J2KImageWriteParam.FILTER_97);
             }
             ImageTypeSpecifier imageTypeSpecifier = new 
ImageTypeSpecifier(image);
             IIOMetadata data
                     = 
imageWriter.getDefaultImageMetadata(imageTypeSpecifier, param);
             // write
             imageWriter.write(data, new IIOImage(image, null, null), 
param);
         }
         finally
         {
             // clean up
             IOUtils.closeQuietly(out);
             if (ios != null)
             {
                 ios.close();
             }
             if (imageWriter != null)
             {
                 imageWriter.dispose();
             }
         }
     }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message