📄 imageutil.java
字号:
package com.struts2.framework.util;
import com.sun.image.codec.jpeg.*;
import java.awt.Dimension;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
public class ImageUtil {
public static Dimension getDimension(InputStream in) {
try {
ImageInfo ii = new ImageInfo();
ii.setInput(in);
if (ii.check()) {
int width = ii.getWidth();
int height = ii.getHeight();
if (width > 0 && height > 0) {
int format = ii.getFormat();
if (format == ImageInfo.FORMAT_GIF
|| format == ImageInfo.FORMAT_JPEG
|| format == ImageInfo.FORMAT_PNG
|| format == ImageInfo.FORMAT_SWF)
return new Dimension(width, height);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static boolean convert(String oldName, String newSummName,
int width, int height) {
BufferedImage bufferedImage = new BufferedImage(width, height, 1);
Object uc = null;
try {
URL u = new URL("file", "localhost", oldName);
uc = u.getContent();
} catch (IOException ioexception) {
return false;
}
ImageProducer scaledImageProducer = null;
int pixels[] = new int[width * height];
try {
scaledImageProducer = new FilteredImageSource((ImageProducer) uc,
new AreaAveragingScaleFilter(width, height));
PixelGrabber pg = new PixelGrabber(scaledImageProducer, 0, 0,
width, height, pixels, 0, width);
pg.grabPixels();
} catch (InterruptedException interruptedexception) {
return false;
}
bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);
byte result[] = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JPEGEncodeParam encParam = JPEGCodec
.getDefaultJPEGEncodeParam(bufferedImage);
encParam.setQuality(0.75F, false);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos, encParam);
try {
encoder.encode(bufferedImage, encParam);
baos.flush();
byte encodedImage[] = baos.toByteArray();
baos.close();
OutputStream os = new FileOutputStream(newSummName);
os.write(encodedImage);
os.flush();
os.close();
os = null;
} catch (IOException ioexception1) {
return false;
}
return true;
}
public ImageUtil() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -