1 26 27 package net.sourceforge.groboutils.uicapture.v1; 28 29 import java.awt.image.BufferedImage ; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 34 import java.util.Iterator ; 35 36 import javax.imageio.ImageIO ; 37 38 import org.apache.log4j.Logger; 39 40 41 47 public class DefaultScreenScraper implements IScreenScraper 48 { 49 private static final Logger LOG = Logger.getLogger( DefaultScreenScraper.class ); 50 private String writerFormatName = null; 51 52 private static final boolean DEBUG = true; 53 54 55 58 59 65 public DefaultScreenScraper() 66 { 67 String [] names = ImageIO.getWriterFormatNames(); 68 if (names == null || names.length <= 0) 69 { 70 throw new IllegalArgumentException ( 71 "No writer format names supported." ); 72 } 73 74 this.writerFormatName = names[0]; 75 76 for (int i = 0; i < names.length; ++i) 77 { 78 LOG.debug( "Writer format found: "+names[i] ); 79 } 80 81 assertFormatName(); 82 } 83 84 85 91 public DefaultScreenScraper( String writerFormatName ) 92 { 93 this.writerFormatName = writerFormatName; 94 95 assertFormatName(); 96 } 97 98 99 102 103 104 114 public void writeImageToFile( BufferedImage image, File file ) 115 throws IOException 116 { 117 boolean result = ImageIO.write( image, this.writerFormatName, file ); 118 if (!result) 119 { 120 throw new IllegalStateException ( 121 "No appropriate writer was found." ); 122 } 123 } 124 125 126 127 133 public String getFileExtention() 134 { 135 return this.writerFormatName; 136 } 137 138 139 140 143 144 150 protected void assertFormatName() 151 { 152 Iterator iter = ImageIO.getImageWritersByFormatName( 153 this.writerFormatName ); 154 if (iter == null || !iter.hasNext()) 155 { 156 throw new IllegalArgumentException ( 157 "Image Format " + this.writerFormatName + 158 " is not supported in the current runtime system." ); 159 } 160 } 161 } 162 163 | Popular Tags |