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.imageio.ImageIO;
10  import java.awt.image.BufferedImage;
11  import java.io.ByteArrayOutputStream;
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  /***
16   * Use JDK native methods to encode images
17   */
18  public class JDKImageEncoder implements ImageEncoder {
19      public static final String PNG = "png";
20      public static final String JPG = "jpg";
21      public static final String JPEG = "jpeg";
22  
23      public static Map encodingFormats;
24  
25      public static Logger log = Logger.getLogger(JDKImageEncoder.class.getName());
26      protected String encodingFormat;
27      protected ImageEncodeParam encodeParam;
28  
29      private static final String ENCODE = "encode";
30      private static final String INIT_ERROR = "unable to initialize encoder, unsupported encodingFormat: ";
31      private static final String INIT_SUCCESS = "initialized encoder for: ";
32      private static final String ENCODE_SUCCESS = "encoded image in format: ";
33      private static final String ENCODE_ERROR = "error encoding image in format, cause: ";
34  
35      static {
36          encodingFormats = new HashMap();
37          encodingFormats.put(PNG, PNG);
38          encodingFormats.put(JPG, JPG);
39          encodingFormats.put(JPEG, JPG);
40      }
41  
42      /***
43       * Default Constructor
44       */
45      public JDKImageEncoder() {
46          //do nothing
47      }
48  
49      /***
50       * Configure
51       *
52       * @param context
53       */
54      public void configure(ApplicationContext context) {
55          //todo: configure encoder properties here.
56      }
57  
58      public boolean canHandle(ImageRequest request) {
59          encodingFormat = request.getEncodingFormat().toLowerCase();
60          return encodingFormats.containsKey(encodingFormat);
61      }
62  
63      public void initialize(ImageRequest request) throws CodecException {
64          this.encodingFormat = (String) encodingFormats.get(request.getEncodingFormat().toLowerCase());
65  
66          if (this.encodingFormat == null) {
67              String message = INIT_ERROR + encodingFormat;
68              if (log.isEnabledFor(Priority.ERROR)) log.error(message);
69              throw new CodecException(message);
70          }
71          if (log.isDebugEnabled()) log.debug(INIT_SUCCESS + encodingFormat);
72      }
73  
74      public byte[] createFrom(ImageRequest request) throws CodecException {
75          try {
76              BufferedImage buffered = request.getImage().getAsBufferedImage();
77              ByteArrayOutputStream bos = new ByteArrayOutputStream();
78              ImageIO.write(buffered, encodingFormat, bos);
79              if (log.isDebugEnabled()) log.debug(ENCODE_SUCCESS + encodingFormat);
80              return bos.toByteArray();
81          } catch (Exception e) {
82              if (log.isEnabledFor(Priority.ERROR)) log.error(ENCODE_ERROR + e.getMessage());
83              throw new CodecException(e.getMessage());
84          }
85      }
86  }