|
1 /* |
|
2 * |
|
3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * - Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * - Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * - Neither the name of Oracle nor the names of its |
|
17 * contributors may be used to endorse or promote products derived |
|
18 * from this software without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package java2d.demos.Images; |
|
33 |
|
34 |
|
35 import static java.awt.Color.BLACK; |
|
36 import static java.awt.Color.GREEN; |
|
37 import static java.awt.Color.RED; |
|
38 import static java.awt.Color.WHITE; |
|
39 import java.awt.Font; |
|
40 import java.awt.Graphics2D; |
|
41 import java.awt.Image; |
|
42 import java.awt.geom.GeneralPath; |
|
43 import java.awt.geom.Path2D; |
|
44 import java.awt.image.BufferedImage; |
|
45 import java.io.ByteArrayInputStream; |
|
46 import java.io.ByteArrayOutputStream; |
|
47 import java.io.IOException; |
|
48 import java.util.logging.Level; |
|
49 import java.util.logging.Logger; |
|
50 import java2d.Surface; |
|
51 import javax.imageio.IIOImage; |
|
52 import javax.imageio.ImageIO; |
|
53 import javax.imageio.ImageWriteParam; |
|
54 import javax.imageio.ImageWriter; |
|
55 import javax.imageio.plugins.jpeg.JPEGImageWriteParam; |
|
56 import javax.imageio.stream.ImageOutputStream; |
|
57 |
|
58 |
|
59 /** |
|
60 * Render a filled star & duke into a BufferedImage, save the BufferedImage |
|
61 * as a JPEG, display the BufferedImage, using the decoded JPEG BufferedImage |
|
62 * display the JPEG flipped BufferedImage. |
|
63 */ |
|
64 @SuppressWarnings("serial") |
|
65 public class JPEGFlip extends Surface { |
|
66 |
|
67 private static Image img; |
|
68 |
|
69 public JPEGFlip() { |
|
70 setBackground(WHITE); |
|
71 img = getImage("duke.png"); |
|
72 } |
|
73 |
|
74 @Override |
|
75 public void render(int w, int h, Graphics2D g2) { |
|
76 |
|
77 int hh = h / 2; |
|
78 |
|
79 BufferedImage bi = new BufferedImage(w, hh, BufferedImage.TYPE_INT_RGB); |
|
80 Graphics2D big = bi.createGraphics(); |
|
81 |
|
82 // .. use rendering hints from J2DCanvas .. |
|
83 big.setRenderingHints(g2.getRenderingHints()); |
|
84 |
|
85 big.setBackground(getBackground()); |
|
86 big.clearRect(0, 0, w, hh); |
|
87 |
|
88 big.setColor(GREEN.darker()); |
|
89 GeneralPath p = new GeneralPath(Path2D.WIND_NON_ZERO); |
|
90 p.moveTo(-w / 2.0f, -hh / 8.0f); |
|
91 p.lineTo(+w / 2.0f, -hh / 8.0f); |
|
92 p.lineTo(-w / 4.0f, +hh / 2.0f); |
|
93 p.lineTo(+0.0f, -hh / 2.0f); |
|
94 p.lineTo(+w / 4.0f, +hh / 2.0f); |
|
95 p.closePath(); |
|
96 big.translate(w / 2, hh / 2); |
|
97 big.fill(p); |
|
98 |
|
99 float scale = 0.09f; |
|
100 int iw = (int) (scale * w); |
|
101 int ih = (int) (img.getHeight(null) * iw / img.getWidth(null)); |
|
102 big.drawImage(img, -iw / 2, -ih / 2, iw, ih, this); |
|
103 |
|
104 g2.drawImage(bi, 0, 0, this); |
|
105 g2.setFont(new Font("Dialog", Font.PLAIN, 10)); |
|
106 g2.setColor(BLACK); |
|
107 g2.drawString("BufferedImage", 4, 12); |
|
108 |
|
109 |
|
110 BufferedImage bi1 = null; |
|
111 ImageOutputStream ios = null; |
|
112 // To write the jpeg to a file uncomment the File* lines and |
|
113 // comment out the ByteArray*Stream lines. |
|
114 //FileOutputStream out = null; |
|
115 ByteArrayOutputStream out = null; |
|
116 //FileInputStream in = null; |
|
117 ByteArrayInputStream in = null; |
|
118 try { |
|
119 //File file = new File("images", "test.jpg"); |
|
120 //out = new FileOutputStream(file); |
|
121 out = new ByteArrayOutputStream(); |
|
122 ios = ImageIO.createImageOutputStream(out); |
|
123 ImageWriter encoder = |
|
124 ImageIO.getImageWritersByFormatName("JPEG").next(); |
|
125 JPEGImageWriteParam param = new JPEGImageWriteParam(null); |
|
126 |
|
127 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
|
128 param.setCompressionQuality(1.0f); |
|
129 |
|
130 encoder.setOutput(ios); |
|
131 encoder.write(null, new IIOImage(bi, null, null), param); |
|
132 |
|
133 //in = new FileInputStream(file); |
|
134 in = new ByteArrayInputStream(out.toByteArray()); |
|
135 bi1 = ImageIO.read(in); |
|
136 } catch (Exception ex) { |
|
137 g2.setColor(RED); |
|
138 g2.drawString("Error encoding or decoding the image", 5, hh * 2 - 5); |
|
139 return; |
|
140 } finally { |
|
141 if (ios != null) { |
|
142 try { |
|
143 ios.close(); |
|
144 } catch (IOException ex) { |
|
145 Logger.getLogger(JPEGFlip.class.getName()).log(Level.SEVERE, |
|
146 null, ex); |
|
147 } |
|
148 } |
|
149 if (out != null) { |
|
150 try { |
|
151 out.close(); |
|
152 } catch (IOException ex) { |
|
153 Logger.getLogger(JPEGFlip.class.getName()).log(Level.SEVERE, |
|
154 null, ex); |
|
155 } |
|
156 } |
|
157 if (in != null) { |
|
158 try { |
|
159 in.close(); |
|
160 } catch (IOException ex) { |
|
161 Logger.getLogger(JPEGFlip.class.getName()).log(Level.SEVERE, |
|
162 null, ex); |
|
163 } |
|
164 } |
|
165 } |
|
166 |
|
167 if (bi1 == null) { |
|
168 g2.setColor(RED); |
|
169 g2.drawString("Error reading the image", 5, hh * 2 - 5); |
|
170 return; |
|
171 } |
|
172 |
|
173 g2.drawImage(bi1, w, hh * 2, -w, -hh, null); |
|
174 |
|
175 g2.drawString("JPEGImage Flipped", 4, hh * 2 - 4); |
|
176 g2.drawLine(0, hh, w, hh); |
|
177 } |
|
178 |
|
179 public static void main(String s[]) { |
|
180 createDemoFrame(new JPEGFlip()); |
|
181 } |
|
182 } |