1 45 46 package org.jfree.chart.encoders; 47 48 import java.util.Hashtable ; 49 50 56 public class ImageEncoderFactory { 57 private static Hashtable encoders = null; 58 59 static { 60 init(); 61 } 62 63 67 private static void init() { 68 encoders = new Hashtable (); 69 encoders.put("jpeg", "org.jfree.chart.encoders.SunJPEGEncoderAdapter"); 70 try { 71 Class.forName("javax.imageio.ImageIO"); 73 Class.forName("org.jfree.chart.encoders.SunPNGEncoderAdapter"); 75 encoders.put("png", 76 "org.jfree.chart.encoders.SunPNGEncoderAdapter"); 77 encoders.put("jpeg", 78 "org.jfree.chart.encoders.SunJPEGEncoderAdapter"); 79 } 80 catch (ClassNotFoundException e) { 81 encoders.put("png", 82 "org.jfree.chart.encoders.KeypointPNGEncoderAdapter"); 83 } 84 } 85 86 92 public static void setImageEncoder(String format, 93 String imageEncoderClassName) { 94 encoders.put(format, imageEncoderClassName); 95 } 96 97 104 public static ImageEncoder newInstance(String format) { 105 ImageEncoder imageEncoder = null; 106 String className = (String ) encoders.get(format); 107 if (className == null) { 108 throw new IllegalArgumentException ("Unsupported image format - " 109 + format); 110 } 111 try { 112 Class imageEncoderClass = Class.forName(className); 113 imageEncoder = (ImageEncoder) imageEncoderClass.newInstance(); 114 } 115 catch (Exception e) { 116 throw new IllegalArgumentException (e.toString()); 117 } 118 return imageEncoder; 119 } 120 121 129 public static ImageEncoder newInstance(String format, float quality) { 130 ImageEncoder imageEncoder = newInstance(format); 131 imageEncoder.setQuality(quality); 132 return imageEncoder; 133 } 134 135 143 public static ImageEncoder newInstance(String format, 144 boolean encodingAlpha) { 145 ImageEncoder imageEncoder = newInstance(format); 146 imageEncoder.setEncodingAlpha(encodingAlpha); 147 return imageEncoder; 148 } 149 150 159 public static ImageEncoder newInstance(String format, float quality, 160 boolean encodingAlpha) { 161 ImageEncoder imageEncoder = newInstance(format); 162 imageEncoder.setQuality(quality); 163 imageEncoder.setEncodingAlpha(encodingAlpha); 164 return imageEncoder; 165 } 166 167 } 168 | Popular Tags |