请问怎么把图片生成缩略图 请问怎么把图片生成缩略图,例如我想把一个400*400的图片,变成80*80的图片,请问应该怎么写,或说一下用什么知识实现,最好有具体的例子说明一下,帮帮我,多谢大家了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 struts有这个功能 可以去看看 这个也可以用比较简单的方法实现,可以把上传的文件保存到服务器的一个临时目录下,然后<img>这个标签是可以设置高度和宽度的,就OK了。 我这里有段servlet的service方法可以实现,你瞅瞅,可以的话分数的拿来!public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); BufferedImage image = new BufferedImage(600, 500, BufferedImage.TYPE_INT_RGB); Random r = new Random(); Graphics g = image.getGraphics(); g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g.fillRect(0, 0, 600, 500); g.setColor(new Color(0,0,0)); String number = String.valueOf(r.nextInt(99999)); g.drawString(number, 5, 15); OutputStream os = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(image); public static long zoomMax( Image srcImage, String destPath, int maxWidth, int maxHeight ) throws IOException{ return zoomMax( srcImage, destPath, maxWidth, maxHeight, false ); }/** * 缩小图片,产生缩略图 * @param srcImage 源图片对象 * @param destPath 放小后放置路径 * @param maxWidth 放小后的最大宽度 * @param maxHeight 放小后的最大高度 * @param force 是否强制转换 * @return 如果成功返回目标文件的大小,否则返回0 */ public static long zoomMax( Image srcImage, String destPath, int maxWidth, int maxHeight, boolean force ) throws IOException { if( srcImage== null || destPath == null || maxWidth < 0 || maxHeight < 0 ) return 0L; int w = srcImage.getWidth(null); int h = srcImage.getHeight(null); int[] newWidHgt = getRealWidthHeight( w, h, maxWidth, maxHeight ); if( !force && newWidHgt[0] >= w && newWidHgt[1] >= h ) return 0L; return zoom( srcImage, destPath, newWidHgt[0], newWidHgt[1] ); }public static long zoom( Image srcImage, String destPath, int newWidth, int newHeight) throws IOException{ //Buffered BufferedImage img = zoomImage( srcImage, newWidth, newHeight ); return writeImage( img, destPath ); }研究一下吧 用JAVA写的都会失真,生成的图像质量不好,这方面JAVA不在行,缩放都涉及图像运算的,一般这类应用可以用JAVA调用其他的图像处理软件,例如调用ImageMagick 在java web项目中,如何用jquery的ajax调用struts中的action?就像Dwr一样,在js中调用action 学习方向问题,希望各位大侠多给给宝贵意见!感激不尽! 新手问数据库连接 请教一个字符串解析方法的问题 最后遇到的两个问题(iframe,FRAME) fileupload 异常。 恼人的汉字内码转换的问题 (GBK -> UTF-8) 如何在jsp中控制数据库。。。,请教! 如何使这一段文字滚动? JSP页中嵌套JavaScript的问题: 一个在线考试系统,有一个需求:可以满足3000人在同一时刻一起考试.请教大家,像这样的 如何从string中截取字符串
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(600, 500,
BufferedImage.TYPE_INT_RGB);
Random r = new Random();
Graphics g = image.getGraphics();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.fillRect(0, 0, 600, 500);
g.setColor(new Color(0,0,0));
String number = String.valueOf(r.nextInt(99999));
g.drawString(number, 5, 15); OutputStream os = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(image);
public static long zoomMax( Image srcImage, String destPath, int maxWidth, int maxHeight ) throws IOException{
return zoomMax( srcImage, destPath, maxWidth, maxHeight, false );
}/**
* 缩小图片,产生缩略图
* @param srcImage 源图片对象
* @param destPath 放小后放置路径
* @param maxWidth 放小后的最大宽度
* @param maxHeight 放小后的最大高度
* @param force 是否强制转换
* @return 如果成功返回目标文件的大小,否则返回0
*/
public static long zoomMax( Image srcImage, String destPath, int maxWidth, int maxHeight, boolean force ) throws IOException {
if( srcImage== null || destPath == null || maxWidth < 0 || maxHeight < 0 )
return 0L;
int w = srcImage.getWidth(null);
int h = srcImage.getHeight(null);
int[] newWidHgt = getRealWidthHeight( w, h, maxWidth, maxHeight );
if( !force && newWidHgt[0] >= w && newWidHgt[1] >= h ) return 0L;
return zoom( srcImage, destPath, newWidHgt[0], newWidHgt[1] );
}public static long zoom( Image srcImage, String destPath, int newWidth, int newHeight) throws IOException{ //Buffered
BufferedImage img = zoomImage( srcImage, newWidth, newHeight );
return writeImage( img, destPath );
}
研究一下吧