首页 > 应用开发 > Java > 将一图片同比例缩小绘制(转)
/* * Created on 2004-10-11 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package net.jspcn.test; import java.io.File; import java.io.FileOutputStream; import java.awt.Image; import java.awt.image.BufferedImage; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class JpgTest { public void jpgTset() throws Exception{ File _file = new File("d:1.jpg"); //读入文件 Image src = javax.imageio.ImageIO.read(_file); //构造Image对象 int wideth=src.getWidth(null); //得到源图宽 int height=src.getHeight(null); //得到源图长 BufferedImage tag = new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src,0,0,wideth/2,height/2,null); //绘制缩小后的图 FileOutputStream out=new FileOutputStream("D: ewfile.jpg"); //输出到文件流 //File file = new File("D: ewFile.jpg"); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); //JPEGImageEncoder encoder = JPEGCodec.c encoder.encode(tag); //近JPEG编码 out.close(); } public static void main(String[] args){ try{ new JpgTest().jpgTset(); }catch(Exception e){ e.printStackTrace(); } } } |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9650775/viewspace-922496/ ,如需转载,请注明出处,否则将追究法律责任。