package com.yj.until;
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;
/**
* @author 作者yangjing: 图片压缩
* @date 创建时间:2017-2-17 下午6:42:05
*/
public class ImageTransformer {
/**
* 图片压缩
* @param srcImage 源图片文件路径 (如:srcImage="G:/32/2015101713.jpg")
* @param tarImage 目的图片文件路径 (如:tarImage="G:/32/2015101713_720_720.jpg")
* @param maxPixel 转换的像素 (如:maxPixel=720)
* @param
*/
public static void transformer(String srcImage,String tarImage,int maxPixel) {
//源图片文件
File srcImageFile = new File(srcImage);
//目的图片文件
File tarImageFile = new File(tarImage);
// 生成图片转化对象
AffineTransform transform = new AffineTransform();
// 通过缓存读入缓存对象
BufferedImage image = null;
try {
image = ImageIO.read(srcImageFile);
} catch (IOException e) {
e.printStackTrace();
}
int imageWidth = image.getWidth();//原图片的高度
int imageHeight = image.getHeight();//原图片的宽度
int changeWidth = 0;//压缩后图片的高度
int changeHeight = 0;//压缩后图片的宽度
double scale = 0;// 定义小图片和原图片比例
if (maxPixel != 0) {
if (imageWidth > imageHeight) {
changeWidth = maxPixel;
scale = (double) changeWidth / (double) imageWidth;
changeHeight = (int) (imageHeight * scale);
} else {
changeHeight = maxPixel;
scale = (double) changeHeight / (double) imageHeight;
changeWidth = (int) (imageWidth * scale);
}
}
// 生成转换比例
transform.setToScale(scale, scale);
// 生成转换操作对象
AffineTransformOp transOp = new AffineTransformOp(transform, null);
//生成压缩图片缓冲对象
BufferedImage basll = new BufferedImage(changeWidth, changeHeight,
BufferedImage.TYPE_3BYTE_BGR);
//生成缩小图片
transOp.filter(image, basll);
try {
//输出缩小图片
ImageIO.write(basll, "jpeg",tarImageFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
java按像素压缩,生成压缩图片。
猜你喜欢
转载自blog.csdn.net/jingyang07/article/details/55657416
今日推荐
周排行