Example usage for com.itextpdf.text.pdf PdfName JBIG2GLOBALS

List of usage examples for com.itextpdf.text.pdf PdfName JBIG2GLOBALS

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfName JBIG2GLOBALS.

Prototype

PdfName JBIG2GLOBALS

To view the source code for com.itextpdf.text.pdf PdfName JBIG2GLOBALS.

Click Source Link

Document

A name

Usage

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

License:Open Source License

/**
 * Tead a JBIG2 image and give a BufferedImage
 * @param image//from  w  w w. java2s.c o m
 * @return The BufferedImage obj
 */
public static BufferedImage readJBIG2(PdfImageObject image) {
    BufferedImage buffImg = null;

    PdfDictionary dic = image.getDictionary();
    PdfDictionary decodedic = dic.getAsDict(PdfName.DECODEPARMS);
    PdfStream globalStream = decodedic.getAsStream(PdfName.JBIG2GLOBALS);

    try {
        byte[] byteArrayGlobal = PdfReader.getStreamBytes((PRStream) globalStream);

        InputStream in = new ByteArrayInputStream(image.getImageAsBytes());
        ImageInputStream stream = ImageIO.createImageInputStream(in);

        InputStream inG = new ByteArrayInputStream(byteArrayGlobal);
        ImageInputStream streamG = ImageIO.createImageInputStream(inG);

        JBIG2ImageReader reader = new JBIG2ImageReader(new JBIG2ImageReaderSpi());
        reader.setInput(stream);
        JBIG2Globals globals = reader.processGlobals(streamG);
        reader.setGlobals(globals);
        ImageReadParam param = reader.getDefaultReadParam();
        buffImg = reader.read(0, param);

        in.close();
        inG.close();

    } catch (Exception e) {
        logger.error("Exception", e);
        PdfTrickMessages.append("ERROR", Consts.SENDLOG_MSG);
    }
    return buffImg;
}