KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > attachments > JDK13IO


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.axis2.attachments;
18
19
20 import sun.awt.image.codec.JPEGImageEncoderImpl;
21
22 import java.awt.*;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27
28 /**
29  * JDK1.3 based Image I/O
30  *
31  * @author <a HREF="mailto:butek@us.ibm.com">Russell Butek</a>
32  */

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

41     public void saveImage(String JavaDoc mimeType, Image JavaDoc image, OutputStream JavaDoc os)
42             throws Exception JavaDoc {
43
44         BufferedImage JavaDoc rendImage = null;
45
46         // Create a BufferedImage
47
if (image instanceof BufferedImage JavaDoc) {
48             rendImage = (BufferedImage JavaDoc) image;
49         } else {
50             MediaTracker tracker = new MediaTracker(this);
51             tracker.addImage(image, 0);
52             tracker.waitForAll();
53             rendImage = new BufferedImage JavaDoc(image.getWidth(null), image.getHeight(null), 1);
54             Graphics g = rendImage.createGraphics();
55             g.drawImage(image, 0, 0, null);
56         }
57
58         // Write the image to the output stream
59
if ("image/jpeg".equals(mimeType)) {
60             JPEGImageEncoderImpl j = new JPEGImageEncoderImpl(os);
61             j.encode(rendImage);
62         }
63         else {
64             throw new IOException JavaDoc("Supports Jpeg Only");
65         }
66     } // saveImage
67

68     /**
69      * Load an Image.
70      * @param in the stream to load the image
71      * @return the Image
72      */

73     public Image JavaDoc loadImage(InputStream JavaDoc in) throws Exception JavaDoc {
74         if (in.available() <= 0) {
75             return null;
76         }
77         else {
78             byte[] bytes = new byte[in.available()];
79             org.apache.axis2.attachments.IOUtils.readFully(in,bytes);
80             return Toolkit.getDefaultToolkit().createImage(bytes);
81         }
82     } // loadImage
83
}
84
85
Popular Tags