-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCompressor.java
97 lines (76 loc) · 2.59 KB
/
ImageCompressor.java
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
package qiwi.util;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jetbrains.annotations.NotNull;
import qiwi.util.bean.converter.ImageIOConverter;
import javax.imageio.*;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Locale;
import static qiwi.core.util.converter.ValueConverter.convertTo;
@Singleton
public class ImageCompressor {
private static final Logger log = LoggerFactory.getLogger(ImageCompressor.class);
@NotNull
public byte[] compress(@NotNull byte[] image) {
return compress(image, DEFAULT_COMPRESSION_QUALITY);
}
@NotNull
public byte[] compress(@NotNull byte[] image, float compressionQuality) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageOutputStream iout = null;
try {
doCompress(image, compressionQuality, iout = ImageIOConverter.toImageOutputStream(out));
iout.flush();
return out.toByteArray();
} catch (IOException e) {
log.error("ImageCompressor compress exception", e);
throw new IllegalStateException(e);
} finally {
try {
out.close();
if (iout != null) {
iout.close();
}
} catch (IOException ignored) {
}
}
}
private void doCompress(@NotNull byte[] image, float compressionQuality, ImageOutputStream iout) {
final RenderedImage renderedImage = convertTo(RenderedImage.class, image);
final ImageWriter imageWriter = convertTo(ImageWriter.class, image);
imageWriter.setOutput(iout);
try {
imageWriter.write(
null,
new IIOImage(renderedImage, null, null),
makeImageWriteParam(compressionQuality)
);
} catch (Exception e) {
log.error("ImageCompressor doCompress exception", e);
throw new IllegalStateException(e);
} finally {
imageWriter.dispose();
}
}
private JPEGImageWriteParam makeImageWriteParam(float compressionQuality) {
final JPEGImageWriteParam imageWriteParam = new JPEGImageWriteParam(Locale.getDefault()) {
@Override
public void setCompressionQuality(float quality) {
if (quality < 0.0f || quality > 1.0f) {
throw new IllegalArgumentException("Quality must be in the range [0.0..1.0]");
}
this.compressionQuality = 256 - (quality * 256);
}
};
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(compressionQuality);
return imageWriteParam;
}
/* 80%-quality of the original */
private static final float DEFAULT_COMPRESSION_QUALITY = 0.8f;
}