ImageMagickUtils.java
6.7 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.framework.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ImageMagickUtils {
private static Log log = LogFactory.getLog(ImageMagickUtils.class);
private static final String MAGICK_DIRECTORY = PropertiesLoader.get("MAGICK_DIRECTORY", "/application.properties");
private static final String MAGICK_FILE = PropertiesLoader.get("MAGICK_FILE", "/application.properties");
private static final String IMAGE_CONFIG = PropertiesLoader.get("IMAGE_SIZE", "/application.properties");
public static Boolean resize(File ori) {
try {
log.info("MAGICK_FILE::::" + MAGICK_FILE);
int[][] whs = getImageSizeConfig();
String pathPicFrom = ori.getAbsolutePath();
log.info("pathPicFrom===============" + pathPicFrom);
try {
javax.imageio.ImageIO.read(ori);
} catch (Exception e) {
convertCYMK(pathPicFrom);
}
convertOrient(pathPicFrom);
String sizeStr = getImageSize(pathPicFrom);
log.info("sizeStr===============" + sizeStr);
if (StringUtils.isNotEmpty(sizeStr)) {
String[] wh = sizeStr.split("x");
log.info(wh[0] + "===============wh===================" + wh[1]);
int imageWideth = Integer.parseInt(wh[0]);
int imageHeight = Integer.parseInt(wh[1]);
for (int i = 0; i < whs.length; i++) {
int changeToWideth = whs[i][0];
int changeToHeight = whs[i][1];
if (imageWideth <= changeToWideth && imageHeight <= changeToHeight) {
File to = FileUtils.genOutFile(ori, changeToWideth, changeToHeight);
FileUtils.copyFile(ori, to);
} else {
File saveFile = FileUtils.genOutFile(ori, changeToWideth, changeToHeight);
int[] outwh = new int[2];
if (imageWideth >= imageHeight) {
outwh[0] = changeToWideth;
outwh[1] = (int) (1.0 * imageHeight / imageWideth * changeToWideth);
} else {
outwh[0] = (int) (imageWideth * changeToHeight / imageHeight * 1.0);
outwh[1] = changeToHeight;
}
// 修正開始轉換圖檔
// 改用bat檔直接執行convert指令
String pathPicTo = saveFile.getAbsolutePath();
log.info("pathPicTo===============" + pathPicTo);
convertPic(pathPicFrom, outwh, pathPicTo);
}
}
}
} catch (IOException ioe) {
log.error("File " + ori.getAbsolutePath() + " io error!!!", ioe);
return false;
}
return true;
}
private static String getImageSize(String source) throws IOException {
List<String> parameterList = new ArrayList<String>();
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
parameterList.add(MAGICK_FILE);
}
parameterList.add("identify");
parameterList.add("-format");
parameterList.add("%wx%h");
parameterList.add(source);
log.info("getImageSize parameterList======" + parameterList);
ProcessBuilder processBuilder = new ProcessBuilder(parameterList);
processBuilder.redirectErrorStream(true);
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
processBuilder.directory(new File(MAGICK_DIRECTORY));
}
Process process = processBuilder.start();
// Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String imageSize = br.readLine();
return imageSize.startsWith("identify") ? null : imageSize;
}
private static void convertPic(String pathPicFrom, int[] outwh, String pathPicTo) throws IOException {
if (!new File(pathPicTo).exists()) {
List<String> parameterList = new ArrayList<String>();
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
parameterList.add(MAGICK_FILE);
}
parameterList.add("convert");
parameterList.add("-resize");
parameterList.add(String.valueOf(outwh[0]) + "x" + String.valueOf(outwh[1]));
parameterList.add("-antialias");
parameterList.add(pathPicFrom);
parameterList.add(pathPicTo);
log.info("convertPic parameterList======" + parameterList);
ProcessBuilder processBuilder = new ProcessBuilder(parameterList);
processBuilder.redirectErrorStream(true);
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
processBuilder.directory(new File(MAGICK_DIRECTORY));
}
waitForProcess(processBuilder.start());
}
}
private static void convertCYMK(String pathPicFrom) throws IOException {
List<String> parameterList = new ArrayList<String>();
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
parameterList.add(MAGICK_FILE);
}
parameterList.add("mogrify");
parameterList.add("-colorspace");
parameterList.add("RGB");
parameterList.add("-quality");
parameterList.add("80");
parameterList.add(pathPicFrom);
log.info("convertCYMK parameterList======" + parameterList);
ProcessBuilder processBuilder = new ProcessBuilder(parameterList);
processBuilder.redirectErrorStream(true);
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
processBuilder.directory(new File(MAGICK_DIRECTORY));
}
waitForProcess(processBuilder.start());
}
private static void convertOrient(String pathPicFrom) throws IOException {
List<String> parameterList = new ArrayList<String>();
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
parameterList.add(MAGICK_FILE);
}
parameterList.add("convert");
parameterList.add(pathPicFrom);
parameterList.add("-auto-orient");
parameterList.add(pathPicFrom);
log.info("convertOrient parameterList======" + parameterList);
ProcessBuilder processBuilder = new ProcessBuilder(parameterList);
processBuilder.redirectErrorStream(true);
if (StringUtils.isNotEmpty(MAGICK_DIRECTORY)) {
processBuilder.directory(new File(MAGICK_DIRECTORY));
}
waitForProcess(processBuilder.start());
}
private static void waitForProcess(Process process) throws IOException {
// Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
private static int[][] getImageSizeConfig() {
int[][] result;
String[] p = IMAGE_CONFIG.split(";");
result = new int[p.length][2];
for (int i = 0; i < p.length; i++) {
String m = p[i];
m = m.replace("[", "");
m = m.replace("]", "");
String[] y = m.split(",");
result[i][0] = Integer.parseInt(y[1].replace("\"", ""));
result[i][1] = Integer.parseInt(y[2].replace("\"", ""));
}
return result;
}
}