pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1787225 - in /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/graphics/image/JPEGFactory.java pdmodel/graphics/image/PDImageXObject.java util/filetypedetector/FileTypeDetector.java
Date Thu, 16 Mar 2017 18:28:58 GMT
Author: tilman
Date: Thu Mar 16 18:28:58 2017
New Revision: 1787225

URL: http://svn.apache.org/viewvc?rev=1787225&view=rev
Log:
PDFBOX-3716: add methods for byte array input, as suggested by Alex Yolkin

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java?rev=1787225&r1=1787224&r2=1787225&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java
Thu Mar 16 18:28:58 2017
@@ -71,8 +71,23 @@ public final class JPEGFactory
     public static PDImageXObject createFromStream(PDDocument document, InputStream stream)
             throws IOException
     {
+        return createFromByteArray(document, IOUtils.toByteArray(stream));
+    }
+
+    /**
+     * 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(IOUtils.toByteArray(stream));
+        ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
 
         // read image
         BufferedImage awtImage = readJPEG(byteStream);

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java?rev=1787225&r1=1787224&r2=1787225&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
Thu Mar 16 18:28:58 2017
@@ -22,6 +22,7 @@ import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
 import java.awt.image.WritableRaster;
 import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -285,6 +286,56 @@ public final class PDImageXObject extend
         throw new IllegalArgumentException("Image type not supported: " + file.getName());
     }
 
+    /**
+     * Create a PDImageXObject from bytes of an image file. The file format is determined
by the
+     * file content. The following file types are supported: jpg, jpeg, tif, tiff, gif, bmp
and png.
+     * This is a convenience method that calls {@link JPEGFactory#createFromBytesArray},
+     * {@link CCITTFactory#createFromFile} or {@link ImageIO#read} combined with
+     * {@link LosslessFactory#createFromImage}. (The later can also be used to create a
+     * PDImageXObject from a BufferedImage).
+     *
+     * @param byteArray bytes from an image file.
+     * @param document the document that shall use this PDImageXObject.
+     * @param name name of image file for exception messages, can be null.
+     * @return a PDImageXObject.
+     * @throws IOException if there is an error when reading the file or creating the
+     * PDImageXObject.
+     * @throws IllegalArgumentException if the image type is not supported.
+     */
+    public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray,
String name) throws IOException
+    {
+        FileType fileType;
+        try
+        {
+            fileType = FileTypeDetector.detectFileType(byteArray);
+        }
+        catch (IOException e)
+        {
+            throw new IOException("Could not determine file type: " + name, e);
+        }
+        if (fileType == null)
+        {
+            throw new IllegalArgumentException("Image type not supported: " + name);
+        }
+
+        if (fileType.equals(FileType.JPEG))
+        {
+            PDImageXObject imageXObject = JPEGFactory.createFromByteArray(document, byteArray);
+            return imageXObject;
+        }
+        if (fileType.equals(FileType.TIFF))
+        {
+            return CCITTFactory.createFromByteArray(document, byteArray);
+        }
+        if (fileType.equals(FileType.BMP) || fileType.equals(FileType.GIF) || fileType.equals(FileType.PNG))
+        {
+            ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
+            BufferedImage bim = ImageIO.read(bais);
+            return LosslessFactory.createFromImage(document, bim);
+        }
+        throw new IllegalArgumentException("Image type not supported: " + name);
+    }
+
     // repairs parameters using decode result
     private static PDStream repair(PDStream stream, COSInputStream input)
     {

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java?rev=1787225&r1=1787224&r2=1787225&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/filetypedetector/FileTypeDetector.java
Thu Mar 16 18:28:58 2017
@@ -110,4 +110,9 @@ public final class FileTypeDetector
         //noinspection ConstantConditions
         return root.find(bytes);
     }
+
+    public static FileType detectFileType(final byte[] fileBytes) throws IOException
+    {
+        return root.find(fileBytes);
+    }
 }



Mime
View raw message