咖啡汪日志——做人脸图片上传时,用到的一个very好用的java图片压缩工具类,
程序员文章站
2022-04-08 14:57:48
...
package com.cccc.common.utils;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import javax.swing.ImageIcon;
/**
* 描述:图片工具类
*
* @Author: Yue Zejian
* @Date: Created in 2021/11/22 15:41
*/
public class ImageUtils {
public static void main(String[] args) throws Exception{
//需要压缩的图片地址 123.jpg为需要压缩的图片
File customaryFile = new File("C://D//Document//123.jpg");
//压缩过后输出的路径地址 456.jpg 可进行设置为任意名称
File compressAfter = new File("C://D//Document//456.jpg");
//长宽需要根据需要等比例调整,会直接影响到压缩后的图片大小,(600*1250 0.8f 可以把17.8M的图片压缩到 68K)
ImageUtils.imageResize(customaryFile,compressAfter,600,1250,0.8f);
}
/**
* 缩放图片(压缩图片质量,改变图片尺寸)
* 若原图宽度小于新宽度,则宽度不变
* @param originalFile 原图片路径地址
* @param resizedFile 压缩后输出路径地址
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* @param quality 图片质量参数 0.7f 相当于70%质量
*/
public static void imageResize(File originalFile, File resizedFile,
int maxWidth,int maxHeight, float quality) throws IOException {
if (quality > 1) {
throw new IllegalArgumentException(
"图片质量需设置在0.1-1范围");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
int newWidth = maxWidth;
if(iWidth < maxWidth){
newWidth = iWidth;
}
if (iWidth >= iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
}
int newHeight = maxHeight;
if(iHeight < maxHeight){
newHeight = iHeight;
}
if(resizedImage==null && iHeight >= iWidth){
resizedImage = i.getScaledInstance((newHeight * iWidth) / iHeight,
newHeight, Image.SCALE_SMOOTH);
}
//此代码确保加载图像中的所有像素
Image temp = new ImageIcon(resizedImage).getImage();
//创建缓冲图像
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
//将图像复制到缓冲图像
Graphics g = bufferedImage.createGraphics();
//清除背景并绘制图像。
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
//将jpeg写入文件
String formatName = resizedFile.getName().substring(resizedFile.getName().lastIndexOf(".") + 1);
ImageIO.write(bufferedImage, formatName ,resizedFile );
}
/**
* 图片转化为byte数组
* @param path
* @return
*/
public byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
/**
* byte数组转化为图片
* @param data
* @param path
*/
public void byte2image(byte[] data,String path){
if(data.length<3||path.equals("")) {
return;
}
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
}