永发信息网

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

答案:3  悬赏:20  手机版
解决时间 2021-02-12 21:04
jsp上传图片并产生缩略图的代码
最佳答案
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();
全部回答
我有个包,可以在tomcat中运行,不过没有 source code.. 要的找我要。 已发送。。
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(); } 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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
【厌倦的意思】毫不厌倦的意思,反义词,近义词
清原满族自治县红透山镇职业技术学校在哪里啊
我有时睡觉时会梦见以后会发生的事的某一情景
隆辉驾校(惠州惠东县)地址好找么,我有些事要
爱的那么认真
帮帮忙啊 我的三叶草怎么总坏啊 就是鞋底和
福美意鲜供直达精品超市地址在哪,我要去那里
除了菲利普岛,墨尔本附近还有哪里能看到小企
八星驾校(惠州惠东县)地址在哪,我要去那里办
关于保险的论述题:为具有一定特点的某单位设
宝骏560换了备胎仪表盘是不是有故障灯亮
一品蛋糕旗舰店这个地址在什么地方,我要处理
Ⅰ、Ⅱ类电能计量装置准确度最低要求为有功电
安泰驾校(惠州惠东县)地址有知道的么?有点事
找歇后语的典故40字
推荐资讯
棒棒门业游埠专卖店在哪里啊,我有事要去这个
东村街道青年中心青少年服务管理站这个地址在
王合头村村地址在哪,我要去那里办事
有使用索尼swr30手环的吗
中国拥有6艘航母要几年
男朋友亲姐姐给我送菜我该怎么说
请问男生声乐有假声吗?
加盟英利光伏后多少钱可以拿货
腾达路由器电源线插不进去,里面有个铁片卡着
什么是自动喷水灭火系统末端试水装置
不是石家庄户口的话,做免费孕前检查需要什么
格尔木致美概念工作室地址有知道的么?有点事
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?