View Javadoc

1   package org.jmage.encoder;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.log4j.Priority;
5   import org.jmage.ApplicationContext;
6   import org.jmage.ImageRequest;
7   import ranab.img.gif.GifImage;
8   
9   import java.awt.*;
10  import java.awt.image.BufferedImage;
11  import java.io.ByteArrayOutputStream;
12  import java.io.IOException;
13  
14  /***
15   * Encodes Images in GIF format
16   */
17  public class GifEncoder implements ImageEncoder {
18      public static Logger log = Logger.getLogger(GifEncoder.class.getName());
19      private static final String GIF_SUCCESS = " encoded image in format: gif";
20      private static final String GIF_ERROR = "unable to encode gif image, cause: ";
21      private static final String GIF_INIT = "initialized encoder for: gif";
22      private static final String GIF = "gif";
23  
24      /***
25       * Default Constructor
26       */
27      public GifEncoder() {
28          //do nothing
29      }
30  
31      /***
32       * Test whether the ImageEncoder can handle the format
33       *
34       * @param request the ImageRequest
35       * @return [true | false]
36       */
37      public boolean canHandle(ImageRequest request) {
38          return GIF.equals(request.getEncodingFormat().toLowerCase());
39      }
40  
41      /***
42       * Configure encoder properties
43       *
44       * @param context
45       */
46      public void configure(ApplicationContext context) {
47          //TODO: configure encoderproperties here
48      }
49  
50      /***
51       * Initialize the ImageEncoder for a particular encodingformat in the ImageRequest
52       *
53       * @param request
54       */
55      public void initialize(ImageRequest request) throws CodecException {
56          if (log.isDebugEnabled()) log.debug(GIF_INIT);
57      }
58  
59  
60      /***
61       * Encode the image in the specified format
62       *
63       * @param request the imageRequest
64       * @return the encoded image as a byte array.
65       */
66      public byte[] createFrom(ImageRequest request) throws CodecException {
67          try {
68  
69              BufferedImage buffered = request.getImage().getAsBufferedImage();
70              int w = buffered.getWidth();
71              int h = buffered.getHeight();
72  
73              GifImage gif = new GifImage(w, h);
74              Graphics gifGraphics = gif.getGraphics();
75              gifGraphics.drawImage(buffered, 0, 0, w, h, null);
76  
77              ByteArrayOutputStream bos = new ByteArrayOutputStream();
78              gif.encode(bos);
79              if (log.isInfoEnabled()) log.info(GIF_SUCCESS);
80              return bos.toByteArray();
81          } catch (IOException e) {
82              String message = GIF_ERROR + e.getMessage();
83              if (log.isEnabledFor(Priority.ERROR)) log.error(message);
84              throw new CodecException(message);
85          }
86      }
87  
88      public String toString() {
89          return "[" + this.getClass().getName() + "#" + this.hashCode() + "]";
90      }
91  }