jsp上传图片并产生缩略图的代码

 我来答
uftdskfkthh
推荐于2016-03-05 · 超过28用户采纳过TA的回答
知道答主
回答量:58
采纳率:0%
帮助的人:57.3万
展开全部
/**
* 图片缩小算法,方形区域抽样
*/
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;

public class ImageUtil {

private int width;
private int height;
private int zoomWidth;
private int zoomHeight;
private File destFile;
private BufferedImage srcBufferImage;

public static void resizeFix(File srcFile, File destFile, int width,
int height) throws IOException {
new ImageUtil(srcFile, destFile, width, height);
}

public static void resizeFix(BufferedImage bufImg, File destFile,
int width, int height) throws IOException {
new ImageUtil(bufImg, destFile, width, height);
}

protected ImageUtil(File srcFile, File destFile, int zoomWidth,
int zoomHeight) throws IOException {
this.destFile = destFile;
this.zoomWidth = zoomWidth;
this.zoomHeight = zoomHeight;
this.srcBufferImage = javax.imageio.ImageIO.read(srcFile);
this.width = this.srcBufferImage.getWidth();
this.height = this.srcBufferImage.getHeight();
if (width <= zoomWidth && height <= zoomHeight) {
FileUtils.copyFile(srcFile, destFile);
} else {
resizeFix();
}
}

protected ImageUtil(BufferedImage srcBufferImage, File destFile,
int zoomWidth, int zoomHeight) throws IOException {
this.destFile = destFile;
this.zoomWidth = zoomWidth;
this.zoomHeight = zoomHeight;
this.srcBufferImage = srcBufferImage;
this.width = this.srcBufferImage.getWidth();
this.height = this.srcBufferImage.getHeight();
resizeFix();
}

/**
* 压缩图片
*
* @throws IOException
*/
protected void resizeFix() throws IOException {
if (width <= zoomWidth && height <= zoomHeight) {
resize(width, height);
} else if ((float) width / height > (float) zoomWidth / zoomHeight) {
resize(zoomWidth, Math.round((float) zoomWidth * height / width));
} else {
resize(Math.round((float) zoomHeight * width / height), zoomHeight);
}
}

private void resize(int w, int h) throws IOException {
BufferedImage imgBuf = scaleImage(w, h);
File parent = destFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
ImageIO.write(imgBuf, "jpeg", destFile);
}

private BufferedImage scaleImage(int outWidth, int outHeight) {
int[] rgbArray = srcBufferImage.getRGB(0, 0, width, height, null, 0,
width);
BufferedImage pbFinalOut = new BufferedImage(outWidth, outHeight,
BufferedImage.TYPE_INT_RGB);
double hScale = ((double) width) / ((double) outWidth);// 宽缩小的倍数
double vScale = ((double) height) / ((double) outHeight);// 高缩小的倍数

int winX0, winY0, winX1, winY1;
int valueRGB = 0;
long R, G, B;
int x, y, i, j;
int n;
for (y = 0; y < outHeight; y++) {
winY0 = (int) (y * vScale + 0.5);// 得到原图高的Y坐标
if (winY0 < 0) {
winY0 = 0;
}
winY1 = (int) (winY0 + vScale + 0.5);
if (winY1 > height) {
winY1 = height;
}
for (x = 0; x < outWidth; x++) {
winX0 = (int) (x * hScale + 0.5);
if (winX0 < 0) {
winX0 = 0;
}
winX1 = (int) (winX0 + hScale + 0.5);
if (winX1 > width) {
winX1 = width;
}
R = 0;
G = 0;
B = 0;
for (i = winX0; i < winX1; i++) {
for (j = winY0; j < winY1; j++) {
valueRGB = rgbArray[width * j + i];
R += getRedValue(valueRGB);
G += getGreenValue(valueRGB);
B += getBlueValue(valueRGB);
}
}
n = (winX1 - winX0) * (winY1 - winY0);
R = (int) (((double) R) / n + 0.5);
G = (int) (((double) G) / n + 0.5);
B = (int) (((double) B) / n + 0.5);
valueRGB = comRGB(clip((int) R), clip((int) G), clip((int) B));
pbFinalOut.setRGB(x, y, valueRGB);
}
}
return pbFinalOut;
}

private int clip(int x) {
if (x < 0)
return 0;
if (x > 255)
return 255;
return x;
}

private int getRedValue(int rgbValue) {
int temp = rgbValue & 0x00ff0000;
return temp >> 16;
}

private int getGreenValue(int rgbValue) {
int temp = rgbValue & 0x0000ff00;
return temp >> 8;
}

private int getBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
}

private int comRGB(int redValue, int greenValue, int blueValue) {

return (redValue << 16) + (greenValue << 8) + blueValue;
}

public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
ImageUtil.resizeFix(new File("d:/04.jpg"), new File("d:/big-n.jpg"),
400, 400);
long end = System.currentTimeMillis();
System.out.println("success:" + (end - start));
}
}

依赖commons-io.jar
千锋教育
2016-01-19 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
jsp产生缩略图:
//读入刚才上传的文件
java.io.File file = new java.io.File(saveurl);
//新的缩略图保存地址
String newurl=request.getRealPath("/")+url+"min_"+filename+"."+ext;
//构造Image对象 Image src = javax.imageio.ImageIO.read(file);
float tagsize=200;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null); //得到源图长
int new_w=0;
int new_h=0;
int temps教程ize;
float tempdouble;
if(old_w>old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
new_w=Math.round(old_w/tempdouble);
new_h=Math.round(old_h/tempdouble);//计算新图长宽
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码

newimage.close();
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
生术界锣华月号6252
2011-08-22
知道答主
回答量:53
采纳率:0%
帮助的人:26.8万
展开全部
我用的是uploadify
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式