Class JPEGFactory
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory
-
public final class JPEGFactory extends java.lang.Object
Factory for creating a PDImageXObject containing a JPEG compressed image.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
JPEGFactory.Dimensions
-
Field Summary
Fields Modifier and Type Field Description private static org.apache.commons.logging.Log
LOG
-
Constructor Summary
Constructors Modifier Constructor Description private
JPEGFactory()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static PDImageXObject
createFromByteArray(PDDocument document, byte[] byteArray)
Creates a new JPEG Image XObject from a byte array containing JPEG data.static PDImageXObject
createFromImage(PDDocument document, java.awt.image.BufferedImage image)
Creates a new JPEG PDImageXObject from a BufferedImage.static PDImageXObject
createFromImage(PDDocument document, java.awt.image.BufferedImage image, float quality)
Creates a new JPEG PDImageXObject from a BufferedImage and a given quality.static PDImageXObject
createFromImage(PDDocument document, java.awt.image.BufferedImage image, float quality, int dpi)
Creates a new JPEG Image XObject from a BufferedImage, a given quality and dpi metadata.static PDImageXObject
createFromStream(PDDocument document, java.io.InputStream stream)
Creates a new JPEG Image XObject from an input stream containing JPEG data.private static PDImageXObject
createJPEG(PDDocument document, java.awt.image.BufferedImage image, float quality, int dpi)
private static void
encodeImageToJPEGStream(java.awt.image.BufferedImage image, float quality, int dpi, java.io.OutputStream out)
private static java.awt.image.BufferedImage
getAlphaImage(java.awt.image.BufferedImage image)
private static java.awt.image.BufferedImage
getColorImage(java.awt.image.BufferedImage image)
private static PDColorSpace
getColorSpaceFromAWT(java.awt.image.BufferedImage awtImage)
private static javax.imageio.ImageWriter
getJPEGImageWriter()
private static int
getNumComponentsFromImageMetadata(javax.imageio.ImageReader reader)
private static JPEGFactory.Dimensions
retrieveDimensions(java.io.ByteArrayInputStream stream)
-
-
-
Method Detail
-
createFromStream
public static PDImageXObject createFromStream(PDDocument document, java.io.InputStream stream) throws java.io.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
- the document where the image will be createdstream
- a stream of JPEG data- Returns:
- a new Image XObject
- Throws:
java.io.IOException
- if the input stream cannot be read
-
createFromByteArray
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray) throws java.io.IOException
Creates a new JPEG Image XObject from a byte array containing JPEG data.- Parameters:
document
- the document where the image will be createdbyteArray
- bytes of JPEG image- Returns:
- a new Image XObject
- Throws:
java.io.IOException
- if the input stream cannot be read
-
retrieveDimensions
private static JPEGFactory.Dimensions retrieveDimensions(java.io.ByteArrayInputStream stream) throws java.io.IOException
- Throws:
java.io.IOException
-
getNumComponentsFromImageMetadata
private static int getNumComponentsFromImageMetadata(javax.imageio.ImageReader reader) throws java.io.IOException
- Throws:
java.io.IOException
-
createFromImage
public static PDImageXObject createFromImage(PDDocument document, java.awt.image.BufferedImage image) throws java.io.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
- the document where the image will be createdimage
- the BufferedImage to embed- Returns:
- a new Image XObject
- Throws:
java.io.IOException
- if the JPEG data cannot be written
-
createFromImage
public static PDImageXObject createFromImage(PDDocument document, java.awt.image.BufferedImage image, float quality) throws java.io.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
- the document where the image will be createdimage
- the BufferedImage to embedquality
- The desired JPEG compression quality; between 0 (best compression) and 1 (best image quality). SeeImageWriteParam.setCompressionQuality(float)
for more details.- Returns:
- a new Image XObject
- Throws:
java.io.IOException
- if the JPEG data cannot be written
-
createFromImage
public static PDImageXObject createFromImage(PDDocument document, java.awt.image.BufferedImage image, float quality, int dpi) throws java.io.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
- the document where the image will be createdimage
- the BufferedImage to embedquality
- The desired JPEG compression quality; between 0 (best compression) and 1 (best image quality). SeeImageWriteParam.setCompressionQuality(float)
for more details.dpi
- the desired dpi (resolution) value of the JPEG to be stored in metadata. This value has no influence on image content or size.- Returns:
- a new Image XObject
- Throws:
java.io.IOException
- if the JPEG data cannot be written
-
getAlphaImage
private static java.awt.image.BufferedImage getAlphaImage(java.awt.image.BufferedImage image)
-
createJPEG
private static PDImageXObject createJPEG(PDDocument document, java.awt.image.BufferedImage image, float quality, int dpi) throws java.io.IOException
- Throws:
java.io.IOException
-
getJPEGImageWriter
private static javax.imageio.ImageWriter getJPEGImageWriter() throws java.io.IOException
- Throws:
java.io.IOException
-
encodeImageToJPEGStream
private static void encodeImageToJPEGStream(java.awt.image.BufferedImage image, float quality, int dpi, java.io.OutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
getColorSpaceFromAWT
private static PDColorSpace getColorSpaceFromAWT(java.awt.image.BufferedImage awtImage)
-
getColorImage
private static java.awt.image.BufferedImage getColorImage(java.awt.image.BufferedImage image)
-
-