pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1828604 - in /pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools: PDFToImage.java imageio/ImageIOUtil.java
Date Sat, 07 Apr 2018 18:33:48 GMT
Author: tilman
Date: Sat Apr  7 18:33:48 2018
New Revision: 1828604

URL: http://svn.apache.org/viewvc?rev=1828604&view=rev
Log:
PDFBOX-4186: add quality option to pdfbox-app, as suggested by Martin Hausner

Modified:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java?rev=1828604&r1=1828603&r2=1828604&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java Sat Apr  7 18:33:48
2018
@@ -55,6 +55,7 @@ public final class PDFToImage
     private static final String COLOR = "-color";
     private static final String RESOLUTION = "-resolution";
     private static final String DPI = "-dpi";
+    private static final String QUALITY = "-quality";
     private static final String CROPBOX = "-cropbox";
     private static final String TIME = "-time";
 
@@ -97,6 +98,7 @@ public final class PDFToImage
         int endPage = Integer.MAX_VALUE;
         String color = "rgb";
         int dpi;
+        float quality = 1.0f;
         float cropBoxLowerLeftX = 0;
         float cropBoxLowerLeftY = 0;
         float cropBoxUpperRightX = 0;
@@ -166,6 +168,10 @@ public final class PDFToImage
                     i++;
                     dpi = Integer.parseInt(args[i]);
                     break;
+                case QUALITY:
+                    i++;
+                    quality = Float.parseFloat(args[i]);
+                    break;
                 case CROPBOX:
                     i++;
                     cropBoxLowerLeftX = Float.valueOf(args[i]);
@@ -246,7 +252,7 @@ public final class PDFToImage
                 {
                     BufferedImage image = renderer.renderImageWithDPI(i, dpi, imageType);
                     String fileName = outputPrefix + (i + 1) + "." + imageFormat;
-                    success &= ImageIOUtil.writeImage(image, fileName, dpi);
+                    success &= ImageIOUtil.writeImage(image, fileName, dpi, quality);
                 }
 
                 // performance stats
@@ -282,8 +288,8 @@ public final class PDFToImage
             + "  -page <number>                   : The only page to extract (1-based)\n"
             + "  -startPage <int>                 : The first page to start extraction
(1-based)\n"
             + "  -endPage <int>                   : The last page to extract(inclusive)\n"
-            + "  -color <int>                     : The color depth (valid: bilevel,
gray, rgb, rgba)\n"
-            + "  -dpi <int>                       : The DPI of the output image\n"
+            + "  -color <int>                     : The color depth (valid: bilevel,
gray, rgb (default), rgba)\n"
+            + "  -quality <float>                 : The quality to be used when compressing
the image (0 < quality <= 1 (default))\n"
             + "  -cropbox <int> <int> <int> <int> : The page area
to export\n"
             + "  -time                            : Prints timing information to stdout\n"
             + "  <inputfile>                      : The PDF document to use\n";

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java?rev=1828604&r1=1828603&r2=1828604&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java Sat
Apr  7 18:33:48 2018
@@ -65,11 +65,30 @@ public final class ImageIOUtil
     public static boolean writeImage(BufferedImage image, String filename,
             int dpi) throws IOException
     {
+        return writeImage(image, filename, dpi, 1.0f);
+    }
+
+    /**
+     * Writes a buffered image to a file using the given image format.
+     * See {@link #writeImage(BufferedImage image, String formatName,
+     * OutputStream output, int dpi, float quality)} for more details.
+     *
+     * @param image the image to be written
+     * @param filename used to construct the filename for the individual image. Its suffix
will be
+     * used as the image format.
+     * @param dpi the resolution in dpi (dots per inch) to be used in metadata
+     * @param quality quality to be used when compressing the image (0 &lt; quality &lt;
1.0f)
+     * @return true if the image file was produced, false if there was an error.
+     * @throws IOException if an I/O error occurs
+     */
+    public static boolean writeImage(BufferedImage image, String filename,
+            int dpi, float quality) throws IOException
+    {
         File file = new File(filename);
-        try
-        (FileOutputStream output = new FileOutputStream(file)) {
+        try (FileOutputStream output = new FileOutputStream(file))
+        {
             String formatName = filename.substring(filename.lastIndexOf('.') + 1);
-            return writeImage(image, formatName, output, dpi);
+            return writeImage(image, formatName, output, dpi, quality);
         }
     }
 



Mime
View raw message