KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > components > image > MerlinIO


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.components.image;
18
19 import javax.imageio.IIOImage JavaDoc;
20 import javax.imageio.ImageWriter JavaDoc;
21 import java.awt.*;
22 import java.awt.image.BufferedImage JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * JDK1.4 based Image I/O
29  *
30  * NOTE: NEEDS WORK!!!! NEEDS TO BE TESTED.
31  *
32  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
33  * @since 2.0
34  */

35 public class MerlinIO extends Component implements ImageIO {
36     /**
37      * Save an image.
38      * @param mimeType the mime-type of the format to save the image
39      * @param image the image to save
40      * @param os the stream to write to
41      * @exception JimiException if an error prevents image encoding
42      */

43     public void saveImage(String JavaDoc mimeType, Image JavaDoc image, OutputStream JavaDoc os)
44             throws Exception JavaDoc {
45         ImageWriter JavaDoc writer = null;
46         Iterator JavaDoc iter = javax.imageio.ImageIO.getImageWritersByMIMEType(mimeType);
47         if (iter.hasNext()) {
48             writer = (ImageWriter JavaDoc) iter.next();
49         }
50         writer.setOutput(javax.imageio.ImageIO.createImageOutputStream(os));
51         BufferedImage JavaDoc rendImage = null;
52         if (image instanceof BufferedImage JavaDoc) {
53             rendImage = (BufferedImage JavaDoc) image;
54         } else {
55             MediaTracker tracker = new MediaTracker(this);
56             tracker.addImage(image, 0);
57             tracker.waitForAll();
58             rendImage = new BufferedImage JavaDoc(image.getWidth(null), image.getHeight(null), 1);
59             Graphics g = rendImage.createGraphics();
60             g.drawImage(image, 0, 0, null);
61         }
62         writer.write(new IIOImage JavaDoc(rendImage, null, null));
63         writer.dispose();
64     }
65
66     /**
67      * Load an Image.
68      * @param in the stream to load the image
69      * @return the Image
70      */

71     public Image JavaDoc loadImage(InputStream JavaDoc in) throws Exception JavaDoc {
72         return javax.imageio.ImageIO.read(in);
73     }
74 }
75
76
Popular Tags