`
xbwolf
  • 浏览: 12605 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA图像缩放

    博客分类:
  • java
阅读更多
public void testImageZoom() {

		try {
			BufferedImage src = ImageIO.read(new File("d:\\tmp\\1.jpg")); // 读入文件
			int width = src.getWidth(); // 得到源图宽
			int height = src.getHeight(); // 得到源图长
                          
                          //  获取一个宽、长是原来1/2的图像实例
			Image image = src.getScaledInstance(width / 2, height / 2, Image.SCALE_DEFAULT);
			BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  //缩放图像
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null); // 绘制缩小后的图
			g.dispose();
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ImageIO.write(tag,"JPEG",bos);// 输出到bos
			 
			FileOutputStream out = new FileOutputStream("d:\\tmp\\result.jpg");
			out.write(bos.toByteArray());  //写文件
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics