pull/371/head
wuyibo 2 years ago
parent 1be789faab
commit 1556fc1c2d

@ -1,141 +0,0 @@
package com.ruoyi.system.utils;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* @author
* @date 20230811 16:52
* @Description Javautil,
*/
public class ImageCompressUtil {
/**
*
* ()
*
* @param oldFile
* @param width
* @param height
* @param quality
* @param smallIcon (),yasuo.jpg,yasuo(+smallIcon).jpg
* @return
*/
public static String zipImageFile(String oldFile, int width, int height,
float quality, String smallIcon) {
if (oldFile == null) {
return null;
}
String newImage = null;
try {
/**对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(new File(oldFile));
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
/** 压缩后的文件名 */
newImage = filePrex + smallIcon + oldFile.substring(filePrex.length());
/** 压缩之后临时存放位置 */
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
}
/**
* ()
* @param fileName
* @param is
* @return
*/
public static String writeFile(String fileName, InputStream is) {
if (fileName == null || fileName.trim().length() == 0) {
return null;
}
try {
/** 首先保存到临时文件 */
FileOutputStream fos = new FileOutputStream(fileName);
byte[] readBytes = new byte[512];// 缓冲大小
int readed = 0;
while ((readed = is.read(readBytes)) > 0) {
fos.write(readBytes, 0, readed);
}
fos.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
/**
*
*
* @param srcURL
* @param deskURL
* @param comBase
* @param scale (/) 1
* scale>=1,height=comBase,width;scale<1,width=comBase,height
* @throws Exception
* @author shenbin
* @createTime 2014-12-16
* @lastModifyTime 2014-12-16
*/
public static void saveMinPhoto(String srcURL, String deskURL, double comBase,
double scale) throws Exception {
File srcFile = new java.io.File(srcURL);
Image src = ImageIO.read(srcFile);
int srcHeight = src.getHeight(null);
int srcWidth = src.getWidth(null);
int deskHeight = 0;// 缩略图高
int deskWidth = 0;// 缩略图宽
double srcScale = (double) srcHeight / srcWidth;
/**缩略图宽高算法*/
if ((double) srcHeight > comBase || (double) srcWidth > comBase) {
if (srcScale >= scale || 1 / srcScale > scale) {
if (srcScale >= scale) {
deskHeight = (int) comBase;
deskWidth = srcWidth * deskHeight / srcHeight;
} else {
deskWidth = (int) comBase;
deskHeight = srcHeight * deskWidth / srcWidth;
}
} else {
if ((double) srcHeight > comBase) {
deskHeight = (int) comBase;
deskWidth = srcWidth * deskHeight / srcHeight;
} else {
deskWidth = (int) comBase;
deskHeight = srcHeight * deskWidth / srcWidth;
}
}
} else {
deskHeight = srcHeight;
deskWidth = srcWidth;
}
BufferedImage tag = new BufferedImage(deskWidth, deskHeight, BufferedImage.TYPE_3BYTE_BGR);
tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null); //绘制缩小后的图
FileOutputStream deskImage = new FileOutputStream(deskURL); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);
encoder.encode(tag); //近JPEG编码
deskImage.close();
}
}
Loading…
Cancel
Save