Author: tilman
Date: Sat Apr  7 18:32:57 2018
New Revision: 1828603

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

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

Modified: 
pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java?rev=1828603&r1=1828602&r2=1828603&view=diff
==============================================================================
--- 
pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java 
(original)
+++ 
pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java 
Sat Apr  7 18:32:57 2018
@@ -49,6 +49,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";
 
@@ -91,6 +92,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;
@@ -163,6 +165,11 @@ public final class PDFToImage
                 i++;
                 dpi = Integer.parseInt(args[i]);
             }
+            else if( args[i].equals( QUALITY ) )
+            {
+                i++;
+                quality = Float.parseFloat(args[i]);
+            }
             else if( args[i].equals( CROPBOX ) )
             {
                 i++;
@@ -246,7 +253,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
@@ -289,8 +296,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/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java?rev=1828603&r1=1828602&r2=1828603&view=diff
==============================================================================
--- 
pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
 (original)
+++ 
pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/imageio/ImageIOUtil.java
 Sat Apr  7 18:32:57 2018
@@ -65,12 +65,31 @@ 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);
         FileOutputStream output = new FileOutputStream(file);
         try
         {
             String formatName = filename.substring(filename.lastIndexOf('.') + 
1);
-            return writeImage(image, formatName, output, dpi);
+            return writeImage(image, formatName, output, dpi, quality);
         }
         finally
         {


Reply via email to