diff --git a/dist/ImageCrop.mpk b/dist/ImageCrop.mpk index aba7708..5316088 100644 Binary files a/dist/ImageCrop.mpk and b/dist/ImageCrop.mpk differ diff --git a/src_module/ImageCrop.mpr b/src_module/ImageCrop.mpr index dcb4596..2d23389 100644 Binary files a/src_module/ImageCrop.mpr and b/src_module/ImageCrop.mpr differ diff --git a/src_module/javasource/imagecrop/actions/CopyFileContent.java b/src_module/javasource/imagecrop/actions/CopyFileContent.java index 2c955b2..ee8aebd 100644 --- a/src_module/javasource/imagecrop/actions/CopyFileContent.java +++ b/src_module/javasource/imagecrop/actions/CopyFileContent.java @@ -14,14 +14,17 @@ import com.mendix.systemwideinterfaces.core.IMendixObject; import com.mendix.webui.CustomJavaAction; -public class CopyFileContent extends CustomJavaAction +/** + * + */ +public class CopyFileContent extends CustomJavaAction { private IMendixObject OriginalFile; private IMendixObject TargetFile; - private java.lang.Long thumbWidth; - private java.lang.Long thumbHeight; + private Long thumbWidth; + private Long thumbHeight; - public CopyFileContent(IContext context, IMendixObject OriginalFile, IMendixObject TargetFile, java.lang.Long thumbWidth, java.lang.Long thumbHeight) + public CopyFileContent(IContext context, IMendixObject OriginalFile, IMendixObject TargetFile, Long thumbWidth, Long thumbHeight) { super(context); this.OriginalFile = OriginalFile; @@ -31,7 +34,7 @@ public CopyFileContent(IContext context, IMendixObject OriginalFile, IMendixObje } @Override - public java.lang.Boolean executeAction() throws Exception + public Boolean executeAction() throws Exception { // BEGIN USER CODE Core.storeImageDocumentContent(getContext(), this.TargetFile, Core.getFileDocumentContent(getContext(), this.OriginalFile), this.thumbWidth.intValue(), this.thumbHeight.intValue()); @@ -44,7 +47,7 @@ public java.lang.Boolean executeAction() throws Exception * Returns a string representation of this action */ @Override - public java.lang.String toString() + public String toString() { return "CopyFileContent"; } diff --git a/src_module/javasource/imagecrop/actions/CreateBWImage.java b/src_module/javasource/imagecrop/actions/CreateBWImage.java index 5459115..e7b6c70 100644 --- a/src_module/javasource/imagecrop/actions/CreateBWImage.java +++ b/src_module/javasource/imagecrop/actions/CreateBWImage.java @@ -17,30 +17,35 @@ import com.mendix.webui.CustomJavaAction; import imagecrop.implementation.ImageUtil; -public class CreateBWImage extends CustomJavaAction +/** + * + */ +public class CreateBWImage extends CustomJavaAction { private IMendixObject ColorImage; private IMendixObject BWImage; - private java.lang.Long thumbnailWidth; - private java.lang.Long thumbnailHeight; + private Long thumbnailWidth; + private Long thumbnailHeight; + private java.math.BigDecimal jpegCompressionQuality; - public CreateBWImage(IContext context, IMendixObject ColorImage, IMendixObject BWImage, java.lang.Long thumbnailWidth, java.lang.Long thumbnailHeight) + public CreateBWImage(IContext context, IMendixObject ColorImage, IMendixObject BWImage, Long thumbnailWidth, Long thumbnailHeight, java.math.BigDecimal jpegCompressionQuality) { super(context); this.ColorImage = ColorImage; this.BWImage = BWImage; this.thumbnailWidth = thumbnailWidth; this.thumbnailHeight = thumbnailHeight; + this.jpegCompressionQuality = jpegCompressionQuality; } @Override - public java.lang.Boolean executeAction() throws Exception + public Boolean executeAction() throws Exception { // BEGIN USER CODE InputStream is = null; InputStream stream = null; try { - ImageUtil.processImageBW(this.getContext(), BWImage, ColorImage, thumbnailWidth.intValue(), thumbnailHeight.intValue()); + ImageUtil.processImageBW(this.getContext(), BWImage, ColorImage, thumbnailWidth.intValue(), thumbnailHeight.intValue(), jpegCompressionQuality.floatValue()); } catch (IOException e) { Core.getLogger(this.toString()).error(e); @@ -59,7 +64,7 @@ public java.lang.Boolean executeAction() throws Exception * Returns a string representation of this action */ @Override - public java.lang.String toString() + public String toString() { return "CreateBWImage"; } diff --git a/src_module/javasource/imagecrop/actions/CropImage.java b/src_module/javasource/imagecrop/actions/CropImage.java index e3720d6..05a6f52 100644 --- a/src_module/javasource/imagecrop/actions/CropImage.java +++ b/src_module/javasource/imagecrop/actions/CropImage.java @@ -14,16 +14,20 @@ import com.mendix.webui.CustomJavaAction; import imagecrop.implementation.ImageUtil; -public class CropImage extends CustomJavaAction +/** + * + */ +public class CropImage extends CustomJavaAction { private IMendixObject __cropImgObj; private imagecrop.proxies.CropImage cropImgObj; - private java.lang.Long imageWidth; - private java.lang.Long imageHeight; - private java.lang.Long thumbnailWidth; - private java.lang.Long thumbnailHeight; + private Long imageWidth; + private Long imageHeight; + private Long thumbnailWidth; + private Long thumbnailHeight; + private java.math.BigDecimal jpegCompressionQuality; - public CropImage(IContext context, IMendixObject cropImgObj, java.lang.Long imageWidth, java.lang.Long imageHeight, java.lang.Long thumbnailWidth, java.lang.Long thumbnailHeight) + public CropImage(IContext context, IMendixObject cropImgObj, Long imageWidth, Long imageHeight, Long thumbnailWidth, Long thumbnailHeight, java.math.BigDecimal jpegCompressionQuality) { super(context); this.__cropImgObj = cropImgObj; @@ -31,10 +35,11 @@ public CropImage(IContext context, IMendixObject cropImgObj, java.lang.Long imag this.imageHeight = imageHeight; this.thumbnailWidth = thumbnailWidth; this.thumbnailHeight = thumbnailHeight; + this.jpegCompressionQuality = jpegCompressionQuality; } @Override - public java.lang.Boolean executeAction() throws Exception + public Boolean executeAction() throws Exception { this.cropImgObj = __cropImgObj == null ? null : imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj); @@ -59,7 +64,9 @@ public java.lang.Boolean executeAction() throws Exception cropWidth = Math.round(this.cropImgObj.getcrop_width() * ratio); } - ImageUtil.processImage(this.getContext(), cropImgObj.getMendixObject(), cropWidth, cropHeight, thumbnailWidth.intValue(), thumbnailHeight.intValue(), true, x1, y1, x2, y2); + ImageUtil.processImage(this.getContext(), cropImgObj.getMendixObject(), + cropWidth, cropHeight, thumbnailWidth.intValue(), thumbnailHeight.intValue(), + true, x1, y1, x2, y2, jpegCompressionQuality.floatValue()); return true; } else @@ -71,7 +78,7 @@ public java.lang.Boolean executeAction() throws Exception * Returns a string representation of this action */ @Override - public java.lang.String toString() + public String toString() { return "CropImage"; } diff --git a/src_module/javasource/imagecrop/actions/ScaleImage.java b/src_module/javasource/imagecrop/actions/ScaleImage.java index e3158a9..9fa5909 100644 --- a/src_module/javasource/imagecrop/actions/ScaleImage.java +++ b/src_module/javasource/imagecrop/actions/ScaleImage.java @@ -14,23 +14,28 @@ import com.mendix.webui.CustomJavaAction; import imagecrop.implementation.ImageUtil; -public class ScaleImage extends CustomJavaAction +/** + * + */ +public class ScaleImage extends CustomJavaAction { private IMendixObject __cropImgObj; private imagecrop.proxies.CropImage cropImgObj; - private java.lang.Long thumbnailWidth; - private java.lang.Long thumbnailHeight; + private Long thumbnailWidth; + private Long thumbnailHeight; + private java.math.BigDecimal jpegCompressionQuality; - public ScaleImage(IContext context, IMendixObject cropImgObj, java.lang.Long thumbnailWidth, java.lang.Long thumbnailHeight) + public ScaleImage(IContext context, IMendixObject cropImgObj, Long thumbnailWidth, Long thumbnailHeight, java.math.BigDecimal jpegCompressionQuality) { super(context); this.__cropImgObj = cropImgObj; this.thumbnailWidth = thumbnailWidth; this.thumbnailHeight = thumbnailHeight; + this.jpegCompressionQuality = jpegCompressionQuality; } @Override - public java.lang.Boolean executeAction() throws Exception + public Boolean executeAction() throws Exception { this.cropImgObj = __cropImgObj == null ? null : imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj); @@ -45,7 +50,7 @@ public java.lang.Boolean executeAction() throws Exception ImageUtil.processImage(this.getContext(), cropImgObj.getMendixObject(), newWidth, newHeight, thumbnailWidth.intValue(), thumbnailHeight.intValue(), - false, 0, 0, 0, 0); + false, 0, 0, 0, 0, jpegCompressionQuality.floatValue()); return true; } else { @@ -58,7 +63,7 @@ public java.lang.Boolean executeAction() throws Exception * Returns a string representation of this action */ @Override - public java.lang.String toString() + public String toString() { return "ScaleImage"; } diff --git a/src_module/javasource/imagecrop/actions/SetInitialImageProps.java b/src_module/javasource/imagecrop/actions/SetInitialImageProps.java index 163e117..c2d258b 100644 --- a/src_module/javasource/imagecrop/actions/SetInitialImageProps.java +++ b/src_module/javasource/imagecrop/actions/SetInitialImageProps.java @@ -18,7 +18,10 @@ import com.mendix.webui.CustomJavaAction; import com.mendix.systemwideinterfaces.core.IMendixObject; -public class SetInitialImageProps extends CustomJavaAction +/** + * + */ +public class SetInitialImageProps extends CustomJavaAction { private IMendixObject UploadedImage; @@ -29,7 +32,7 @@ public SetInitialImageProps(IContext context, IMendixObject UploadedImage) } @Override - public java.lang.Boolean executeAction() throws Exception + public Boolean executeAction() throws Exception { // BEGIN USER CODE try (InputStream is = Core.getImage(getContext(), this.UploadedImage, false)) @@ -56,7 +59,7 @@ public java.lang.Boolean executeAction() throws Exception * Returns a string representation of this action */ @Override - public java.lang.String toString() + public String toString() { return "SetInitialImageProps"; } diff --git a/src_module/javasource/imagecrop/implementation/ImageUtil.java b/src_module/javasource/imagecrop/implementation/ImageUtil.java index 237436a..6b3749a 100644 --- a/src_module/javasource/imagecrop/implementation/ImageUtil.java +++ b/src_module/javasource/imagecrop/implementation/ImageUtil.java @@ -8,11 +8,13 @@ import java.io.IOException; import java.io.InputStream; import java.util.Iterator; - +import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageReader; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; import javax.imageio.stream.ImageInputStream; - +import javax.imageio.stream.MemoryCacheImageOutputStream; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.systemwideinterfaces.core.IMendixObject; @@ -24,26 +26,26 @@ public ImageUtil() { } public static void processImageBW( IContext context, IMendixObject bwImageObj, IMendixObject colorImageObj, - int thumbnailWidth, int thumbnailHeight) throws IOException { + int thumbnailWidth, int thumbnailHeight, float jpegCompressionQuality) throws IOException { try ( InputStream is = Core.getImage(context, colorImageObj, false) ) { - BufferedImage original = ImageIO.read(is); - BufferedImage alteredImage = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB); + BufferedImage originalImage = ImageIO.read(is); + BufferedImage alteredImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB); ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); - op.filter(original, alteredImage); + op.filter(originalImage, alteredImage); String formatName = getFormatName(context, colorImageObj); - write(context, bwImageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight); + write(context, bwImageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight, jpegCompressionQuality); } } public static void processImage(IContext context, IMendixObject imageObj, int width, int height, int thumbnailWidth, int thumbnailHeight, - Boolean crop, int x1, int y1, int x2, int y2) { + Boolean crop, int x1, int y1, int x2, int y2, float jpegCompressionQuality) { try ( InputStream is = Core.getImage(context, imageObj, false) ) { BufferedImage originalImage = ImageIO.read(is); @@ -58,7 +60,7 @@ public static void processImage(IContext context, IMendixObject imageObj, String formatName = getFormatName(context, imageObj); - write(context, imageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight); + write(context, imageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight, jpegCompressionQuality); } catch (IOException e) { Core.getLogger("ImageCrop").error(e); } @@ -88,10 +90,29 @@ private static String getFormatName(IContext context, IMendixObject imageObj) th } private static void write( IContext context, IMendixObject imageObj, BufferedImage alteredImage, String formatName, - int thumbnailWidth, int thumbnailHeight) throws IOException + int thumbnailWidth, int thumbnailHeight, float jpegCompressionQuality) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); - ImageIO.write(alteredImage, formatName, os); + + + if (formatName.toLowerCase().equals("jpeg") || formatName.toLowerCase().equals("jpg") ) { + float compQuality = jpegCompressionQuality; + if (compQuality < 0f) { + compQuality = 0f; + } else if (compQuality > 1f) { + compQuality = 1f; + } + + ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); + ImageWriteParam param = writer.getDefaultWriteParam(); + param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // Needed see javadoc + param.setCompressionQuality(compQuality); // Highest quality + writer.setOutput(new MemoryCacheImageOutputStream(os)); + writer.write(null, new IIOImage(alteredImage, null, null), param); + } else { + ImageIO.write(alteredImage, formatName, os); + } + try ( InputStream stream = new ByteArrayInputStream(os.toByteArray()) ) { Core.storeImageDocumentContent(context, imageObj, stream,