Example usage for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject getImage

List of usage examples for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject getImage

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject getImage.

Prototype

@Override
public BufferedImage getImage() throws IOException 

Source Link

Document

The returned images are cached via a SoftReference.

Usage

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignaturePlaceholderExtractor.java

License:EUPL

/**
 * Checks an image if it is a placeholder for a signature.
 *
 * @param image/*from  w w w. j  a  v  a  2 s.  c  om*/
 * @return
 * @throws IOException
 */
private SignaturePlaceholderData checkImage(PDImageXObject image) throws IOException {
    BufferedImage bimg = image.getImage();
    if (bimg == null) {
        String type = image.getSuffix();
        if (type != null) {
            type = type.toUpperCase() + " images";
        } else {
            type = "Image type";
        }
        logger.info("Unable to extract image for QRCode analysis. " + type
                + " not supported. Add additional JAI Image filters to your classpath. Refer to https://jai.dev.java.net. Skipping image.");
        return null;
    }
    if (bimg.getHeight() < 10 || bimg.getWidth() < 10) {
        logger.debug("Image too small for QRCode. Skipping image.");
        return null;
    }

    LuminanceSource source = new BufferedImageLuminanceSource(bimg);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    long before = System.currentTimeMillis();
    try {
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        Vector<BarcodeFormat> formats = new Vector<BarcodeFormat>();
        formats.add(BarcodeFormat.QR_CODE);
        hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
        result = new MultiFormatReader().decode(bitmap, hints);

        String text = result.getText();
        String profile = null;
        String type = null;
        String sigKey = null;
        String id = null;
        if (text != null) {
            if (text.startsWith(QR_PLACEHOLDER_IDENTIFIER)) {

                String[] data = text.split(";");
                if (data.length > 1) {
                    for (int i = 1; i < data.length; i++) {
                        String kvPair = data[i];
                        String[] kv = kvPair.split("=");
                        if (kv.length != 2) {
                            logger.debug("Invalid parameter in placeholder data: " + kvPair);
                        } else {
                            if (kv[0].equalsIgnoreCase(SignaturePlaceholderData.ID_KEY)) {
                                id = kv[1];
                            } else if (kv[0].equalsIgnoreCase(SignaturePlaceholderData.PROFILE_KEY)) {
                                profile = kv[1];
                            } else if (kv[0].equalsIgnoreCase(SignaturePlaceholderData.SIG_KEY_KEY)) {
                                sigKey = kv[1];
                            } else if (kv[0].equalsIgnoreCase(SignaturePlaceholderData.TYPE_KEY)) {
                                type = kv[1];
                            }
                        }
                    }
                }
                return new SignaturePlaceholderData(profile, type, sigKey, id);
            } else {
                logger.warn("QR-Code found but does not start with \"" + QR_PLACEHOLDER_IDENTIFIER
                        + "\". Ignoring QR placeholder.");
            }
        }
    } catch (ReaderException re) {
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Could not decode - not a placeholder. needed: " + (System.currentTimeMillis() - before));
        }
        if (!(re instanceof NotFoundException)) {
            if (logger.isInfoEnabled()) {
                logger.info("Failed to decode image", re);
            }
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        if (logger.isInfoEnabled()) {
            logger.info("Failed to decode image. Probably a zxing bug", e);
        }
    }
    return null;
}

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);
            }//www.  ja v a2s .  c om
        } 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();
}

From source file:org.fit.pdfdom.PDFBoxTree.java

License:Open Source License

protected void processImageOperation(List<COSBase> arguments) throws IOException {
    COSName objectName = (COSName) arguments.get(0);
    PDXObject xobject = getResources().getXObject(objectName);
    if (xobject instanceof PDImageXObject) {
        PDImageXObject pdfImage = (PDImageXObject) xobject;
        BufferedImage outputImage = pdfImage.getImage();

        // x, y and size are handled by css attributes but still need to rotate the image so pulling
        // only rotation out of the matrix so no giant whitespace offset from translations
        Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
        AffineTransform tr = ctm.createAffineTransform();
        double rotate = Math.atan2(tr.getShearY(), tr.getScaleY()) - Math.toRadians(pdpage.getRotation());
        outputImage = ImageUtils.rotateImage(outputImage, rotate);
        byte[] imageData = getImageData(outputImage);

        Rectangle2D imageBounds = pdfImage.getImage().getRaster().getBounds();
        AffineTransform pageTransform = createCurrentPageTransformation();
        AffineTransform imageTransform = new AffineTransform(ctm.createAffineTransform());
        imageTransform.scale(1.0 / pdfImage.getWidth(), -1.0 / pdfImage.getHeight());
        imageTransform.translate(0, -pdfImage.getHeight());
        pageTransform.concatenate(imageTransform);
        Rectangle2D bounds = pageTransform.createTransformedShape(imageBounds).getBounds2D();

        float x = (float) bounds.getX();
        float y = (float) bounds.getY();

        renderImage(x, y, (float) bounds.getWidth(), (float) bounds.getHeight(), "image/png", imageData);
    }//ww  w .j av  a 2 s .c  o  m
}