1 package ch.ige.edossier.util;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.util.Map;
6 import java.util.Properties;
7 import ch.ige.edossier.web.vo.ImageVO;
8 import com.sun.media.jai.codec.ImageCodec;
9 import com.sun.media.jai.codec.MemoryCacheSeekableStream;
10
11
21 public class ImageProcessor
22 {
23
26 public ImageProcessor()
27 {}
28
29
34 public static ImageProcessor getInstance()
35 {
36 return new ImageProcessor();
37 }
38
39
46 public Map writeImage( String outputPath, byte[] image ) throws Exception
47 {
48 MemoryCacheSeekableStream memorySeekableStream = new MemoryCacheSeekableStream( new ByteArrayInputStream( image ) );
49 String typ = ImageCodec.getDecoderNames( memorySeekableStream )[ 0 ];
50 if( typ.equals( "jpeg" ) )
51 {
52 typ = "jpg";
53 }
54 return writeImage( outputPath, image, typ );
55 }
56
57
65 public Map writeImage( String outputPath, byte[] image, String typ ) throws Exception
66 {
67 checkDirectory();
68
69 ImageWriter imageWriter;
70
71 if( typ.equalsIgnoreCase( "jpg" ) )
72 {
73 imageWriter = new JPEGImageWriter();
74 }
75 else if( typ.equalsIgnoreCase( "tiff" ) )
76 {
77 imageWriter = new TIFFImageWriter();
78 }
79 else {
81 imageWriter = new TIFFImageWriter();
82 }
83 return imageWriter.processImage( outputPath, image );
85 }
86
87
94 public ImageVO defineDimension( ImageVO imageVO, Map map ) throws NumberFormatException
95 {
96 String width = ( String )map.get( "width" );
97 String height = ( String )map.get( "height" );
98
99 if( ( Integer.parseInt( width ) > 200 ) || ( Integer.parseInt( height ) > 200 ) )
100 {
101 if( Integer.parseInt( width ) > Integer.parseInt( height ) )
102 {
103 float scalFactor = Float.parseFloat( width ) / 200;
104 imageVO.setThumbsWidth( "200" );
105 imageVO.setThumbsHeight( String.valueOf( ( int )Math.round( Float.parseFloat( height ) / scalFactor ) ) );
106 }
107 else
108 {
109 float scalFactor = Float.parseFloat( height ) / 200;
110 imageVO.setThumbsWidth( String.valueOf( ( int )Math.round( Float.parseFloat( width ) / scalFactor ) ) );
111 imageVO.setThumbsHeight( "200" );
112 }
113 imageVO.setIsThumbs( true );
114 }
115 else
116 {
117 imageVO.setThumbsWidth( width );
118 imageVO.setThumbsHeight( height );
119 }
120 imageVO.setImageWidth( width );
121 imageVO.setImageHeight( height );
122
123 return imageVO;
124 }
125
126
130 private void checkDirectory() throws Exception
131 {
132 Properties properties = ServerProperties.loadServerProperties();
133 File statFile = new File( properties.getProperty( "pathImages" ) );
134 if( !statFile.isDirectory() )
135 {
136 statFile.mkdirs();
137 }
138 }
139 }
140