1 36 37 40 41 package java2d.demos.Images; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import com.sun.image.codec.jpeg.*; 46 import java.awt.image.BufferedImage ; 47 import java.awt.image.DataBuffer ; 48 import java.awt.geom.GeneralPath ; 49 import java.io.*; 50 import java2d.Surface; 51 52 53 58 public class JPEGFlip extends Surface { 59 60 private static Image img; 61 62 public JPEGFlip() { 63 setBackground(WHITE); 64 img = getImage("duke.gif"); 65 } 66 67 68 public void render(int w, int h, Graphics2D g2) { 69 70 int hh = h/2; 71 72 BufferedImage bi = new BufferedImage (w, hh, BufferedImage.TYPE_INT_RGB); 73 Graphics2D big = bi.createGraphics(); 74 75 big.setRenderingHints(g2.getRenderingHints()); 77 78 big.setBackground(getBackground()); 79 big.clearRect(0, 0, w, hh); 80 81 big.setColor(GREEN.darker()); 82 GeneralPath p = new GeneralPath (GeneralPath.WIND_NON_ZERO); 83 p.moveTo(- w / 2.0f, - hh / 8.0f); 84 p.lineTo(+ w / 2.0f, - hh / 8.0f); 85 p.lineTo(- w / 4.0f, + hh / 2.0f); 86 p.lineTo(+ 0.0f, - hh / 2.0f); 87 p.lineTo(+ w / 4.0f, + hh / 2.0f); 88 p.closePath(); 89 big.translate(w/2, hh/2); 90 big.fill(p); 91 92 int iw = img.getWidth(this); 93 int ih = img.getHeight(this); 94 if (hh < ih * 1.5) 95 ih = (int) (ih * ((hh / (ih*1.5)))); 96 big.drawImage(img, -img.getWidth(this)/2, -ih/2, iw, ih, this); 97 98 g2.drawImage(bi, 0, 0, this); 99 g2.setFont(new Font("Dialog", Font.PLAIN, 10)); 100 g2.setColor(BLACK); 101 g2.drawString("BufferedImage", 4, 12); 102 103 104 BufferedImage bi1 = null; 105 106 try { 107 ByteArrayOutputStream out = new ByteArrayOutputStream(); 112 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 113 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); 114 param.setQuality(1.0f, false); 115 encoder.setJPEGEncodeParam(param); 116 encoder.encode(bi); 117 118 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 120 JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); 121 bi1 = decoder.decodeAsBufferedImage(); 122 } catch (Exception ex) { 123 g2.setColor(RED); 124 g2.drawString("write permissions on images/test.jpg?", 5, hh*2-5); 125 return; 126 } 127 128 if (bi1 == null) { 129 g2.setColor(RED); 130 g2.drawString("decodeAsBufferedImage=null", 5, hh*2-5); 131 return; 132 } 133 134 BufferedImage bi2 = new BufferedImage (bi1.getWidth(),bi1.getHeight(),bi1.getType()); 135 DataBuffer db1 = bi1.getRaster().getDataBuffer(); 136 DataBuffer db2 = bi2.getRaster().getDataBuffer(); 137 138 for (int i = db1.getSize()-1, j = 0; i >= 0; --i, j++) { 139 db2.setElem(j, db1.getElem(i)); 140 } 141 142 g2.drawImage(bi2, 0, hh, this); 143 144 g2.drawString("JPEGImage Flipped", 4, hh*2-4); 145 g2.drawLine(0, hh, w, hh); 146 } 147 148 149 public static void main(String s[]) { 150 createDemoFrame(new JPEGFlip()); 151 } 152 } 153 | Popular Tags |