1 import java.awt.Image; 2 import java.awt.image.BufferedImage; 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 8 import javax.imageio.ImageIO; 9 10 11 12 public class ChangeSize { 13 14 15 public static void main(String[] args) throws Exception { 16 //读取图片 17 BufferedInputStream in = new BufferedInputStream(new FileInputStream("E:/test.png")); 18 //字节流转图片对象 19 Image bi =ImageIO.read(in); 20 //获取图像的高度,宽度 21 int height=bi.getHeight(null); 22 int width =bi.getWidth(null); 23 //构建图片流 24 BufferedImage tag = new BufferedImage(width / 2, height / 2, BufferedImage.TYPE_INT_RGB); 25 //绘制改变尺寸后的图 26 tag.getGraphics().drawImage(bi, 0, 0,width / 2, height / 2, null); 27 //输出流 28 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("E:/copy.png")); 29 //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 30 //encoder.encode(tag); 31 ImageIO.write(tag, "PNG",out); 32 in.close(); 33 out.close();
//转字节流
//ByteArrayOutputStream out = new ByteArrayOutputStream();
//ImageIO.write(tag, "PNG",out);
//InputStream is = new ByteArrayInputStream(out.toByteArray());
34 } 35 }