Skip to content

Commit d76b371

Browse files
authored
Merge pull request #8 from mendix/jpulleman-patch-1
Add files via upload
2 parents 45cd4bf + 2a6cd2f commit d76b371

File tree

8 files changed

+87
-43
lines changed

8 files changed

+87
-43
lines changed

dist/ImageCrop.mpk

5.13 KB
Binary file not shown.

src_module/ImageCrop.mpr

42 KB
Binary file not shown.

src_module/javasource/imagecrop/actions/CopyFileContent.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
import com.mendix.systemwideinterfaces.core.IMendixObject;
1515
import com.mendix.webui.CustomJavaAction;
1616

17-
public class CopyFileContent extends CustomJavaAction<java.lang.Boolean>
17+
/**
18+
*
19+
*/
20+
public class CopyFileContent extends CustomJavaAction<Boolean>
1821
{
1922
private IMendixObject OriginalFile;
2023
private IMendixObject TargetFile;
21-
private java.lang.Long thumbWidth;
22-
private java.lang.Long thumbHeight;
24+
private Long thumbWidth;
25+
private Long thumbHeight;
2326

24-
public CopyFileContent(IContext context, IMendixObject OriginalFile, IMendixObject TargetFile, java.lang.Long thumbWidth, java.lang.Long thumbHeight)
27+
public CopyFileContent(IContext context, IMendixObject OriginalFile, IMendixObject TargetFile, Long thumbWidth, Long thumbHeight)
2528
{
2629
super(context);
2730
this.OriginalFile = OriginalFile;
@@ -31,7 +34,7 @@ public CopyFileContent(IContext context, IMendixObject OriginalFile, IMendixObje
3134
}
3235

3336
@Override
34-
public java.lang.Boolean executeAction() throws Exception
37+
public Boolean executeAction() throws Exception
3538
{
3639
// BEGIN USER CODE
3740
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
4447
* Returns a string representation of this action
4548
*/
4649
@Override
47-
public java.lang.String toString()
50+
public String toString()
4851
{
4952
return "CopyFileContent";
5053
}

src_module/javasource/imagecrop/actions/CreateBWImage.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,35 @@
1717
import com.mendix.webui.CustomJavaAction;
1818
import imagecrop.implementation.ImageUtil;
1919

20-
public class CreateBWImage extends CustomJavaAction<java.lang.Boolean>
20+
/**
21+
*
22+
*/
23+
public class CreateBWImage extends CustomJavaAction<Boolean>
2124
{
2225
private IMendixObject ColorImage;
2326
private IMendixObject BWImage;
24-
private java.lang.Long thumbnailWidth;
25-
private java.lang.Long thumbnailHeight;
27+
private Long thumbnailWidth;
28+
private Long thumbnailHeight;
29+
private java.math.BigDecimal jpegCompressionQuality;
2630

27-
public CreateBWImage(IContext context, IMendixObject ColorImage, IMendixObject BWImage, java.lang.Long thumbnailWidth, java.lang.Long thumbnailHeight)
31+
public CreateBWImage(IContext context, IMendixObject ColorImage, IMendixObject BWImage, Long thumbnailWidth, Long thumbnailHeight, java.math.BigDecimal jpegCompressionQuality)
2832
{
2933
super(context);
3034
this.ColorImage = ColorImage;
3135
this.BWImage = BWImage;
3236
this.thumbnailWidth = thumbnailWidth;
3337
this.thumbnailHeight = thumbnailHeight;
38+
this.jpegCompressionQuality = jpegCompressionQuality;
3439
}
3540

3641
@Override
37-
public java.lang.Boolean executeAction() throws Exception
42+
public Boolean executeAction() throws Exception
3843
{
3944
// BEGIN USER CODE
4045
InputStream is = null;
4146
InputStream stream = null;
4247
try {
43-
ImageUtil.processImageBW(this.getContext(), BWImage, ColorImage, thumbnailWidth.intValue(), thumbnailHeight.intValue());
48+
ImageUtil.processImageBW(this.getContext(), BWImage, ColorImage, thumbnailWidth.intValue(), thumbnailHeight.intValue(), jpegCompressionQuality.floatValue());
4449
}
4550
catch (IOException e) {
4651
Core.getLogger(this.toString()).error(e);
@@ -59,7 +64,7 @@ public java.lang.Boolean executeAction() throws Exception
5964
* Returns a string representation of this action
6065
*/
6166
@Override
62-
public java.lang.String toString()
67+
public String toString()
6368
{
6469
return "CreateBWImage";
6570
}

src_module/javasource/imagecrop/actions/CropImage.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,32 @@
1414
import com.mendix.webui.CustomJavaAction;
1515
import imagecrop.implementation.ImageUtil;
1616

17-
public class CropImage extends CustomJavaAction<java.lang.Boolean>
17+
/**
18+
*
19+
*/
20+
public class CropImage extends CustomJavaAction<Boolean>
1821
{
1922
private IMendixObject __cropImgObj;
2023
private imagecrop.proxies.CropImage cropImgObj;
21-
private java.lang.Long imageWidth;
22-
private java.lang.Long imageHeight;
23-
private java.lang.Long thumbnailWidth;
24-
private java.lang.Long thumbnailHeight;
24+
private Long imageWidth;
25+
private Long imageHeight;
26+
private Long thumbnailWidth;
27+
private Long thumbnailHeight;
28+
private java.math.BigDecimal jpegCompressionQuality;
2529

26-
public CropImage(IContext context, IMendixObject cropImgObj, java.lang.Long imageWidth, java.lang.Long imageHeight, java.lang.Long thumbnailWidth, java.lang.Long thumbnailHeight)
30+
public CropImage(IContext context, IMendixObject cropImgObj, Long imageWidth, Long imageHeight, Long thumbnailWidth, Long thumbnailHeight, java.math.BigDecimal jpegCompressionQuality)
2731
{
2832
super(context);
2933
this.__cropImgObj = cropImgObj;
3034
this.imageWidth = imageWidth;
3135
this.imageHeight = imageHeight;
3236
this.thumbnailWidth = thumbnailWidth;
3337
this.thumbnailHeight = thumbnailHeight;
38+
this.jpegCompressionQuality = jpegCompressionQuality;
3439
}
3540

3641
@Override
37-
public java.lang.Boolean executeAction() throws Exception
42+
public Boolean executeAction() throws Exception
3843
{
3944
this.cropImgObj = __cropImgObj == null ? null : imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj);
4045

@@ -59,7 +64,9 @@ public java.lang.Boolean executeAction() throws Exception
5964
cropWidth = Math.round(this.cropImgObj.getcrop_width() * ratio);
6065
}
6166

62-
ImageUtil.processImage(this.getContext(), cropImgObj.getMendixObject(), cropWidth, cropHeight, thumbnailWidth.intValue(), thumbnailHeight.intValue(), true, x1, y1, x2, y2);
67+
ImageUtil.processImage(this.getContext(), cropImgObj.getMendixObject(),
68+
cropWidth, cropHeight, thumbnailWidth.intValue(), thumbnailHeight.intValue(),
69+
true, x1, y1, x2, y2, jpegCompressionQuality.floatValue());
6370

6471
return true;
6572
} else
@@ -71,7 +78,7 @@ public java.lang.Boolean executeAction() throws Exception
7178
* Returns a string representation of this action
7279
*/
7380
@Override
74-
public java.lang.String toString()
81+
public String toString()
7582
{
7683
return "CropImage";
7784
}

src_module/javasource/imagecrop/actions/ScaleImage.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@
1414
import com.mendix.webui.CustomJavaAction;
1515
import imagecrop.implementation.ImageUtil;
1616

17-
public class ScaleImage extends CustomJavaAction<java.lang.Boolean>
17+
/**
18+
*
19+
*/
20+
public class ScaleImage extends CustomJavaAction<Boolean>
1821
{
1922
private IMendixObject __cropImgObj;
2023
private imagecrop.proxies.CropImage cropImgObj;
21-
private java.lang.Long thumbnailWidth;
22-
private java.lang.Long thumbnailHeight;
24+
private Long thumbnailWidth;
25+
private Long thumbnailHeight;
26+
private java.math.BigDecimal jpegCompressionQuality;
2327

24-
public ScaleImage(IContext context, IMendixObject cropImgObj, java.lang.Long thumbnailWidth, java.lang.Long thumbnailHeight)
28+
public ScaleImage(IContext context, IMendixObject cropImgObj, Long thumbnailWidth, Long thumbnailHeight, java.math.BigDecimal jpegCompressionQuality)
2529
{
2630
super(context);
2731
this.__cropImgObj = cropImgObj;
2832
this.thumbnailWidth = thumbnailWidth;
2933
this.thumbnailHeight = thumbnailHeight;
34+
this.jpegCompressionQuality = jpegCompressionQuality;
3035
}
3136

3237
@Override
33-
public java.lang.Boolean executeAction() throws Exception
38+
public Boolean executeAction() throws Exception
3439
{
3540
this.cropImgObj = __cropImgObj == null ? null : imagecrop.proxies.CropImage.initialize(getContext(), __cropImgObj);
3641

@@ -45,7 +50,7 @@ public java.lang.Boolean executeAction() throws Exception
4550

4651
ImageUtil.processImage(this.getContext(), cropImgObj.getMendixObject(),
4752
newWidth, newHeight, thumbnailWidth.intValue(), thumbnailHeight.intValue(),
48-
false, 0, 0, 0, 0);
53+
false, 0, 0, 0, 0, jpegCompressionQuality.floatValue());
4954

5055
return true;
5156
} else {
@@ -58,7 +63,7 @@ public java.lang.Boolean executeAction() throws Exception
5863
* Returns a string representation of this action
5964
*/
6065
@Override
61-
public java.lang.String toString()
66+
public String toString()
6267
{
6368
return "ScaleImage";
6469
}

src_module/javasource/imagecrop/actions/SetInitialImageProps.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
import com.mendix.webui.CustomJavaAction;
1919
import com.mendix.systemwideinterfaces.core.IMendixObject;
2020

21-
public class SetInitialImageProps extends CustomJavaAction<java.lang.Boolean>
21+
/**
22+
*
23+
*/
24+
public class SetInitialImageProps extends CustomJavaAction<Boolean>
2225
{
2326
private IMendixObject UploadedImage;
2427

@@ -29,7 +32,7 @@ public SetInitialImageProps(IContext context, IMendixObject UploadedImage)
2932
}
3033

3134
@Override
32-
public java.lang.Boolean executeAction() throws Exception
35+
public Boolean executeAction() throws Exception
3336
{
3437
// BEGIN USER CODE
3538
try (InputStream is = Core.getImage(getContext(), this.UploadedImage, false))
@@ -56,7 +59,7 @@ public java.lang.Boolean executeAction() throws Exception
5659
* Returns a string representation of this action
5760
*/
5861
@Override
59-
public java.lang.String toString()
62+
public String toString()
6063
{
6164
return "SetInitialImageProps";
6265
}

src_module/javasource/imagecrop/implementation/ImageUtil.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import java.io.IOException;
99
import java.io.InputStream;
1010
import java.util.Iterator;
11-
11+
import javax.imageio.IIOImage;
1212
import javax.imageio.ImageIO;
1313
import javax.imageio.ImageReader;
14+
import javax.imageio.ImageWriteParam;
15+
import javax.imageio.ImageWriter;
1416
import javax.imageio.stream.ImageInputStream;
15-
17+
import javax.imageio.stream.MemoryCacheImageOutputStream;
1618
import com.mendix.core.Core;
1719
import com.mendix.systemwideinterfaces.core.IContext;
1820
import com.mendix.systemwideinterfaces.core.IMendixObject;
@@ -24,26 +26,26 @@ public ImageUtil() {
2426
}
2527

2628
public static void processImageBW( IContext context, IMendixObject bwImageObj, IMendixObject colorImageObj,
27-
int thumbnailWidth, int thumbnailHeight) throws IOException {
29+
int thumbnailWidth, int thumbnailHeight, float jpegCompressionQuality) throws IOException {
2830

2931
try ( InputStream is = Core.getImage(context, colorImageObj, false) )
3032
{
31-
BufferedImage original = ImageIO.read(is);
32-
BufferedImage alteredImage = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
33+
BufferedImage originalImage = ImageIO.read(is);
34+
BufferedImage alteredImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
3335

3436
ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
35-
op.filter(original, alteredImage);
37+
op.filter(originalImage, alteredImage);
3638

3739
String formatName = getFormatName(context, colorImageObj);
3840

39-
write(context, bwImageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight);
41+
write(context, bwImageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight, jpegCompressionQuality);
4042
}
4143
}
4244

4345

4446
public static void processImage(IContext context, IMendixObject imageObj,
4547
int width, int height, int thumbnailWidth, int thumbnailHeight,
46-
Boolean crop, int x1, int y1, int x2, int y2) {
48+
Boolean crop, int x1, int y1, int x2, int y2, float jpegCompressionQuality) {
4749
try ( InputStream is = Core.getImage(context, imageObj, false) )
4850
{
4951
BufferedImage originalImage = ImageIO.read(is);
@@ -58,7 +60,7 @@ public static void processImage(IContext context, IMendixObject imageObj,
5860

5961
String formatName = getFormatName(context, imageObj);
6062

61-
write(context, imageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight);
63+
write(context, imageObj, alteredImage, formatName, thumbnailWidth, thumbnailHeight, jpegCompressionQuality);
6264
} catch (IOException e) {
6365
Core.getLogger("ImageCrop").error(e);
6466
}
@@ -88,10 +90,29 @@ private static String getFormatName(IContext context, IMendixObject imageObj) th
8890
}
8991

9092
private static void write( IContext context, IMendixObject imageObj, BufferedImage alteredImage, String formatName,
91-
int thumbnailWidth, int thumbnailHeight) throws IOException
93+
int thumbnailWidth, int thumbnailHeight, float jpegCompressionQuality) throws IOException
9294
{
9395
ByteArrayOutputStream os = new ByteArrayOutputStream();
94-
ImageIO.write(alteredImage, formatName, os);
96+
97+
98+
if (formatName.toLowerCase().equals("jpeg") || formatName.toLowerCase().equals("jpg") ) {
99+
float compQuality = jpegCompressionQuality;
100+
if (compQuality < 0f) {
101+
compQuality = 0f;
102+
} else if (compQuality > 1f) {
103+
compQuality = 1f;
104+
}
105+
106+
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
107+
ImageWriteParam param = writer.getDefaultWriteParam();
108+
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // Needed see javadoc
109+
param.setCompressionQuality(compQuality); // Highest quality
110+
writer.setOutput(new MemoryCacheImageOutputStream(os));
111+
writer.write(null, new IIOImage(alteredImage, null, null), param);
112+
} else {
113+
ImageIO.write(alteredImage, formatName, os);
114+
}
115+
95116
try ( InputStream stream = new ByteArrayInputStream(os.toByteArray()) )
96117
{
97118
Core.storeImageDocumentContent(context, imageObj, stream,

0 commit comments

Comments
 (0)