1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.image;
35
36
37 import java.awt.Color;
38 import java.awt.Graphics2D;
39 import java.awt.geom.AffineTransform;
40 import java.awt.image.BufferedImage;
41 import java.io.ByteArrayInputStream;
42 import java.io.ByteArrayOutputStream;
43 import java.io.IOException;
44 import java.math.BigDecimal;
45
46 import javax.imageio.IIOImage;
47 import javax.imageio.ImageIO;
48 import javax.imageio.ImageWriteParam;
49 import javax.imageio.ImageWriter;
50 import javax.imageio.stream.MemoryCacheImageOutputStream;
51
52 import org.apache.log4j.Logger;
53
54
55
56
57 public class ImageUtil
58 {
59
60
61 private static final String PARAMETER_JPG = "jpg";
62
63 private static final Logger _logger = Logger.getLogger( ImageUtil.class );
64
65
66
67
68
69
70
71
72
73 public static byte[] resizeImage( Object blobImage, String strWidth, String strHeight, float fQuality )
74 {
75
76 byte[] newBlobImage = ( byte[] ) blobImage;
77 if ( blobImage != null )
78 {
79 ByteArrayInputStream bais = new ByteArrayInputStream( newBlobImage );
80 BufferedImage image = null;
81 try
82 {
83 image = ImageIO.read( bais );
84
85
86 double nParamWidth = Double.valueOf( strWidth );
87 double nParamHeight = Double.valueOf( strHeight );
88 double nParamRatio = BigDecimal.valueOf( nParamWidth ).divide( BigDecimal.valueOf( nParamHeight ), 6 ).doubleValue();
89
90
91 double nImageWidth = Double.valueOf( image.getWidth() );
92 double nImageHeight = Double.valueOf( image.getHeight() );
93 double nImageRatio = BigDecimal.valueOf( nImageWidth ).divide( BigDecimal.valueOf( nImageHeight ), 6 ).doubleValue();
94
95 if ( nImageWidth > nParamWidth || nImageHeight > nParamHeight )
96 {
97 double nTargetWidth = 0;
98 double nTargetHeight = 0;
99
100 if ( nParamRatio > nImageRatio )
101 {
102
103 nTargetHeight = ( nImageHeight > nParamHeight ) ? nParamHeight : nImageHeight;
104 nTargetWidth = BigDecimal.valueOf( nTargetHeight ).multiply( BigDecimal.valueOf( nImageRatio ) ).doubleValue();
105 }
106
107 else
108 {
109
110 nTargetWidth = ( nImageWidth > nParamWidth ) ? nParamWidth : nImageWidth;
111 nTargetHeight = BigDecimal.valueOf( nTargetWidth ).divide( BigDecimal.valueOf( nImageRatio ), 6 ).doubleValue();
112 }
113 BufferedImage bdest = new BufferedImage( ( ( Double ) nTargetWidth ).intValue(), ( ( Double ) nTargetHeight ).intValue(), BufferedImage.TYPE_INT_RGB );
114 Graphics2D g = bdest.createGraphics();
115 AffineTransform at = AffineTransform.getScaleInstance( Double.valueOf( nTargetWidth ) / image.getWidth(), Double.valueOf( nTargetHeight ) / image.getHeight() );
116 g.drawRenderedImage( image, at );
117
118 return getJPEGImageAsByteTab( bdest , fQuality ).toByteArray();
119 }
120 }
121 catch ( IOException e )
122 {
123 _logger.error( "Error ImageUtil : " + e.getMessage(), e );
124 }
125 }
126 return newBlobImage;
127 }
128
129
130
131
132
133
134
135 private static ByteArrayOutputStream getJPEGImageAsByteTab( BufferedImage image, float fQuality )
136 {
137 ByteArrayOutputStream outBuffered = null;
138 try
139 {
140 outBuffered = new ByteArrayOutputStream();
141
142
143 ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName( PARAMETER_JPG ).next( );
144 ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam( );
145 jpgWriteParam.setCompressionMode( ImageWriteParam.MODE_EXPLICIT );
146 jpgWriteParam.setCompressionQuality( fQuality );
147 jpgWriter.setOutput( new MemoryCacheImageOutputStream( outBuffered ) );
148 IIOImage outputImage = new IIOImage( image, null, null );
149 jpgWriter.write( null, outputImage, jpgWriteParam );
150 jpgWriter.dispose( );
151 outBuffered.flush( );
152 }
153 catch ( IOException e2 )
154 {
155 return null;
156 }
157 return outBuffered;
158 }
159 }