|
|
@@ -1,11 +1,6 @@
|
|
|
package com.izouma.awesomeadmin.util;
|
|
|
|
|
|
-import java.awt.*;
|
|
|
-import java.awt.color.ColorSpace;
|
|
|
-import java.awt.image.*;
|
|
|
-import java.io.*;
|
|
|
-import java.util.Iterator;
|
|
|
-
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
|
|
|
import javax.imageio.IIOException;
|
|
|
@@ -13,6 +8,11 @@ import javax.imageio.ImageIO;
|
|
|
import javax.imageio.ImageReader;
|
|
|
import javax.imageio.stream.ImageInputStream;
|
|
|
import javax.imageio.stream.ImageOutputStream;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.color.ColorSpace;
|
|
|
+import java.awt.image.*;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Iterator;
|
|
|
|
|
|
|
|
|
public class ImagesUtil {
|
|
|
@@ -64,14 +64,15 @@ public class ImagesUtil {
|
|
|
* 当scale>=1,缩略图height=comBase,width按原图宽高比例;若scale<1,缩略图width=comBase,height按原图宽高比例
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static InputStream changeMinPhoto(InputStream fin, String imageType, double comBase,
|
|
|
- double scale) {
|
|
|
+ public static InputStream changeMinPhotoOld(InputStream fin, String imageType, double comBase,
|
|
|
+ double scale) {
|
|
|
try {
|
|
|
// get image format in a file
|
|
|
//File file = new File("newimage.jpg");
|
|
|
|
|
|
// create an image input stream from the specified file
|
|
|
- ImageInputStream iis = ImageIO.createImageInputStream(fin);
|
|
|
+ ByteArrayOutputStream baos = cloneInputStream(fin);
|
|
|
+ ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(baos.toByteArray()));
|
|
|
|
|
|
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
|
|
|
if (readers == null || !readers.hasNext()) {
|
|
|
@@ -88,8 +89,30 @@ public class ImagesUtil {
|
|
|
image = reader.read(0); //RGB
|
|
|
} catch (IIOException e) {
|
|
|
// 读取Raster (没有颜色的转换).
|
|
|
- Raster raster = reader.readRaster(0, null);//CMYK
|
|
|
- image = createJPEG4(raster);
|
|
|
+// Raster raster = reader.readRaster(0, null);//CMYK
|
|
|
+// image = createJPEG4(raster);
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ String tmpImagename = "CMYK_" + MbappUtil.create_nonce_str() + "." + format;
|
|
|
+ File src = new File("/tmp/" + tmpImagename);
|
|
|
+ FileUtils.copyInputStreamToFile(new ByteArrayInputStream(baos.toByteArray()), src);
|
|
|
+ String[] cmd = {"sh", "-c", "convert -colorspace RGB -resize '4000000@>' -quality 60 /tmp/" + tmpImagename + " /tmp/RGB" + tmpImagename};
|
|
|
+ //System.out.println("1xxxx:" + new Date());
|
|
|
+ Process p = Runtime.getRuntime().exec(cmd);
|
|
|
+ //System.out.println("2xxxx:" + new Date());
|
|
|
+ p.waitFor();
|
|
|
+ //System.out.println("3xxxx:" + new Date());
|
|
|
+ image = ImageIO.read(new FileInputStream(new File("/tmp/RGB" + tmpImagename)));
|
|
|
+ //System.out.println("4xxxx:" + new Date());
|
|
|
+ if (!"".equals(tmpImagename)) {//清楚临时文件
|
|
|
+ deleteFile("/tmp/" + tmpImagename);
|
|
|
+ deleteFile("/tmp/RGB" + tmpImagename);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception ee) {
|
|
|
+ ee.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -129,6 +152,7 @@ public class ImagesUtil {
|
|
|
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
|
|
|
ImageIO.write(tag, imageType, imageOutput);
|
|
|
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
+ iis.close();
|
|
|
return inputStream;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -136,6 +160,51 @@ public class ImagesUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ public static InputStream changeMinPhoto(InputStream fin, String imageType) {
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream baos = cloneInputStream(fin);
|
|
|
+
|
|
|
+
|
|
|
+ String tmpImagename = "CMYK_" + MbappUtil.create_nonce_str() + "." + imageType;
|
|
|
+ File src = new File("/tmp/" + tmpImagename);
|
|
|
+ FileUtils.copyInputStreamToFile(new ByteArrayInputStream(baos.toByteArray()), src);
|
|
|
+ String[] cmd = {"sh", "-c", "convert -resize '4000000@>' -quality 60 /tmp/" + tmpImagename + " /tmp/RGB" + tmpImagename};
|
|
|
+ //System.out.println("1xxxx:" + new Date());
|
|
|
+ Process p = Runtime.getRuntime().exec(cmd);
|
|
|
+ //System.out.println("2xxxx:" + new Date());
|
|
|
+ p.waitFor();
|
|
|
+ //System.out.println("3xxxx:" + new Date());
|
|
|
+ InputStream inputStream = new FileInputStream(new File("/tmp/RGB" + tmpImagename));
|
|
|
+ //System.out.println("4xxxx:" + new Date());
|
|
|
+ if (!"".equals(tmpImagename)) {//清楚临时文件
|
|
|
+ deleteFile("/tmp/" + tmpImagename);
|
|
|
+ deleteFile("/tmp/RGB" + tmpImagename);
|
|
|
+ }
|
|
|
+
|
|
|
+ return inputStream;
|
|
|
+ } catch (Exception ee) {
|
|
|
+ ee.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static ByteArrayOutputStream cloneInputStream(InputStream input) {
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = input.read(buffer)) > -1) {
|
|
|
+ baos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ baos.flush();
|
|
|
+ return baos;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static BufferedImage createJPEG4(Raster raster) {
|
|
|
int w = raster.getWidth();
|
|
|
int h = raster.getHeight();
|
|
|
@@ -169,4 +238,21 @@ public class ImagesUtil {
|
|
|
ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
|
|
|
return new BufferedImage(cm, (WritableRaster) raster, true, null);
|
|
|
}
|
|
|
+
|
|
|
+ public static boolean deleteFile(String fileName) {
|
|
|
+ File file = new File(fileName);
|
|
|
+ // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
|
|
+ if (file.exists() && file.isFile()) {
|
|
|
+ if (file.delete()) {
|
|
|
+ System.out.println("删除单个文件" + fileName + "成功!");
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ System.out.println("删除单个文件" + fileName + "失败!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("删除单个文件失败:" + fileName + "不存在!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|