/** * Creates a new JPEG PDImageXObject from a BufferedImage and a given quality. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * The image will be created with a dpi value of 72 to be stored in metadata. * @param document the document where the image will be created * @param image the BufferedImage to embed * @param quality The desired JPEG compression quality; between 0 (best * compression) and 1 (best image quality). See * {@link ImageWriteParam#setCompressionQuality(float)} for more details. * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality) throws IOException { return createFromImage(document, image, quality, 72); }
/** * Creates a new JPEG PDImageXObject from a BufferedImage. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException { return createFromImage(document, image, 0.75f); }
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; }
/** * Creates a new JPEG PDImageXObject from a BufferedImage. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException { return createFromImage(document, image, 0.75f); }
/** * Creates a new JPEG PDImageXObject from a BufferedImage and a given quality. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * The image will be created with a dpi value of 72 to be stored in metadata. * @param document the document where the image will be created * @param image the BufferedImage to embed * @param quality the desired JPEG compression quality * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality) throws IOException { return createFromImage(document, image, quality, 72); }
/** * Creates a new JPEG PDImageXObject from a BufferedImage. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException { return createFromImage(document, image, 0.75f); }
/** * Creates a new JPEG PDImageXObject from a BufferedImage and a given quality. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * The image will be created with a dpi value of 72 to be stored in metadata. * @param document the document where the image will be created * @param image the BufferedImage to embed * @param quality The desired JPEG compression quality; between 0 (best * compression) and 1 (best image quality). See * {@link ImageWriteParam#setCompressionQuality(float)} for more details. * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality) throws IOException { return createFromImage(document, image, quality, 72); }
private PDImageXObject ensureCached(final ScaledJpeg sj) { BufferedImage bufferedImage = sj.bufferedImage(); PDImageXObject temp = jpegMap.get(bufferedImage); if (temp == null) { try { temp = JPEGFactory.createFromImage(doc, bufferedImage); } catch (IOException ioe) { // can there ever be an exception here? Doesn't it get written later? throw new IllegalStateException("Caught exception creating a PDImageXObject from a bufferedImage", ioe); } jpegMap.put(bufferedImage, temp); } return temp; }
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; }
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; }