JAVA进行图片压缩

处理图片压缩的终极版本。附上测试用例。

/**
	 * 
	 * 压缩图片,并等比缩小。
	 * 
	 * @author aren
	 * @param data
	 *            输入图片数据的byte[]。
	 * @param width
	 *            最大输出宽度,但是最后会根据图片本身比例调整。推荐值800。
	 * @param height
	 *            最大输出高度,但是最后会根据图片本身比例调整。推荐值600。
	 * @param type
	 *            指定最后存储的图片类型,支持字符串jpg,png,gif,bmp,jpeg。如果为null,则默认输出jpg格式图片。
	 * @param maxSize
	 *            指定最大输出图片的容量大小。可以为null表示不指定压缩容量大小。不要小于10000,推荐100000。
	 * @return 输出图片数据的byte[]。
	 * @throws Exception
	 */
	public byte[] zipImageToScaledSize(byte[] data, int width, int height, String type, Integer maxSize)
			throws Exception {
		if (data == null) {
			return null;
		}
		if (width <= 0 || height <= 0) {
			width = 800;
			height = 600;
		}
		// 设定输出格式
		String[] supportType = new String[] { "jpg", "png", "bmp", "jpeg", "gif" };
		if (type == null || !ArrayUtils.contains(supportType, type)) {
			type = "jpg";
		}
		int pointedHeight;
		int pointedWidth;
		ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
		BufferedImage bufferedImage = ImageIO.read(inputStream);
		inputStream.close();
		int originalHeight = bufferedImage.getHeight();
		int originalWidth = bufferedImage.getWidth();
		// 设定等比例压缩。
		if ((originalHeight / (double) height) > (originalWidth / (double) width)) {
			pointedHeight = NumberUtils.min(height, originalHeight);
			pointedWidth = -1;
		} else {
			pointedHeight = -1;
			pointedWidth = NumberUtils.min(width, originalWidth);
		}
		// 压缩图片,此处附上颜色类型BufferedImage.TYPE_INT_RGB。Color.WHITE,可以有效避免png转jpg时图片发红的问题。
		Image newImage = bufferedImage.getScaledInstance(pointedWidth, pointedHeight, Image.SCALE_SMOOTH);
		BufferedImage newBufferedImage = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null),
				BufferedImage.TYPE_INT_RGB);		
		newBufferedImage.getGraphics().drawImage(newImage, 0, 0,Color.WHITE, null);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ImageIO.write(newBufferedImage, type, byteArrayOutputStream);
		byteArrayOutputStream.close();
		data = byteArrayOutputStream.toByteArray();
		if (maxSize != null && data.length > maxSize) {
			// 设定递归的保险,以免图片质量太差
			if (maxSize < 5000 && (data.length > 10 * maxSize)) {
				maxSize = 5000;
			}
			// 递归压缩
			double scale = Math.max(Math.pow(maxSize / (double) data.length, 0.5), 0.9);
			return zipImageToScaledSize(data, (int) (width * scale), (int) (height * scale), type, maxSize);
		} else {
			return data;
		}
}

 附上测试用例。

	@Test
	public void getformatname() throws Exception {
		//文件夹遍历图片,文件夹地址自定。
		File fileFolder=new File("D:/downloads/testImage");
		File[] fileList=fileFolder.listFiles();
		for(File file:fileList){
		
		InputStream input = new FileInputStream(file);
		byte[] data = new byte[(input.available())];
		input.read(data);
		input.close();
		String type="bmp";
		data = zipImageToScaledSize(data,600, 800, type,100000);

		OutputStream output = new FileOutputStream("D:/downloads/testResult/"+Math.random()+"."+type);
		output.write(data);
		output.close();
		}
	}
发布了14 篇原创文章 · 获赞 6 · 访问量 7767

猜你喜欢

转载自blog.csdn.net/freshrookie/article/details/82665555
今日推荐