1- // This file was generated by Mendix Business Modeler.
1+ // This file was generated by Mendix Modeler.
22//
33// WARNING: Only the following code will be retained when actions are regenerated:
44// - the import list
1212import java .awt .image .BufferedImage ;
1313import java .io .ByteArrayInputStream ;
1414import java .io .ByteArrayOutputStream ;
15+ import java .io .IOException ;
1516import java .io .InputStream ;
1617import javax .imageio .ImageIO ;
1718import com .mendix .core .Core ;
2223/**
2324 *
2425 */
25- public class CropImage extends CustomJavaAction <Boolean >
26- {
26+ public class CropImage extends CustomJavaAction <Boolean > {
2727 private IMendixObject __cropImgObj ;
2828 private imagecrop .proxies .CropImage cropImgObj ;
2929 private Long imageWidth ;
3030 private Long imageHeight ;
3131 private Long thumbnailWidth ;
3232 private Long thumbnailHeight ;
3333
34- public CropImage (IContext context , IMendixObject cropImgObj , Long imageWidth , Long imageHeight , Long thumbnailWidth , Long thumbnailHeight )
35- {
34+ public CropImage (IContext context , IMendixObject cropImgObj , Long imageWidth , Long imageHeight , Long thumbnailWidth ,
35+ Long thumbnailHeight ) {
3636 super (context );
3737 this .__cropImgObj = cropImgObj ;
3838 this .imageWidth = imageWidth ;
@@ -42,48 +42,59 @@ public CropImage(IContext context, IMendixObject cropImgObj, Long imageWidth, Lo
4242 }
4343
4444 @ Override
45- public Boolean executeAction () throws Exception
46- {
47- this . cropImgObj = __cropImgObj == null ? null : imagecrop .proxies .CropImage .initialize (getContext (), __cropImgObj );
45+ public Boolean executeAction () throws Exception {
46+ this . cropImgObj = __cropImgObj == null ? null
47+ : imagecrop .proxies .CropImage .initialize (getContext (), __cropImgObj );
4848
4949 // BEGIN USER CODE
5050 int cropHeight = this .imageHeight .intValue ();
5151 int cropWidth = this .imageWidth .intValue ();
52-
52+
5353 int x1 = this .cropImgObj .getcrop_x1 ();
5454 int y1 = this .cropImgObj .getcrop_y1 ();
5555 int x2 = this .cropImgObj .getcrop_x2 ();
5656 int y2 = this .cropImgObj .getcrop_y2 ();
57-
57+
5858 if (x2 > 0 && y2 > 0 ) {
5959 if (cropHeight == 0 && cropWidth == 0 ) {
6060 cropHeight = this .cropImgObj .getcrop_height ();
6161 cropWidth = this .cropImgObj .getcrop_width ();
6262 } else if (cropHeight == 0 ) {
6363 float ratio = (float ) cropWidth / this .cropImgObj .getcrop_width ().floatValue ();
64- cropHeight = Math .round (this .cropImgObj .getcrop_height ()* ratio );
64+ cropHeight = Math .round (this .cropImgObj .getcrop_height () * ratio );
6565 } else if (cropWidth == 0 ) {
6666 float ratio = (float ) cropHeight / this .cropImgObj .getcrop_height ().floatValue ();
67- cropWidth = Math .round (this .cropImgObj .getcrop_width ()* ratio );
67+ cropWidth = Math .round (this .cropImgObj .getcrop_width () * ratio );
6868 }
69- InputStream is = Core .getImage (getContext (), this .cropImgObj .getMendixObject (), false );
70- BufferedImage originalImage = ImageIO .read (is );
7169
72- BufferedImage alteredImage = new BufferedImage ( cropWidth , cropHeight , BufferedImage . TYPE_INT_RGB ) ;
73- alteredImage . getGraphics (). drawImage ( originalImage , 0 , 0 , cropWidth , cropHeight , x1 , y1 , x2 , y2 , null ) ;
70+ InputStream is = null ;
71+ InputStream stream = null ;
7472
73+ try {
74+ is = Core .getImage (getContext (), this .cropImgObj .getMendixObject (), false );
75+ BufferedImage originalImage = ImageIO .read (is );
7576
76- String formatName = ScaleImage .getFormatName ( Core .getImage (getContext (), this .cropImgObj .getMendixObject (), false ) );
77-
78- ByteArrayOutputStream os = new ByteArrayOutputStream ();
79- ImageIO .write (alteredImage , formatName , os );
80- InputStream stream = new ByteArrayInputStream (os .toByteArray ());
81- Core .storeImageDocumentContent (getContext (), this .cropImgObj .getMendixObject (), stream ,
82- this .thumbnailWidth .intValue (), this .thumbnailHeight .intValue ());
83-
77+ BufferedImage alteredImage = new BufferedImage (cropWidth , cropHeight , BufferedImage .TYPE_INT_RGB );
78+ alteredImage .getGraphics ().drawImage (originalImage , 0 , 0 , cropWidth , cropHeight , x1 , y1 , x2 , y2 , null );
79+
80+ String formatName = ScaleImage
81+ .getFormatName (Core .getImage (getContext (), this .cropImgObj .getMendixObject (), false ));
82+
83+ ByteArrayOutputStream os = new ByteArrayOutputStream ();
84+ ImageIO .write (alteredImage , formatName , os );
85+ stream = new ByteArrayInputStream (os .toByteArray ());
86+ Core .storeImageDocumentContent (getContext (), this .cropImgObj .getMendixObject (), stream ,
87+ this .thumbnailWidth .intValue (), this .thumbnailHeight .intValue ());
88+ } catch (IOException e ) {
89+ Core .getLogger (this .toString ()).error (e );
90+ } finally {
91+ if (is != null )
92+ is .close ();
93+ if (stream != null )
94+ stream .close ();
95+ }
8496 return true ;
85- }
86- else
97+ } else
8798 return false ;
8899 // END USER CODE
89100 }
@@ -92,8 +103,7 @@ public Boolean executeAction() throws Exception
92103 * Returns a string representation of this action
93104 */
94105 @ Override
95- public String toString ()
96- {
106+ public String toString () {
97107 return "CropImage" ;
98108 }
99109
0 commit comments