分类: Java
2008-03-14 15:13:33
| #代码省去了冗长的异常捕获处理。 import javax.imageio.*; import java.io.*; import java.awt.image.*; import com.gif4j.GifEncoder; public class PhotoHandle{ public static void main(String[] args)throws Exception{ BufferedImage img = ImageIO.read(new File("1.jpg")); int h = img.getHeight(); int w = img.getWidth(); if(h>=96 && w >=96){ int nw = 96; int nh = (nw * h) / w; if(nh>96) { nh = 96; nw = (nh * w) / h; } ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage dest = new BufferedImage(nw, nh,BufferedImage.TYPE_4BYTE_ABGR); dest.getGraphics().drawImage(img,0,0,nw, nh,null); GifEncoder.encode(dest, out); //ImageIO.write(dest, "gif", out); FileOutputStream fos=new FileOutputStream("2.jpg"); out.writeTo(fos); out.close(); fos.close(); } } } |