java 图片处理工具类
方法说明
| 方法 | 描述 |
|---|
| cutCenterImage | 从中间裁剪图片不进行缩放 |
| zoomImage | 缩放图片根据最短边 |
| zoomCutCenterImage | 缩放裁剪图片,先进行缩放,然后进行裁剪 |
实现代码
public class ImgUtils {
public static String format = "PNG";
public static int unit = 1024;
public static BufferedImage cutCenterImage(BufferedImage image, int width, int height) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, format, os);
InputStream input = new ByteArrayInputStream(os.toByteArray());
Iterator iterator = ImageIO.getImageReadersByFormatName(format);
ImageReader reader = (ImageReader) iterator.next();
ImageInputStream iis = ImageIO.createImageInputStream(input);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
int imageIndex = 0;
Rectangle rect = new Rectangle((reader.getWidth(imageIndex) - width) / 2, (reader.getHeight(imageIndex) - height) / 2, width, height);
param.setSourceRegion(rect);
return reader.read(0, param);
}
public static BufferedImage zoomImage(BufferedImage image, int width, int height) throws Exception {
double wr = 0, hr = 0;
//获取缩放比例
double wRatio = width * 1.0 / image.getWidth();
double hRatio = height * 1.0 / image.getHeight();
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio, hRatio), null);
return ato.filter(image, null);
}
public static BufferedImage zoomImage(InputStream is, Integer size) throws Exception {
int fileSize = is.available();
//文件大于size时,才进行缩放。注意:size以K为单位
if (fileSize < size * unit) {
return ImageIO.read(is);
}
// 获取长*宽(面积)缩放比例
double sizeRate = (size * 1024 * 0.5) / fileSize;
// 获取长和宽分别的缩放比例,即面积缩放比例的2次方根
double sideRate = Math.sqrt(sizeRate);
BufferedImage bufImg = ImageIO.read(is);
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(sideRate, sideRate), null);
return ato.filter(bufImg, null);
}
public static BufferedImage zoomCutCenterImage(BufferedImage image, int width, int height) throws Exception {
// 读文件获取
int w = image.getWidth();
int h = image.getHeight();
double wRatio = 1.0 * width / w;
double hRatio = 1.0 * height / h;
double ratio = Math.max(wRatio, hRatio);
BufferedImage zoomImage = zoomImage(image, (int) (w * ratio), (int) (h * ratio));
return cutCenterImage(zoomImage, width, height);
}
public static byte[] getByte(File file) {
byte[] bytes = null;
try {
FileInputStream fis = new FileInputStream(file);
bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}
public static void writeFile(BufferedImage image, File file) throws IOException {
ImageIO.write(image, format, file);
}
public static void writeFile(BufferedImage image, String filePath) throws IOException {
ImageIO.write(image, format, new File(filePath));
}
public static BufferedImage getBufferedImage(InputStream is) throws IOException {
return ImageIO.read(is);
}
private static BufferedImage getBufferedImage(File file) throws IOException {
return ImageIO.read(file);
}
private static String getImageType(File file) {
if (file != null && file.exists() && file.isFile()) {
String fileName = file.getName();
int index = fileName.lastIndexOf(".");
if (index != -1 && index < fileName.length() - 1) {
return fileName.substring(index + 1);
}
}
return null;
}
}