Merge remote-tracking branch 'origin/unstable' into testing
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 12 Mar 2022 12:30:21 +0000 (13:30 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 12 Mar 2022 12:30:21 +0000 (13:30 +0100)
Conflicts:
sdk/output-cms-rap.target
sdk/output-cms-rcp.target

Makefile
cnf/unstable.bnd
org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java [new file with mode: 0644]
org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java
org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java
sdk/output-cms-rap.target
sdk/output-cms-rcp.target

index 10b73819a82235585eae51bf5f66cc7006bc3736..c1803c93b347559a47cf03c55429098335fc8ee1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,8 @@ org.argeo.tp.jetty \
 org.argeo.tp.eclipse.equinox \
 org.argeo.tp.eclipse.rap \
 org.argeo.tp.jcr \
+org.argeo.tp.formats \
+org.argeo.tp.gis \
 org.argeo.cms \
 org.argeo.cms.eclipse.rap \
 
index 1de4beb510a8b7f0f45bfef6604a6c106ae222a0..1f15381547f6786543e91cfcdec77ca2c827b22b 100644 (file)
@@ -1,6 +1,6 @@
 MAJOR=2
 MINOR=3
-MICRO=4
+MICRO=5
 qualifier=.next
 
 category=org.argeo.suite
diff --git a/org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java b/org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java
new file mode 100644 (file)
index 0000000..7fd308d
--- /dev/null
@@ -0,0 +1,321 @@
+package org.argeo.app.image;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.geom.AffineTransform;
+import java.awt.image.AffineTransformOp;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import javax.imageio.ImageIO;
+
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.Imaging;
+import org.apache.commons.imaging.common.ImageMetadata;
+import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem;
+import org.apache.commons.imaging.common.RationalNumber;
+import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
+import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
+import org.apache.commons.imaging.formats.tiff.TiffField;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
+import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
+import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
+import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
+import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
+import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
+
+public class ImageProcessor {
+       private Callable<InputStream> inSupplier;
+       private Callable<OutputStream> outSupplier;
+
+       public ImageProcessor(Callable<InputStream> inSupplier, Callable<OutputStream> outSupplier) {
+               super();
+               this.inSupplier = inSupplier;
+               this.outSupplier = outSupplier;
+       }
+
+       public void process() {
+               try {
+                       ImageMetadata metadata = null;
+                       Integer orientation = null;
+                       try (InputStream in = inSupplier.call()) {
+                               metadata = Imaging.getMetadata(in, null);
+                               orientation = getOrientation(metadata);
+                       }
+                       try (InputStream in = inSupplier.call()) {
+                               if (orientation != null && orientation != TiffTagConstants.ORIENTATION_VALUE_HORIZONTAL_NORMAL) {
+                                       BufferedImage sourceImage = ImageIO.read(in);
+                                       AffineTransform transform = getExifTransformation(orientation, sourceImage.getWidth(),
+                                                       sourceImage.getHeight());
+                                       BufferedImage targetImage = transformImage(sourceImage, orientation, transform);
+                                       Path temp = Files.createTempFile("image", ".jpg");
+                                       try {
+                                               try (OutputStream out = Files.newOutputStream(temp)) {
+                                                       ImageIO.write(targetImage, "jpeg", out);
+                                               }
+                                               copyWithMetadata(() -> Files.newInputStream(temp), metadata);
+                                       } finally {
+                                               Files.deleteIfExists(temp);
+                                       }
+                               } else {
+                                       try (OutputStream out = outSupplier.call()) {
+                                               copyWithMetadata(() -> in, metadata);
+                                       }
+                               }
+                       }
+               } catch (Exception e) {
+                       throw new RuntimeException("Cannot process image", e);
+               }
+       }
+
+       protected void copyWithMetadata(Callable<InputStream> inSupplier, ImageMetadata metadata) {
+               try (InputStream in = inSupplier.call(); OutputStream out = outSupplier.call();) {
+                       TiffOutputSet outputSet = null;
+                       if (metadata != null && metadata instanceof JpegImageMetadata) {
+                               final TiffImageMetadata exif = ((JpegImageMetadata) metadata).getExif();
+
+                               if (null != exif) {
+                                       outputSet = exif.getOutputSet();
+//                                     outputSet.getInteroperabilityDirectory().removeField(TiffTagConstants.TIFF_TAG_ORIENTATION);
+
+                                       for (TiffOutputDirectory dir : outputSet.getDirectories()) {
+//                                             TiffOutputField field = dir.findField(TiffTagConstants.TIFF_TAG_ORIENTATION);
+//                                             if (field != null) {
+                                               dir.removeField(TiffTagConstants.TIFF_TAG_ORIENTATION);
+                                               dir.removeField(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH);
+                                               dir.removeField(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH);
+                                               dir.removeField(ExifTagConstants.EXIF_TAG_EXIF_IMAGE_WIDTH);
+                                               dir.removeField(ExifTagConstants.EXIF_TAG_EXIF_IMAGE_LENGTH);
+//                                                     System.out.println("Removed orientation from " + dir.description());
+//                                             }
+                                       }
+                               }
+                       }
+
+                       if (null == outputSet) {
+                               outputSet = new TiffOutputSet();
+                       }
+                       new ExifRewriter().updateExifMetadataLossless(in, out, outputSet);
+               } catch (Exception e) {
+                       throw new RuntimeException("Could not update EXIF metadata", e);
+               }
+
+       }
+
+       public static BufferedImage transformImage(BufferedImage image, Integer orientation, AffineTransform transform)
+                       throws Exception {
+
+               AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
+
+               int width = image.getWidth();
+               int height = image.getHeight();
+               switch (orientation) {
+               case TiffTagConstants.ORIENTATION_VALUE_ROTATE_180:
+               case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_270_CW:
+               case TiffTagConstants.ORIENTATION_VALUE_ROTATE_90_CW:
+               case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_90_CW:
+               case TiffTagConstants.ORIENTATION_VALUE_ROTATE_270_CW:
+                       width = image.getHeight();
+                       height = image.getWidth();
+                       break;
+               }
+
+               BufferedImage destinationImage = new BufferedImage(width, height, image.getType());
+
+               Graphics2D g = destinationImage.createGraphics();
+               g.setBackground(Color.WHITE);
+               g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight());
+               destinationImage = op.filter(image, destinationImage);
+               return destinationImage;
+       }
+
+       public static int getOrientation(ImageMetadata metadata) {
+               if (metadata instanceof JpegImageMetadata) {
+                       JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
+                       TiffField field = jpegMetadata.findEXIFValue(TiffTagConstants.TIFF_TAG_ORIENTATION);
+                       if (field == null)
+                               return TiffTagConstants.ORIENTATION_VALUE_HORIZONTAL_NORMAL;
+                       try {
+                               return field.getIntValue();
+                       } catch (ImageReadException e) {
+                               throw new IllegalStateException(e);
+                       }
+               } else {
+                       throw new IllegalArgumentException("Unsupported metadata format " + metadata.getClass());
+               }
+       }
+
+       public static AffineTransform getExifTransformation(Integer orientation, int width, int height) {
+
+               AffineTransform t = new AffineTransform();
+
+               switch (orientation) {
+               case TiffTagConstants.ORIENTATION_VALUE_HORIZONTAL_NORMAL:
+                       return null;
+               case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL: // Flip X
+                       t.scale(-1.0, 1.0);
+                       t.translate(-width, 0);
+                       break;
+               case TiffTagConstants.ORIENTATION_VALUE_ROTATE_180: // PI rotation
+                       t.translate(width, height);
+                       t.rotate(Math.PI);
+                       break;
+               case TiffTagConstants.ORIENTATION_VALUE_MIRROR_VERTICAL: // Flip Y
+                       t.scale(1.0, -1.0);
+                       t.translate(0, -height);
+                       break;
+               case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_270_CW: // - PI/2 and Flip X
+                       t.rotate(-Math.PI / 2);
+                       t.scale(-1.0, 1.0);
+                       break;
+               case TiffTagConstants.ORIENTATION_VALUE_ROTATE_90_CW: // -PI/2 and -width
+                       t.translate(height, 0);
+                       t.rotate(Math.PI / 2);
+                       break;
+               case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_90_CW: // PI/2 and Flip
+                       t.scale(-1.0, 1.0);
+                       t.translate(-height, 0);
+                       t.translate(0, width);
+                       t.rotate(3 * Math.PI / 2);
+                       break;
+               case TiffTagConstants.ORIENTATION_VALUE_ROTATE_270_CW: // PI / 2
+                       t.translate(0, width);
+                       t.rotate(3 * Math.PI / 2);
+                       break;
+               }
+
+               return t;
+       }
+
+       public static void main(String[] args) throws Exception {
+               Path imagePath = Paths.get(args[0]);
+               Path targetPath = Paths.get(args[1]);
+
+               ImageProcessor imageProcessor = new ImageProcessor(() -> Files.newInputStream(imagePath),
+                               () -> Files.newOutputStream(targetPath));
+               imageProcessor.process();
+
+               try (InputStream in = Files.newInputStream(targetPath)) {
+                       metadataExample(in, null);
+               }
+
+       }
+
+       public static void metadataExample(InputStream in, String fileName) throws ImageReadException, IOException {
+               // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
+               final ImageMetadata metadata = Imaging.getMetadata(in, fileName);
+
+               // System.out.println(metadata);
+
+               if (metadata instanceof JpegImageMetadata) {
+                       final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
+
+                       // Jpeg EXIF metadata is stored in a TIFF-based directory structure
+                       // and is identified with TIFF tags.
+                       // Here we look for the "x resolution" tag, but
+                       // we could just as easily search for any other tag.
+                       //
+                       // see the TiffConstants file for a list of TIFF tags.
+
+                       // System.out.println("file: " + file.getPath());
+
+                       // print out various interesting EXIF tags.
+                       printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_XRESOLUTION);
+                       printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_DATE_TIME);
+                       printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
+                       printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED);
+                       printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_ISO);
+                       printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
+                       printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
+                       printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_BRIGHTNESS_VALUE);
+                       printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
+                       printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE);
+                       printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
+                       printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
+
+                       System.out.println();
+
+                       // simple interface to GPS data
+                       final TiffImageMetadata exifMetadata = jpegMetadata.getExif();
+                       if (null != exifMetadata) {
+                               final TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
+                               if (null != gpsInfo) {
+                                       final String gpsDescription = gpsInfo.toString();
+                                       final double longitude = gpsInfo.getLongitudeAsDegreesEast();
+                                       final double latitude = gpsInfo.getLatitudeAsDegreesNorth();
+
+                                       System.out.println("    " + "GPS Description: " + gpsDescription);
+                                       System.out.println("    " + "GPS Longitude (Degrees East): " + longitude);
+                                       System.out.println("    " + "GPS Latitude (Degrees North): " + latitude);
+                               }
+                       }
+
+                       // more specific example of how to manually access GPS values
+                       final TiffField gpsLatitudeRefField = jpegMetadata
+                                       .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
+                       final TiffField gpsLatitudeField = jpegMetadata
+                                       .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE);
+                       final TiffField gpsLongitudeRefField = jpegMetadata
+                                       .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
+                       final TiffField gpsLongitudeField = jpegMetadata
+                                       .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
+                       if (gpsLatitudeRefField != null && gpsLatitudeField != null && gpsLongitudeRefField != null
+                                       && gpsLongitudeField != null) {
+                               // all of these values are strings.
+                               final String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
+                               final RationalNumber[] gpsLatitude = (RationalNumber[]) (gpsLatitudeField.getValue());
+                               final String gpsLongitudeRef = (String) gpsLongitudeRefField.getValue();
+                               final RationalNumber[] gpsLongitude = (RationalNumber[]) gpsLongitudeField.getValue();
+
+                               final RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
+                               final RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
+                               final RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
+
+                               final RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
+                               final RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
+                               final RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
+
+                               // This will format the gps info like so:
+                               //
+                               // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
+                               // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E
+
+                               System.out.println("    " + "GPS Latitude: " + gpsLatitudeDegrees.toDisplayString() + " degrees, "
+                                               + gpsLatitudeMinutes.toDisplayString() + " minutes, " + gpsLatitudeSeconds.toDisplayString()
+                                               + " seconds " + gpsLatitudeRef);
+                               System.out.println("    " + "GPS Longitude: " + gpsLongitudeDegrees.toDisplayString() + " degrees, "
+                                               + gpsLongitudeMinutes.toDisplayString() + " minutes, " + gpsLongitudeSeconds.toDisplayString()
+                                               + " seconds " + gpsLongitudeRef);
+
+                       }
+
+                       System.out.println();
+
+                       final List<ImageMetadataItem> items = jpegMetadata.getItems();
+                       for (final ImageMetadataItem item : items) {
+                               System.out.println("    " + "item: " + item);
+
+                       }
+
+                       System.out.println();
+               }
+       }
+
+       private static void printTagValue(final JpegImageMetadata jpegMetadata, final TagInfo tagInfo) {
+               final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
+               if (field == null) {
+                       System.out.println(tagInfo.name + ": " + "Not Found.");
+               } else {
+                       System.out.println(tagInfo.name + ": " + field.getValueDescription());
+               }
+       }
+
+}
index 25d00f45f4176bc0902706f9d75a3a5b148a9dd9..a5864d83336206d6e81c7229c05aea3febb3ef97 100644 (file)
@@ -1,6 +1,8 @@
 package org.argeo.app.servlet.odk;
 
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.time.Instant;
 import java.time.ZoneId;
 import java.time.ZoneOffset;
