1 package de.jwi.jgallery.imageio; 2 3 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.Serializable ; 28 import java.util.Iterator ; 29 30 import javax.imageio.ImageIO ; 31 import javax.imageio.ImageWriteParam ; 32 import javax.imageio.ImageWriter ; 33 import javax.imageio.stream.ImageOutputStream ; 34 35 import java.awt.image.renderable.ParameterBlock ; 36 37 import javax.media.jai.Interpolation; 38 import javax.media.jai.JAI; 39 import javax.media.jai.RenderedOp; 40 41 import com.sun.media.jai.codec.FileSeekableStream; 42 43 import de.jwi.jgallery.IThumbnailWriter; 44 45 49 50 public class JAIThumbnailWriter 51 implements IThumbnailWriter, Serializable 52 { 53 public void write(File infile, File outfile, float compressionQuality, 54 int thumbBounds) throws IOException 55 { 56 57 58 59 RenderedOp image = JAI.create("stream", new FileSeekableStream(infile)); 60 61 int imageWidth = image.getWidth(); 62 int imageHeight = image.getHeight(); 63 64 float thumbRatio = (float) thumbBounds 65 / Math.max(imageWidth, imageHeight); 66 67 71 72 Interpolation interp = Interpolation.getInstance( 73 Interpolation.INTERP_BILINEAR); 74 75 80 81 ParameterBlock params = new ParameterBlock (); 82 params.addSource(image); 83 params.add(thumbRatio); params.add(thumbRatio); params.add(0.0F); params.add(0.0F); params.add(interp); 89 90 RenderedOp thumbImage = JAI.create("scale", params); 91 92 93 ImageWriter writer = null; 95 Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); 96 if (iter.hasNext()) 97 { 98 writer = (ImageWriter ) iter.next(); 99 } 100 101 ImageWriteParam iwp = writer.getDefaultWriteParam(); 102 iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 103 iwp.setCompressionQuality(compressionQuality); 104 105 ImageOutputStream ios = ImageIO.createImageOutputStream(outfile); 107 writer.setOutput(ios); 108 109 writer.write(thumbImage); 110 111 ios.flush(); 113 writer.dispose(); 114 ios.close(); 115 } 116 117 } 118 | Popular Tags |