请问怎么把图片生成缩略图,例如我想把一个400*400的图片,变成80*80的图片,请问应该怎么写,或说一下用什么知识实现,最好有具体的例子说明一下,帮帮我,多谢大家了

解决方案 »

  1.   

    struts有这个功能 可以去看看
      

  2.   

    这个也可以用比较简单的方法实现,可以把上传的文件保存到服务器的一个临时目录下,然后<img>这个标签是可以设置高度和宽度的,就OK了。
      

  3.   

    我这里有段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);
      

  4.   


    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 );
    }
    研究一下吧
      

  5.   

    用JAVA写的都会失真,生成的图像质量不好,这方面JAVA不在行,缩放都涉及图像运算的,一般这类应用可以用JAVA调用其他的图像处理软件,例如调用ImageMagick