Top Description Inners Fields Constructors Methods
org.apache.pdfbox.pdmodel.graphics.image

public final Class JPEGFactory

extends Object
Class Inheritance
Imports
java.awt.Transparency, java.awt.color.ColorSpace, .ICC_ColorSpace, java.awt.image.BufferedImage, .ColorConvertOp, .Raster, .WritableRaster, java.io.ByteArrayInputStream, .ByteArrayOutputStream, .InputStream, .IOException, java.util.Iterator, javax.imageio.IIOImage, .ImageIO, .ImageReader, .ImageTypeSpecifier, .ImageWriteParam, .ImageWriter, javax.imageio.metadata.IIOMetadata, javax.imageio.plugins.jpeg.JPEGImageWriteParam, javax.imageio.stream.ImageInputStream, .ImageOutputStream, javax.xml.xpath.XPath, .XPathExpressionException, .XPathFactory, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.cos.COSArray, .COSInteger, .COSName, org.apache.pdfbox.filter.Filter, org.apache.pdfbox.io.IOUtils, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace, .PDDeviceCMYK, .PDDeviceGray, .PDDeviceRGB, org.w3c.dom.Element

Factory for creating a PDImageXObject containing a JPEG compressed image.
Author
John Hewson

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final Log

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static PDImageXObject

Returns:

a new Image XObject
createFromByteArray
(PDDocument
the document where the image will be created
document
,
byte[]
bytes of JPEG image
byteArray
)

Creates a new JPEG Image XObject from a byte array containing JPEG data.

public static PDImageXObject

Returns:

a new Image XObject
createFromImage
(PDDocument
the document where the image will be created
document
,
BufferedImage
the BufferedImage to embed
image
)

Creates a new JPEG PDImageXObject from a BufferedImage.

public static PDImageXObject

Returns:

a new Image XObject
createFromImage
(PDDocument
the document where the image will be created
document
,
BufferedImage
the BufferedImage to embed
image
,
float
The desired JPEG compression quality; between 0 (best compression) and 1 (best image quality). See ImageWriteParam#setCompressionQuality(float) for more details.
quality
)

Creates a new JPEG PDImageXObject from a BufferedImage and a given quality.

public static PDImageXObject

Returns:

a new Image XObject
createFromImage
(PDDocument
the document where the image will be created
document
,
BufferedImage
the BufferedImage to embed
image
,
float
The desired JPEG compression quality; between 0 (best compression) and 1 (best image quality). See ImageWriteParam#setCompressionQuality(float) for more details.
quality
,
int
the desired dpi (resolution) value of the JPEG to be stored in metadata. This value has no influence on image content or size.
dpi
)

Creates a new JPEG Image XObject from a BufferedImage, a given quality and dpi metadata.

public static PDImageXObject

Returns:

a new Image XObject
createFromStream
(PDDocument
the document where the image will be created
document
,
InputStream
a stream of JPEG data
stream
)

Creates a new JPEG Image XObject from an input stream containing JPEG data.

private static PDImageXObject
createJPEG(PDDocument document, BufferedImage image, float quality, int dpi)

private static byte[]
encodeImageToJPEGStream(BufferedImage image, float quality, int dpi)

private static BufferedImage
private static BufferedImage
private static PDColorSpace
private static ImageWriter
private static int
private static JPEGFactory.Dimensions
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LOGback to summary
private static final Log LOG

Constructor Detail

JPEGFactoryback to summary
private JPEGFactory()

Method Detail

createFromByteArrayback to summary
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray) throws IOException

Creates a new JPEG Image XObject from a byte array containing JPEG data.

Parameters
document:PDDocument

the document where the image will be created

byteArray:byte[]

bytes of JPEG image

Returns:PDImageXObject

a new Image XObject

Exceptions
IOException:
if the input stream cannot be read
createFromImageback to summary
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException

Creates a new JPEG PDImageXObject from a BufferedImage.

Do not read a JPEG image from a stream/file and call this method; you'll get more speed and quality by calling createFromStream() instead.

Parameters
document:PDDocument

the document where the image will be created

image:BufferedImage

the BufferedImage to embed

Returns:PDImageXObject

a new Image XObject

Exceptions
IOException:
if the JPEG data cannot be written
createFromImageback to summary
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality) throws IOException

Creates a new JPEG PDImageXObject from a BufferedImage and a given quality.

Do not read a JPEG image from a stream/file and call this method; you'll get more speed and quality by calling createFromStream() instead. The image will be created with a dpi value of 72 to be stored in metadata.

Parameters
document:PDDocument

the document where the image will be created

image:BufferedImage

the BufferedImage to embed

quality:float

The desired JPEG compression quality; between 0 (best compression) and 1 (best image quality). See ImageWriteParam#setCompressionQuality(float) for more details.

Returns:PDImageXObject

a new Image XObject

Exceptions
IOException:
if the JPEG data cannot be written
createFromImageback to summary
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException

Creates a new JPEG Image XObject from a BufferedImage, a given quality and dpi metadata.

Do not read a JPEG image from a stream/file and call this method; you'll get more speed and quality by calling createFromStream() instead.

Parameters
document:PDDocument

the document where the image will be created

image:BufferedImage

the BufferedImage to embed

quality:float

The desired JPEG compression quality; between 0 (best compression) and 1 (best image quality). See ImageWriteParam#setCompressionQuality(float) for more details.

dpi:int

the desired dpi (resolution) value of the JPEG to be stored in metadata. This value has no influence on image content or size.

Returns:PDImageXObject

a new Image XObject

Exceptions
IOException:
if the JPEG data cannot be written
createFromStreamback to summary
public static PDImageXObject createFromStream(PDDocument document, InputStream stream) throws IOException

Creates a new JPEG Image XObject from an input stream containing JPEG data. The input stream data will be preserved and embedded in the PDF file without modification.

Parameters
document:PDDocument

the document where the image will be created

stream:InputStream

a stream of JPEG data

Returns:PDImageXObject

a new Image XObject

Exceptions
IOException:
if the input stream cannot be read
createJPEGback to summary
private static PDImageXObject createJPEG(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException
encodeImageToJPEGStreamback to summary
private static byte[] encodeImageToJPEGStream(BufferedImage image, float quality, int dpi) throws IOException
getAlphaImageback to summary
private static BufferedImage getAlphaImage(BufferedImage image)
getColorImageback to summary
private static BufferedImage getColorImage(BufferedImage image)
getColorSpaceFromAWTback to summary
private static PDColorSpace getColorSpaceFromAWT(BufferedImage awtImage)
getJPEGImageWriterback to summary
private static ImageWriter getJPEGImageWriter() throws IOException
getNumComponentsFromImageMetadataback to summary
private static int getNumComponentsFromImageMetadata(ImageReader reader) throws IOException
retrieveDimensionsback to summary
private static JPEGFactory.Dimensions retrieveDimensions(ByteArrayInputStream stream) throws IOException
org.apache.pdfbox.pdmodel.graphics.image back to summary

private Class JPEGFactory.Dimensions

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory.Dimensions

Field Summary

Modifier and TypeField and Description
private int
private int
private int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

heightback to summary
private int height
numComponentsback to summary
private int numComponents
widthback to summary
private int width

Constructor Detail

Dimensionsback to summary
private Dimensions()