From: Mathieu Baudier Date: Sat, 12 Mar 2022 12:30:21 +0000 (+0100) Subject: Merge remote-tracking branch 'origin/unstable' into testing X-Git-Tag: v2.1.26~4 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=73f619d9a87672e9f6434123b2a741bdaf2f27e2;hp=ace1678a1f8d093801473cc84890d62546479b82;p=gpl%2Fargeo-suite.git Merge remote-tracking branch 'origin/unstable' into testing Conflicts: sdk/output-cms-rap.target sdk/output-cms-rcp.target --- diff --git a/Makefile b/Makefile index 10b7381..c1803c9 100644 --- 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 \ diff --git a/cnf/unstable.bnd b/cnf/unstable.bnd index 1de4beb..1f15381 100644 --- a/cnf/unstable.bnd +++ b/cnf/unstable.bnd @@ -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 index 0000000..7fd308d --- /dev/null +++ b/org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java @@ -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 inSupplier; + private Callable outSupplier; + + public ImageProcessor(Callable inSupplier, Callable 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 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 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()); + } + } + +} diff --git a/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java b/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java index 25d00f4..a5864d8 100644 --- a/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java +++ b/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java @@ -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); diff --git a/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java b/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java index 79f1769..504fbd2 100644 --- a/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java +++ b/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java @@ -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)) { diff --git a/sdk/output-cms-rap.target b/sdk/output-cms-rap.target index 980d3c2..b6f3036 100644 --- a/sdk/output-cms-rap.target +++ b/sdk/output-cms-rap.target @@ -11,5 +11,8 @@ + + + \ No newline at end of file diff --git a/sdk/output-cms-rcp.target b/sdk/output-cms-rcp.target index d740753..9e81871 100644 --- a/sdk/output-cms-rcp.target +++ b/sdk/output-cms-rcp.target @@ -11,5 +11,8 @@ + + + \ No newline at end of file