用户在上传图片的时候,图片大小一般都相对来说比较大,可是我们的服务器存储空间有限,并且如果不对用户上传的图片进行处理,在页面显示的时候,将延缓页面加载时间,对用户体验来说不是很好,即使使用图片异步加载也是一样。今天我就将我在公司对用户上传图片的处理方案记录一下,以备后期使用。声明图片缩放关键代码是从谷歌上搜的,国外人写的代码确实比国内人写的不错,我只是对某些地方进行了修改,方便我使用。好了废话不多说,见代码。
1、图片缩放代码
package com.pindao.util;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
public class ImageSuoFang {
private String destPath;
private String originalPath;
private int width;
public ImageSuoFang(String originalPath,int width){
this.originalPath = originalPath;
this.width = width;
}
public String resize(String originalPath,
int newWidth) throws IOException {
/*if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
} */
File originalFile = new File(originalPath);
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
Date d = new Date();
String filename = d.getTime()+".jpg";
File destFile = new File(StringUtil.getParentPath(this.originalPath),filename);
this.destPath = destFile.getPath();
// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(new File(destPath));
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(1f, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
out.close();
return this.destPath;
} // Example usage
public static void main(String[] args) throws IOException {
// File originalImage = new File("C:\\11.jpg");
// resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
// resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
File originalImage = new File("C:/Users/Administrator/Desktop/安利/4.png");
// resize(originalImage, new File("C:/Users/Administrator/Desktop/安利/109.jpg"),1900, 0.7f);
// resize(originalImage, new File("C:/Users/Administrator/Desktop/安利/110.jpg"),1900, 1f);
}
public String getDestPath() {
return destPath;
}
}