@@ -21,11 +23,12 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.Part;
 
+import org.argeo.api.cms.CmsLog;
 import org.argeo.api.cms.CmsSession;
 import org.argeo.app.core.SuiteUtils;
+import org.argeo.app.image.ImageProcessor;
 import org.argeo.app.odk.OrxType;
 import org.argeo.app.xforms.FormSubmissionListener;
-import org.argeo.api.cms.CmsLog;
 import org.argeo.cms.auth.RemoteAuthRequest;
 import org.argeo.cms.auth.RemoteAuthUtils;
 import org.argeo.cms.jcr.CmsJcrUtils;
@@ -82,7 +85,22 @@ public class OdkSubmissionServlet extends HttpServlet {
                                                        ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
 
                                } else {
-                                       Node fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(), part.getInputStream());
+                                       Node fileNode;
+                                       if (part.getName().endsWith(".jpg")) {
+                                               // Fix metadata
+                                               Path temp = Files.createTempFile("image", ".jpg");
+                                               try {
+                                                       ImageProcessor imageProcessor = new ImageProcessor(() -> part.getInputStream(),
+                                                                       () -> Files.newOutputStream(temp));
+                                                       imageProcessor.process();
+                                                       fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(),
+                                                                       Files.newInputStream(temp));
+                                               } finally {
+                                                       Files.deleteIfExists(temp);
+                                               }
+                                       } else {
+                                               fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(), part.getInputStream());
+                                       }
                                        String contentType = part.getContentType();
                                        if (contentType != null) {
                                                fileNode.addMixin(NodeType.MIX_MIMETYPE);
index 79f1769257db9052126a49739143243c09946a97..504fbd2e3ed72d54d979aa50b822f03e57a0ecd0 100644 (file)
@@ -2,11 +2,15 @@ package org.argeo.app.ui;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.commons.io.IOUtils;
 import org.argeo.api.cms.CmsEditable;
 import org.argeo.api.cms.CmsEvent;
 import org.argeo.api.cms.CmsStyle;
@@ -232,13 +236,24 @@ public class SuiteUiUtils {
                        throws RepositoryException {
                Node content = fileNode.getNode(Node.JCR_CONTENT);
 
-//             try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
-//                     BufferedImage img = ImageIO.read(in);
-//                     System.out.println("width=" + img.getWidth() + ", height=" + img.getHeight());
-//             } catch (IOException e) {
-//                     throw new RuntimeException(e);
-//             }
+               boolean test = false;
+               if (test) {
+                       try (InputStream in = JcrUtils.getFileAsStream(fileNode);
+                                       OutputStream out = Files.newOutputStream(Paths.get("/home/mbaudier/tmp/" + fileNode.getName()));) {
+//                             BufferedImage img = ImageIO.read(in);
+//                             System.out.println(fileNode.getName() + ": width=" + img.getWidth() + ", height=" + img.getHeight());
+                               IOUtils.copy(in, out);
+                       } catch (IOException e) {
+                               throw new RuntimeException(e);
+                       }
 
+//                     try (InputStream in = JcrUtils.getFileAsStream(fileNode);) {
+//                             ImageData imageData = new ImageData(in);
+//                             System.out.println(fileNode.getName() + ": width=" + imageData.width + ", height=" + imageData.height);
+//                     } catch (IOException e) {
+//                             throw new RuntimeException(e);
+//                     }
+               }
                // TODO move it deeper in the middleware.
                if (!content.isNodeType(EntityType.box.get())) {
                        if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
index 980d3c21e3bd1cfb6870c0294db6c1dba2778835..b6f30363cf53921b1c9da171c11a93a1f4a74295 100644 (file)
@@ -11,5 +11,8 @@
                <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.sdk" type="Directory"/>
                <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.jcr" type="Directory"/>
                <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp" type="Directory"/>
+               <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.formats" type="Directory"/>
+               <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.poi" type="Directory"/>
+               <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.gis" type="Directory"/>
        </locations>
 </target>
\ No newline at end of file
index d7407535c8cbf889bd2c73462d96f796b5b3add8..9e818714e9a327e8bddfc3ff993e272f0143e7b4 100644 (file)
@@ -11,5 +11,8 @@
                <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.sdk" type="Directory"/>
                <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.jcr" type="Directory"/>
                <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp" type="Directory"/>
+               <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.formats" type="Directory"/>
+               <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.poi" type="Directory"/>
+               <location path="${project_loc:argeo-suite}/../output/a2/org.argeo.tp.gis" type="Directory"/>
        </locations>
 </target>
\ No newline at end of file