← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1817: (gis) export image with png(transparent background) and jpeg(white background)

 

------------------------------------------------------------
revno: 1817
committer: Tran Thanh Tri <Tran Thanh Tri@TranThanhTri-PC>
branch nick: trunk
timestamp: Tue 2010-04-27 12:35:16 +0700
message:
  (gis) export image with png(transparent background) and jpeg(white background)
modified:
  dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/ExportImageAction.java
  dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/export/SVGUtils.java
  dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/index.html
  dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/script/index.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/ExportImageAction.java'
--- dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/ExportImageAction.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/ExportImageAction.java	2010-04-27 05:35:16 +0000
@@ -45,11 +45,12 @@
  * @author Tran Thanh Tri
  * @version $Id$
  */
+@SuppressWarnings( "serial" )
 public class ExportImageAction
     extends StreamActionSupport
 {
     private static final Log log = LogFactory.getLog( ExportImageAction.class );
-    
+
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -134,32 +135,49 @@
     {
         this.height = height;
     }
-    
+
+    private String imageFormat;
+
+    public void setImageFormat( String imageFormat )
+    {
+        this.imageFormat = imageFormat;
+    }
+
     @Override
     protected String execute( HttpServletResponse response, OutputStream out )
         throws Exception
     {
-        log.info( "Exporting image, width: " + width + ", height: " + height );
-        
-        SVGUtils.convertToPNG( getSvg(), out, width, height );
-        
+        log.info( "Exporting image, width: " + width + ", height: " + height + ", format: " + this.format );
+
+        if ( imageFormat.equalsIgnoreCase( "image/png" ) )
+        {
+
+            SVGUtils.convertToPNG( getSvg(), out, width, height );
+        }
+        else
+        {
+            SVGUtils.convertToJPEG( getSvg(), out, width, height );
+        }
+
         return SUCCESS;
     }
 
     @Override
     protected String getContentType()
     {
-        return "image/png";
+        return this.imageFormat;
     }
 
     @Override
     protected String getFilename()
     {
-        return "dhis2-gis-image.png";
+        if ( imageFormat.equalsIgnoreCase( "image/png" ) )
+            return "dhis2-gis-image.png";
+        return "dhis2-gis-image.jpg";
     }
 
     private StringBuffer getSvg()
-    {   
+    {
         Period p = periodService.getPeriod( period );
 
         p.setName( format.formatPeriod( p ) );

=== modified file 'dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/export/SVGUtils.java'
--- dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/export/SVGUtils.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/export/SVGUtils.java	2010-04-27 05:35:16 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.awt.Color;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.StringReader;
@@ -34,13 +35,14 @@
 import org.apache.batik.transcoder.TranscoderException;
 import org.apache.batik.transcoder.TranscoderInput;
 import org.apache.batik.transcoder.TranscoderOutput;
+import org.apache.batik.transcoder.image.JPEGTranscoder;
 import org.apache.batik.transcoder.image.PNGTranscoder;
 
 /**
  * @author Tran Thanh Tri
  */
 public class SVGUtils
-{    
+{
     public static void convertToPNG( StringBuffer buffer, OutputStream out, Integer width, Integer height )
         throws TranscoderException, IOException
     {
@@ -48,21 +50,48 @@
         {
             width = 500;
         }
-        
+
         if ( height == null || height < 10 )
         {
             height = 500;
         }
-        
+
         PNGTranscoder t = new PNGTranscoder();
-        
+
         t.addTranscodingHint( PNGTranscoder.KEY_HEIGHT, new Float( height ) );
-        t.addTranscodingHint( PNGTranscoder.KEY_WIDTH, new Float( width ) );
-        
-        TranscoderInput input = new TranscoderInput( new StringReader( buffer.toString() ) );
-        
-        TranscoderOutput output = new TranscoderOutput( out );
-        
+        t.addTranscodingHint( PNGTranscoder.KEY_WIDTH, new Float( width ) );        
+
+        TranscoderInput input = new TranscoderInput( new StringReader( buffer.toString() ) );
+
+        TranscoderOutput output = new TranscoderOutput( out );
+
+        t.transcode( input, output );
+    }
+
+    public static void convertToJPEG( StringBuffer buffer, OutputStream out, Integer width, Integer height )
+        throws TranscoderException, IOException
+    {
+        if ( width == null || width < 10 )
+        {
+            width = 500;
+        }
+
+        if ( height == null || height < 10 )
+        {
+            height = 500;
+        }
+
+        JPEGTranscoder t = new JPEGTranscoder();
+
+        t.addTranscodingHint( JPEGTranscoder.KEY_HEIGHT, new Float( height ) );
+        t.addTranscodingHint( JPEGTranscoder.KEY_WIDTH, new Float( width ) );
+        t.addTranscodingHint( JPEGTranscoder.KEY_BACKGROUND_COLOR, Color.WHITE );
+        t.addTranscodingHint( JPEGTranscoder.KEY_QUALITY, new Float( .8 ) );
+
+        TranscoderInput input = new TranscoderInput( new StringReader( buffer.toString() ) );
+
+        TranscoderOutput output = new TranscoderOutput( out );
+
         t.transcode( input, output );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/index.html'
--- dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/index.html	2010-04-12 17:07:09 +0000
+++ dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/index.html	2010-04-27 05:35:16 +0000
@@ -81,6 +81,7 @@
         <input type="hidden" id="indicatorField" name="indicator"/>
         <input type="hidden" id="legendsField" name="legends"/>
         <input type="hidden" id="dataValuesField" name="datavalues"/>
+		<input type="hidden" id="imageFormat" name="imageFormat"/>
     </form>
     
 </body>

=== modified file 'dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/script/index.js'
--- dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/script/index.js	2010-04-22 17:27:49 +0000
+++ dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/mapping/script/index.js	2010-04-27 05:35:16 +0000
@@ -475,6 +475,25 @@
 			},
 			{
 				xtype: 'combo',
+				id: 'exportimageformat_cb',
+				fieldLabel: 'Image format',
+				labelSeparator: labelseparator,
+				editable: false,
+				valueField: 'id',
+				displayField: 'text',
+				isFormField: true,
+				width: combo_width_fieldset,
+				minListWidth: combo_list_width_fieldset,
+				mode: 'local',
+				triggerAction: 'all',
+				value: 'image/jpeg',
+				store: new Ext.data.SimpleStore({
+					fields: ['id', 'text'],
+					data: [['image/png', 'PNG'], ['image/jpeg', 'JPEG']]
+				})					
+			},
+			{
+				xtype: 'combo',
 				id: 'exportimagequality_cb',
 				fieldLabel: 'Image quality',
 				labelSeparator: labelseparator,
@@ -524,6 +543,7 @@
 						var includeLegend = Ext.getCmp('exportimageincludelegend_chb').getValue();
 						var period = Ext.getCmp('period_cb').getValue();
 						var indicator = Ext.getCmp('indicator_cb').getValue();
+						var imageFormat = Ext.getCmp('exportimageformat_cb').getValue();
 						
 						Ext.getCmp('exportimagetitle_tf').reset();
                         
@@ -540,6 +560,7 @@
                         document.getElementById('periodField').value = period;  
                         document.getElementById('indicatorField').value = indicator;   
                         document.getElementById('legendsField').value = getLegendsJSON();
+						document.getElementById('imageFormat').value = imageFormat;
 
                         exportForm.submit();
 					}
@@ -640,7 +661,7 @@
         closeAction: 'hide',
 		defaults: {layout: 'fit', bodyStyle: 'padding:8px; border:0px'},
 		width: 250,
-		height: 157,
+		height: 190,
         items: [
 		   {
                 xtype: 'panel',
@@ -657,7 +678,7 @@
 		layout: 'fit',
         closeAction: 'hide',
 		defaults: {layout: 'fit', bodyStyle: 'padding:8px; border:0px'},
-		width: 252,
+		width: 260,
 		height: 157,
         items: [
 		   {
@@ -3349,7 +3370,7 @@
 	
 	var exportImageButton = new Ext.Button({
 		iconCls: 'icon-image',
-		tooltip: 'Export map as image (PNG)',
+		tooltip: 'Export map as image',
 		handler: function() {
 			var x = Ext.getCmp('center').x + 15;
 			var y = Ext.getCmp('center').y + 41;