1 18 package org.apache.batik.transcoder.image; 19 20 import java.awt.Color ; 21 import java.awt.image.BufferedImage ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 import org.apache.batik.transcoder.TranscoderException; 26 import org.apache.batik.transcoder.TranscoderOutput; 27 import org.apache.batik.transcoder.TranscodingHints; 28 import org.apache.batik.transcoder.image.resources.Messages; 29 30 import com.sun.image.codec.jpeg.JPEGCodec; 31 import com.sun.image.codec.jpeg.JPEGEncodeParam; 32 import com.sun.image.codec.jpeg.JPEGImageEncoder; 33 34 40 public class JPEGTranscoder extends ImageTranscoder { 41 42 45 public JPEGTranscoder() { 46 hints.put(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white); 47 } 48 49 54 public BufferedImage createImage(int width, int height) { 55 return new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); 56 } 57 58 64 public void writeImage(BufferedImage img, TranscoderOutput output) 65 throws TranscoderException { 66 OutputStream ostream = output.getOutputStream(); 67 ostream = new OutputStreamWrapper(ostream); 71 72 if (ostream == null) { 73 throw new TranscoderException( 74 Messages.formatMessage("jpeg.badoutput", null)); 75 } 76 77 try { 78 float quality; 79 if (hints.containsKey(KEY_QUALITY)) { 80 quality = ((Float )hints.get(KEY_QUALITY)).floatValue(); 81 } else { 82 TranscoderException te; 83 te = new TranscoderException 84 (Messages.formatMessage("jpeg.unspecifiedQuality", null)); 85 handler.error(te); 86 quality = .75f; 87 } 88 89 JPEGImageEncoder jpegEncoder; 90 JPEGEncodeParam params; 91 jpegEncoder = JPEGCodec.createJPEGEncoder(ostream); 92 params = JPEGCodec.getDefaultJPEGEncodeParam(img); 93 params.setQuality(quality, true); 94 95 float PixSzMM = userAgent.getPixelUnitToMillimeter(); 96 int PixSzInch = (int)(25.4/PixSzMM+0.5); 97 params.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH); 98 params.setXDensity(PixSzInch); 99 params.setYDensity(PixSzInch); 100 jpegEncoder.encode(img, params); 101 ostream.flush(); 102 } catch (IOException ex) { 103 throw new TranscoderException(ex); 104 } 105 } 106 107 111 131 public static final TranscodingHints.Key KEY_QUALITY 132 = new QualityKey(); 133 134 137 private static class QualityKey extends TranscodingHints.Key { 138 public boolean isCompatibleValue(Object v) { 139 if (v instanceof Float ) { 140 float q = ((Float )v).floatValue(); 141 return (q > 0 && q <= 1f); 142 } else { 143 return false; 144 } 145 } 146 } 147 148 152 private static class OutputStreamWrapper extends OutputStream { 153 OutputStream os; 154 159 OutputStreamWrapper(OutputStream os) { 160 this.os = os; 161 } 162 163 public void close() throws IOException { 164 if (os == null) return; 165 try { 166 os.close(); 167 } catch (IOException ioe) { 168 os = null; 169 } 170 } 171 172 public void flush() throws IOException { 173 if (os == null) return; 174 try { 175 os.flush(); 176 } catch (IOException ioe) { 177 os = null; 178 } 179 } 180 181 public void write(byte[] b) throws IOException { 182 if (os == null) return; 183 try { 184 os.write(b); 185 } catch (IOException ioe) { 186 os = null; 187 } 188 } 189 190 public void write(byte[] b, int off, int len) throws IOException { 191 if (os == null) return; 192 try { 193 os.write(b, off, len); 194 } catch (IOException ioe) { 195 os = null; 196 } 197 } 198 199 public void write(int b) throws IOException { 200 if (os == null) return; 201 try { 202 os.write(b); 203 } catch (IOException ioe) { 204 os = null; 205 } 206 } 207 } 208 } 209 | Popular Tags |