Java一种减少图片存储空间的方法(转换成jpg格式)

    主要用到BufferedImage对象,将原始图片保存为jpg格式:

public static void thumbImage(String input,String newFile) throws Exception {
		Image src = javax.imageio.ImageIO.read(new File(input));
		int width = src.getWidth(null);// 获取图源宽度
		int height = src.getHeight(null);// 获取图源高度
		BufferedImage thumb = new BufferedImage(width / 1, height / 1, 
				BufferedImage.TYPE_INT_RGB);
		// 绘制缩小后的图
		thumb.getGraphics().drawImage(src, 0,  0, width / 1, height / 1, null);
		File file = new File(newFile);// 输出到文件流
		ImageIO.write(thumb, "jpg", file);
}

    使用效果如何:原图-1947K,转换后-716K,效果好像比较明显,图片质量差别不太大。

    

猜你喜欢

转载自my.oschina.net/jacktanger/blog/1613754