我在做一个注册的demo,但是现在想实现注册上传头像,并且头像可以剪裁,因为是限定大小的嘛。不知道这个功能怎么实现,,请做过的说说思路呗。

解决方案 »

  1.   

    头像问题?图片操作,使用jmagick进行图片处理。
      

  2.   

    jQuery剪切、裁剪插件(或者原生态JS实现)
      

  3.   

    给你一段代码,调用就OK
    /**
     * 创建缩略图
     * @param  OriFilePath     源图片路径
     * @param  TargetFilePath 生成缩略图路径
     * @param height 缩略图高度
     * @param width 缩略图宽度
     */ public static void createFixedBoundImg(String OriFilePath, String TargetFilePath, int height, int width)
    throws Exception {

    double Ratio = 0.0;
    File f = new File(OriFilePath);
    Image src = ImageIO.read(f);
    int oriWidth = src.getWidth(null);
    int oriHeight = src.getHeight(null);
    int tagWidth, tagHeight;
    if (oriWidth > width || oriHeight > height) {
    if (oriHeight > oriWidth) {
    Ratio = (new Integer(height)).doubleValue() / oriHeight;
    tagHeight = height;
    tagWidth = (int) (oriWidth * Ratio);
    }
    else {
    Ratio = (new Integer(width)).doubleValue() / oriWidth;
    tagHeight = (int) (oriHeight * Ratio);
    tagWidth = width;
    }
    }
    else {
    tagHeight = oriHeight;
    tagWidth = oriWidth;
    }

    BufferedImage target = new BufferedImage(tagWidth, tagHeight, BufferedImage.TYPE_INT_RGB);
    target.getGraphics().drawImage(src, 0, 0, tagWidth, tagHeight, null);
    FileOutputStream out = new FileOutputStream(TargetFilePath);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(target);  
    out.close();
    }
      

  4.   

    5楼的哥们,你直接给我搞个demo吧,我学习学习~~~~谢谢哦[email protected]
    分数会多多的