CompressPic.java
3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.example.dahua.lib;
import com.example.dahua.async.ImageUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* 图片压缩工具
*/
public class CompressPic {
public static String CompressPic(String srcPath, String targetPath, String studentcode) throws Exception {
double cutPercent = 0.5;
File file = new File(srcPath.trim());
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
int width = bufferedImage.getWidth(null);
int height = bufferedImage.getHeight(null);
long fileLength = file.length();
if ((fileLength / 1024) < 64) {
writeImgToFile(bufferedImage, width, height, targetPath);
} else
while ((fileLength / 1024) >= 64) {
width = (int) (width * (1 - cutPercent));
height = (int) (height * (1 - cutPercent));
writeImgToFile(bufferedImage, width, height, targetPath);
File file1 = new File(targetPath);
BufferedImage bufferedImage1 = ImageIO.read(new FileInputStream(file1));
width = bufferedImage1.getWidth(null);
height = bufferedImage1.getHeight(null);
fileLength = file1.length();
}
// System.out.printf("图片大小:"+fileLength/1024);
return targetPath;
}
private static void writeImgToFile(BufferedImage bufferedImage, int width, int height, String targetPath) {
try {
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
tag.getGraphics().drawImage(bufferedImage, 0, 0, width, height, null); // 绘制缩小后的图
FileOutputStream deskImage = new FileOutputStream(targetPath); // 输出到文件流
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);
// encoder.encode(tag); // 近JPEG编码
boolean write = ImageIO.write(tag, "jpg", deskImage);
/*if (write) {
int angle = ImageUtils.getImgRotateAngle(targetPath);
System.out.println("angle:" + angle);
if (angle == 0) {
// ImageUtils.rotatePhonePhoto(targetPath, 90);
}
}*/
System.out.println("读写图片:" + write);
deskImage.close();
} catch (Exception e) {
System.out.println("读写图片:" + e.toString());
e.printStackTrace();
}
}
/**
* @param file 源文件
* @param realpath 存放图片路径
* @param fileName 图片文件名字
* @return
*/
public static String writeUploadFile(MultipartFile file, String realpath, String fileName) {
File fileDir = new File(realpath);
if (!fileDir.exists())
fileDir.mkdirs();
InputStream input = null;
FileOutputStream fos = null;
String outPath = realpath + fileName;
try {
input = file.getInputStream();
fos = new FileOutputStream(outPath);
IOUtils.copy(input, fos);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(fos);
}
return outPath;
}
}