Example usage for org.apache.pdfbox.pdmodel.graphics.color PDDeviceGray INSTANCE

List of usage examples for org.apache.pdfbox.pdmodel.graphics.color PDDeviceGray INSTANCE

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.color PDDeviceGray INSTANCE.

Prototype

PDDeviceGray INSTANCE

To view the source code for org.apache.pdfbox.pdmodel.graphics.color PDDeviceGray INSTANCE.

Click Source Link

Document

The single instance of this class.

Usage

From source file:edu.ist.psu.sagnik.research.pdfbox2playground.javatest.ExtractImages.java

License:Apache License

/**
 * Writes the image to a file with the filename + an appropriate suffix, like "Image.jpg".
 * The suffix is automatically set by the
 * @param filename the filename/*w  w w  . java2 s  .  c o  m*/
 * @throws IOException When somethings wrong with the corresponding file.
 */
private void write2file(PDImage pdImage, String filename, boolean directJPEG) throws IOException {
    String suffix = pdImage.getSuffix();
    if (suffix == null) {
        suffix = "png";
    }

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(filename + "." + suffix);
        BufferedImage image = pdImage.getImage();
        if (image != null) {
            if ("jpg".equals(suffix)) {
                String colorSpaceName = pdImage.getColorSpace().getName();
                if (directJPEG || PDDeviceGray.INSTANCE.getName().equals(colorSpaceName)
                        || PDDeviceRGB.INSTANCE.getName().equals(colorSpaceName)) {
                    // RGB or Gray colorspace: get and write the unmodifiedJPEG stream
                    //InputStream data = pdImage.getColor.getPartiallyFilteredStream(JPEG);
                    //IOUtils.copy(data, out);
                    //IOUtils.closeQuietly(data);
                    BufferedImage b = pdImage.getImage();
                    ImageIOUtil.writeImage(b, "jpg", out);
                } else {
                    // for CMYK and other "unusual" colorspaces, the JPEG will be converted
                    ImageIOUtil.writeImage(image, suffix, out);
                }
            } else {
                ImageIOUtil.writeImage(image, suffix, out);
            }
        }
        out.flush();
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:org.apache.tika.parser.pdf.PDF2XHTMLPureJava.java

License:Apache License

private void writeToBuffer(PDImageXObject pdImage, String suffix, OutputStream out) throws IOException {

    BufferedImage image = pdImage.getImage();
    if (image != null) {
        if ("jpg".equals(suffix)) {
            String colorSpaceName = pdImage.getColorSpace().getName();
            //TODO: figure out if we want directJPEG as a configuration
            //previously: if (directJPeg || PDDeviceGray....
            if (PDDeviceGray.INSTANCE.getName().equals(colorSpaceName)
                    || PDDeviceRGB.INSTANCE.getName().equals(colorSpaceName)) {
                // RGB or Gray colorspace: get and write the unmodifiedJPEG stream
                InputStream data = pdImage.getStream().createInputStream(JPEG);
                org.apache.pdfbox.io.IOUtils.copy(data, out);
                org.apache.pdfbox.io.IOUtils.closeQuietly(data);
            } else {
                // for CMYK and other "unusual" colorspaces, the JPEG will be converted
                //ImageIOUtil.writeImage(image, suffix, out);
            }//from  w w  w  .j  a v  a 2 s .co m
        } else if ("jp2".equals(suffix) || "jpx".equals(suffix)) {
            InputStream data = pdImage.createInputStream(JP2);
            org.apache.pdfbox.io.IOUtils.copy(data, out);
            org.apache.pdfbox.io.IOUtils.closeQuietly(data);
        } else if ("jb2".equals(suffix)) {
            InputStream data = pdImage.createInputStream(JB2);
            org.apache.pdfbox.io.IOUtils.copy(data, out);
            org.apache.pdfbox.io.IOUtils.closeQuietly(data);
        } else {
            //ImageIOUtil.writeImage(image, suffix, out);
        }
    }
    out.flush();
}