Hello,
I am trying to use the imageio jpeg2000 encoder to add jpeg2000-encoded
images in a PDF file.
I created a class Jpeg2000Factory that mimics the JpegFactory and I changed
the encodeImageToJPEGStream method to use the jp2 imageWriter:
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
//https://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forD
evelopers/jai-imageio-1_0-docs/com/sun/media/imageio/plugins/jpeg2000/J2KIma
geWriteParam.html
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();
}
}
}
I also change the creationg of the PDImageXObject to use the JPX_DECODE COS:
PDImageXObject pdImage = new PDImageXObject(document, byteStream,
COSName.JPX_DECODE, awtColorImage.getWidth(),
awtColorImage.getHeight(),
awtColorImage.getColorModel().getComponentSize(0),
getColorSpaceFromAWT(awtColorImage));
Still, the produced PDF does not work properly. Adobe acrobat gives the
error 'Insufficient data for an image' when I try to open a PDF with an
image, whereas PDF Xchange viewer open the PDF, but the colors of the image
are somewhat translucent.
Does anyone have any idea what I am doing wrong here?
Any pointers maybe?
Thank you
Costas