KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > components > image > 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.axis.components.image;
18
19 import org.apache.axis.utils.Messages;
20 import org.apache.axis.utils.IOUtils;
21 import sun.awt.image.codec.JPEGImageEncoderImpl;
22
23 import java.awt.Component JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.MediaTracker JavaDoc;
27 import java.awt.Toolkit JavaDoc;
28 import java.awt.image.BufferedImage JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32
33 /**
34  * JDK1.3 based Image I/O
35  *
36  * @author <a HREF="mailto:butek@us.ibm.com">Russell Butek</a>
37  */

38 public class JDK13IO extends Component JavaDoc implements ImageIO {
39     /**
40      * Save an image.
41      * @param mimeType the mime-type of the format to save the image
42      * @param image the image to save
43      * @param os the stream to write to
44      * @exception Exception if an error prevents image encoding
45      */

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

73     /**
74      * Load an Image.
75      * @param in the stream to load the image
76      * @return the Image
77      */

78     public Image JavaDoc loadImage(InputStream JavaDoc in) throws Exception JavaDoc {
79         if (in.available() <= 0) {
80             return null;
81         }
82         else {
83             byte[] bytes = new byte[in.available()];
84             IOUtils.readFully(in,bytes);
85             return Toolkit.getDefaultToolkit().createImage(bytes);
86         }
87     } // loadImage
88
}
89
90
Popular Tags