Java Code Examples for org.apache.pdfbox.cos.COSName#DCT_DECODE

The following examples show how to use org.apache.pdfbox.cos.COSName#DCT_DECODE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JPEGFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private static PDImageXObject createJPEG(PDDocument document, BufferedImage image,
                                         float quality, int dpi) throws IOException
{
    // extract alpha channel (if any)
    BufferedImage awtColorImage = getColorImage(image);
    BufferedImage awtAlphaImage = getAlphaImage(image);

    // create XObject
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    encodeImageToJPEGStream(awtColorImage, quality, dpi, baos);
    ByteArrayInputStream byteStream = new ByteArrayInputStream(baos.toByteArray());
    
    PDImageXObject pdImage = new PDImageXObject(document, byteStream, 
            COSName.DCT_DECODE, awtColorImage.getWidth(), awtColorImage.getHeight(), 
            awtColorImage.getColorModel().getComponentSize(0),
            getColorSpaceFromAWT(awtColorImage));

    // alpha -> soft mask
    if (awtAlphaImage != null)
    {
        PDImage xAlpha = JPEGFactory.createFromImage(document, awtAlphaImage, quality);
        pdImage.getCOSObject().setItem(COSName.SMASK, xAlpha);
    }

    return pdImage;
}
 
Example 2
Source File: JPEGFactory.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a new JPEG Image XObject from a byte array containing JPEG data.
 *
 * @param document the document where the image will be created
 * @param byteArray bytes of JPEG image
 * @return a new Image XObject
 *
 * @throws IOException if the input stream cannot be read
 */
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray)
        throws IOException
{
    // copy stream
    ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);

    // read image
    Raster raster = readJPEGRaster(byteStream);
    byteStream.reset();

    PDColorSpace colorSpace;
    switch (raster.getNumDataElements())
    {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            throw new UnsupportedOperationException("number of data elements not supported: " +
                    raster.getNumDataElements());
    }

    // create PDImageXObject from stream
    PDImageXObject pdImage = new PDImageXObject(document, byteStream, 
            COSName.DCT_DECODE, raster.getWidth(), raster.getHeight(), 8, colorSpace);

    if (colorSpace instanceof PDDeviceCMYK)
    {
        COSArray decode = new COSArray();
        decode.add(COSInteger.ONE);
        decode.add(COSInteger.ZERO);
        decode.add(COSInteger.ONE);
        decode.add(COSInteger.ZERO);
        decode.add(COSInteger.ONE);
        decode.add(COSInteger.ZERO);
        decode.add(COSInteger.ONE);
        decode.add(COSInteger.ZERO);
        pdImage.setDecode(decode);
    }

    return pdImage;
}