java实现图片压缩

使用java压缩图片大小

原文链接


```java package com.study.testImg;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;

public class ChangeImg {

public static void main(String[] args) throws IOException {
	int newWidth = 360;
	int newHeight = 270;
	File imageFile = new File("C:\\Users\\yanlei\\Desktop\\图片\\2999300001-20200910152903-cc8201d173f04c66bae3e6f7ce32ca45-000.jpeg");
	String newPath = "C:\\Users\\yanlei\\Desktop\\图片\\缩放\\zoomImageScale.jpeg";
	zoomImageScale(imageFile, newPath, newWidth);
	newPath = "C:\\Users\\yanlei\\Desktop\\图片\\缩放\\constrainProportios.jpeg";
	constrainProportios(imageFile, newPath, newWidth);
	newPath = "C:\\Users\\yanlei\\Desktop\\图片\\缩放\\zoomImage.jpeg";
	zoomImage(imageFile, newPath, newWidth, newHeight);
}

/**
 * 按指定宽度 等比例缩放图片
 * 
 * @param imageFile
 * @param newPath
 * @param newWidth
 *            新图的宽度
 * @throws IOException
 */
public static void zoomImageScale(File imageFile, String newPath, int newWidth) throws IOException {
	if (!imageFile.canRead())
		return;
	BufferedImage bufferedImage = ImageIO.read(imageFile);
	if (null == bufferedImage)
		return;

	int originalWidth = bufferedImage.getWidth();
	int originalHeight = bufferedImage.getHeight();
	double scale = (double) originalWidth / (double) newWidth; // 缩放的比例

	int newHeight = (int) (originalHeight / scale);

	zoomImageUtils(imageFile, newPath, bufferedImage, newWidth, newHeight);
}

/**
 * 等比例改变图片尺寸,本方法也不能处理 png 图片
 * 
 * @param nw
 *            新图片的宽度
 * @param oldImage
 *            原图片
 * @throws IOException
 */
public static void constrainProportios(File imageFile,String newPath,int newWidth) throws IOException {
	AffineTransform transform = new AffineTransform();
	BufferedImage bis = ImageIO.read(imageFile);
	int w = bis.getWidth();
	int h = bis.getHeight();
	int nh = (newWidth * h) / w;
	double sx = (double) newWidth / w;
	double sy = (double) nh / h;
	transform.setToScale(sx, sy);
	AffineTransformOp ato = new AffineTransformOp(transform, null);
	BufferedImage bid = new BufferedImage(newWidth, nh, BufferedImage.TYPE_3BYTE_BGR);
	ato.filter(bis, bid);

	ImageIO.write(bid, "jpeg", new File(newPath));
	// ImageIO.write(bid, "jpeg", response.getOutputStream());
}

/**
 * 按尺寸缩放图片
 * 
 * @param imageFile
 * @param newPath
 * @param times
 * @throws IOException
 */
public static void zoomImage(File imageFile, String newPath, int width, int height) throws IOException {
	if (imageFile != null && !imageFile.canRead())
		return;
	BufferedImage bufferedImage = ImageIO.read(imageFile);
	if (null == bufferedImage)
		return;

	zoomImageUtils(imageFile, newPath, bufferedImage, width, height);
}

private static void zoomImageUtils(File imageFile, String newPath, BufferedImage bufferedImage, int width, int height) throws IOException {

	String suffix = StringUtils.substringAfterLast(imageFile.getName(), ".");

	// 处理 png 背景变黑的问题
	if (suffix != null && (suffix.trim().toLowerCase().endsWith("png") || suffix.trim().toLowerCase().endsWith("gif"))) {
		BufferedImage to = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = to.createGraphics();
		to = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
		g2d.dispose();

		g2d = to.createGraphics();
		Image from = bufferedImage.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
		g2d.drawImage(from, 0, 0, null);
		g2d.dispose();

		ImageIO.write(to, suffix, new File(newPath));
	} else {
		// 高质量压缩,其实对清晰度而言没有太多的帮助
		// BufferedImage tag = new BufferedImage(width, height,
		// BufferedImage.TYPE_INT_RGB);
		// tag.getGraphics().drawImage(bufferedImage, 0, 0, width, height,
		// null);
		//
		// FileOutputStream out = new FileOutputStream(newPath); // 将图片写入
		// newPath
		// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		// JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
		// jep.setQuality(1f, true); //压缩质量, 1 是最高值
		// encoder.encode(tag, jep);
		// out.close();

		BufferedImage newImage = new BufferedImage(width, height, bufferedImage.getType());
		Graphics g = newImage.getGraphics();
		g.drawImage(bufferedImage, 0, 0, width, height, null);
		g.dispose();
		ImageIO.write(newImage, suffix, new File(newPath));
	}
}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值