PageRenderTime 27ms CodeModel.GetById 22ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/alaspatial/src/main/java/org/ala/spatial/util/WrapBufferedImage.java

http://alageospatialportal.googlecode.com/
Java | 74 lines | 58 code | 12 blank | 4 comment | 5 complexity | 8e00cf320b3d1a474092a3e3022321d7 MD5 | raw file
 1package org.ala.spatial.util;
 2
 3import java.awt.image.BufferedImage;
 4import java.io.ByteArrayInputStream;
 5import java.io.ByteArrayOutputStream;
 6import java.io.IOException;
 7import java.io.Serializable;
 8import javax.imageio.ImageIO;
 9
10/**
11 *
12 * @author ajay
13 */
14public class WrapBufferedImage implements Serializable {
15
16	static final long serialVersionUID = 6583156430853699407L;
17
18	
19    private BufferedImage im = null;
20
21    public WrapBufferedImage() {
22        super();
23    }
24
25    public BufferedImage getIm() {
26        return im;
27    }
28
29    public void setIm(BufferedImage im) {
30        this.im = im;
31    }
32
33    private BufferedImage fromByteArray(byte[] imagebytes) {
34        try {
35            if (imagebytes != null && (imagebytes.length > 0)) {
36                BufferedImage im = ImageIO.read(new ByteArrayInputStream(imagebytes));
37                return im;
38            }
39            return null;
40        } catch (IOException e) {
41            throw new IllegalArgumentException(e.toString());
42        }
43    }
44
45    private byte[] toByteArray(BufferedImage o) {
46        if (o != null) {
47            BufferedImage image = (BufferedImage) o;
48            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
49            try {
50                ImageIO.write(image, "png", baos);
51            } catch (IOException e) {
52                throw new IllegalStateException(e.toString());
53            }
54            byte[] b = baos.toByteArray();
55            return b;
56        }
57        return new byte[0];
58    }
59
60    private void writeObject(java.io.ObjectOutputStream out)
61            throws IOException {
62        byte[] b = toByteArray(im);
63        out.writeInt(b.length);
64        out.write(b);
65    }
66
67    private void readObject(java.io.ObjectInputStream in)
68            throws IOException, ClassNotFoundException {
69        int length = in.readInt();
70        byte[] b = new byte[length];
71        in.read(b);
72        im = fromByteArray(b);
73    }
74}