View Javadoc

1   package org.jmage.encoder;
2   
3   import com.sun.media.jai.codec.ImageEncodeParam;
4   import org.apache.log4j.Logger;
5   import org.apache.log4j.Priority;
6   import org.jmage.ApplicationContext;
7   import org.jmage.ImageRequest;
8   
9   import javax.media.jai.JAI;
10  import java.io.ByteArrayOutputStream;
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  /***
15   * Encodes the most popular images types "bmp", "png", "jpg", and "tif"
16   */
17  public class DefaultImageEncoder implements ImageEncoder {
18      public static final String BMP = "bmp";
19      public static final String TIF = "tif";
20      public static final String TIFF = "tiff";
21  
22      public static Map encodingFormats;
23  
24      public static Logger log = Logger.getLogger(DefaultImageEncoder.class.getName());
25      protected String encodingFormat;
26      protected ImageEncodeParam encodeParam;
27  
28      private static final String ENCODE = "encode";
29      private static final String INIT_ERROR = "unable to initialize encoder, unsupported encodingFormat: ";
30      private static final String INIT_SUCCESS = "initialized encoder for: ";
31      private static final String ENCODE_SUCCESS = "encoded image in format: ";
32  
33      static {
34          encodingFormats = new HashMap();
35          encodingFormats.put(BMP, BMP);
36  //        encodingFormats.put(PNG, PNG);
37  //        encodingFormats.put(JPG, JPEG);
38  //        encodingFormats.put(JPEG, JPEG);
39          encodingFormats.put(TIF, TIFF);
40          encodingFormats.put(TIFF, TIFF);
41      }
42  
43      /***
44       * Default Constructor
45       */
46      public DefaultImageEncoder() {
47          //do nothing
48      }
49  
50      /***
51       * Configure
52       *
53       * @param context
54       */
55      public void configure(ApplicationContext context) {
56          //todo: configure encoder properties here.
57      }
58  
59      /***
60       * Test whether the ImageEncoder can handle the format
61       *
62       * @param request the imageRequest.
63       * @return [true | false]
64       */
65      public boolean canHandle(ImageRequest request) {
66          encodingFormat = request.getEncodingFormat().toLowerCase();
67          return encodingFormats.containsKey(encodingFormat);
68      }
69  
70      /***
71       * Initialize the ImageEncoder for a particular encodingformat using Properties
72       *
73       * @param request
74       */
75      public void initialize(ImageRequest request) throws CodecException {
76          this.encodingFormat = (String) encodingFormats.get(request.getEncodingFormat().toLowerCase());
77  
78          if (this.encodingFormat == null) {
79              String message = INIT_ERROR + encodingFormat;
80              if (log.isEnabledFor(Priority.ERROR)) log.error(message);
81              throw new CodecException(message);
82          } else {
83              if (BMP.equals(encodingFormat)) {
84  //                this.encodeParam = new BMPEncodeParam();
85  //                ((BMPEncodeParam) encodeParam).setVersion(4);
86  //                ((BMPEncodeParam) encodeParam).setTopDown(false);
87              }
88          }
89          if (log.isDebugEnabled()) log.debug(INIT_SUCCESS + encodingFormat);
90      }
91  
92  
93      /***
94       * Encode the image in the specified format
95       *
96       * @param request the ImageRequest
97       * @return the encoded image as a byte array.
98       */
99      public byte[] createFrom(ImageRequest request) throws CodecException {
100         ByteArrayOutputStream dest = new ByteArrayOutputStream();
101         encodingFormat = encodingFormat.toLowerCase();
102         JAI.create(ENCODE, request.getImage(), dest, encodingFormat, encodeParam);
103         if (log.isInfoEnabled()) log.info(ENCODE_SUCCESS + encodingFormat);
104         return dest.toByteArray();
105     }
106 
107     public String toString() {
108         return "[" + this.getClass().getName() + "#" + this.hashCode() + "]";
109     }
110 }