pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1576867 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util: ImageIOUtil.java JPEGUtil.java
Date Wed, 12 Mar 2014 19:27:41 GMT
Author: tilman
Date: Wed Mar 12 19:27:41 2014
New Revision: 1576867

URL: http://svn.apache.org/r1576867
Log:
PDFBOX-1977: set and test resolution for JPEG images

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java?rev=1576867&r1=1576866&r2=1576867&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java Wed Mar 12 19:27:41
2014
@@ -152,14 +152,25 @@ public class ImageIOUtil
                 }
             }
 
-            // metadata
-            setDPI(metadata, dpi, formatName);
-
-            // TIFF metadata
             if (formatName.toLowerCase().startsWith("tif"))
             {
+                // TIFF metadata
                 TIFFUtil.updateMetadata(metadata, image, dpi);
             }
+            else if ("jpeg".equals(formatName.toLowerCase())
+                    || "jpg".equals(formatName.toLowerCase()))
+            {
+                // This segment must be run before other meta operations,
+                // or else "IIOInvalidTreeException: Invalid node: app0JFIF"
+                // The other (general) "meta" methods may not be used, because
+                // this will break the reading of the meta data in tests
+                JPEGUtil.updateMetadata(metadata, dpi);
+            }
+            else
+            {
+                // metadata
+                setDPI(metadata, dpi, formatName);
+            }
 
             // write
             imageOutput = ImageIO.createImageOutputStream(output);

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java?rev=1576867&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java Wed Mar 12 19:27:41
2014
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.util;
+
+import java.awt.image.BufferedImage;
+import javax.imageio.metadata.IIOInvalidTreeException;
+import javax.imageio.metadata.IIOMetadata;
+import org.w3c.dom.Element;
+
+/**
+ *
+ * @author Tilman Hausherr
+ */
+class JPEGUtil
+{
+    static final String JPEG_NATIVE_FORMAT = "javax_imageio_jpeg_image_1.0";
+
+    static void updateMetadata(IIOMetadata metadata, int dpi) throws IIOInvalidTreeException
+    {
+        // https://svn.apache.org/repos/asf/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOJPEGImageWriter.java
+        // http://docs.oracle.com/javase/6/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html
+
+        Element root = (Element) metadata.getAsTree(JPEG_NATIVE_FORMAT);
+        Element child = (Element) root.getElementsByTagName("app0JFIF").item(0);
+        child.setAttribute("resUnits", "1"); // inch
+        child.setAttribute("Xdensity", Integer.toString(dpi));
+        child.setAttribute("Ydensity", Integer.toString(dpi));
+        metadata.mergeTree(JPEG_NATIVE_FORMAT, root);
+    }
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message