现在要实现一种功能
比发上传一张图片,大小有1M,尺寸也很大/
现在想要一个class文件,把上传的图片,自动裁成固定的大小,比如800*600,一百来K这样.
谁开发过这种例子?或看到过.
救助了,

解决方案 »

  1.   

    上传后,设置缩放,java2d的缓冲image有这功能
      

  2.   

    /**
       * 等比例缩放图像, 本方法目前支持三个图片格式jpg, gif, png, 所有生成都被转化为jpg
       *fullname  图片路径
       * path     目标路径(不带文件名)
       *prtfix    生成的文件名的前缀
       *photomaxw         文件宽度范围
       *photomaxh         文件高度范围
       */
      public static String getSmallJpg(String fullname, String path, String prefix,
                                       float photomaxw, float photomaxh) throws
          Exception {
        File filePath = null;
        try {
          File file = new File(fullname); //读入文件
          if (!file.exists()) {
            return null;
          }
          else {
            //上传flash
            if (fullname.endsWith("swf")) {
              String fileName = file.getName();
              String filePiexName = FileUtil.getFilePrefixName(fileName);
              String newPmgPath = path + "/" + prefix + "_" + filePiexName + ".swf";
              FileUtil.copyFile(fullname, newPmgPath);
              File newFile = new File(newPmgPath);
              return newFile.getName();
            }
          }      Image src = javax.imageio.ImageIO.read(file); //构造Image对象
          //  System.out.println("压缩文件名:" + "   " + file.getAbsoluteFile() );
          float wideth = src.getWidth(null); //得到源图宽
          float height = src.getHeight(null); //得到源图长
          System.out.println("class = " + src.getClass());      System.out.println("源文件宽度:" + wideth + "¥"); //测试
          System.out.println("源文件高度:" + height + "¥");      if (wideth > photomaxw) {
            float mw = wideth;
            System.out.println("限制高度:" + photomaxh);
            System.out.println("限制宽度:" + photomaxw);
            wideth = photomaxw;
            height = height * photomaxw / mw;        System.out.println("收缩后的实际宽度:" + wideth + "●"); //测试
            System.out.println("收缩后的实际高度:" + height + "●");        if (height > photomaxh) {
              float mo = height;
              height = photomaxh;
              wideth = wideth * photomaxh / mo;
            }
          }
          else if (wideth < photomaxw || wideth == photomaxw) {
            float mo = height;
            if (height > photomaxh) {
              height = photomaxh;
              wideth = wideth * height / mo;
            }
          }      if (wideth == 0) {
            wideth = src.getWidth(null);
          }      if (height == 0) {
            height = src.getHeight(null);
          }      System.out.println("最终宽度:" + wideth + "●"); //测试
          System.out.println("最终高度:" + height + "●");      if (height == 0 || wideth == 0) {
            return null;
          }      BufferedImage tag = new BufferedImage( (int) wideth, (int) height,
                                                BufferedImage.TYPE_INT_RGB);
          tag.getGraphics().drawImage(src, 0, 0, (int) wideth, (int) height, null); //绘制后的图      String fileName = file.getName();      File newPmgPathDir = new File(path);
          if (!newPmgPathDir.exists() || !newPmgPathDir.isDirectory()) {
            newPmgPathDir.mkdirs();
          }      String filePiexName = FileUtil.getFilePrefixName(fileName);
          String newPmgPath = path + "/" + prefix + "_" + filePiexName + ".jpg";      FileOutputStream out = new FileOutputStream(newPmgPath); //输出到文件流
          System.out.println("getParent:" + file.getParent());
          System.out.println("getAbsoluteFile:" + file.getAbsoluteFile());
          System.out.println("getAbsolutePath:" + file.getAbsolutePath());
          System.out.println("getCanonicalFile:" + file.getCanonicalFile());
          System.out.println("getCanonicalPath:" + file.getCanonicalPath()); //有什么区别????
    ////    System.out.println("生成的文件名为:" + newPmgPath);      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          encoder.encode(tag);
          out.close();      filePath = new File(newPmgPath); //读入文件
          System.out.println("getAbsoluteFile:" + newPmgPath);
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
        return filePath.getName();
      }
      

  3.   

    进mag.qq.com就像
    http://truemag.qq.com/engine/isapi/frame.dll?bc=4226&co=nfrwzk&gp=18&ul=zh-cn 
    这样,速度要像这么快,缓存里保存的不是一张大图片,而是十二张小图片.
    客户的意思是这样哎,真的要苦笑了,我都会开发frame.dll这样的啦,我也去QQ啦
      

  4.   

    zuguanqun啊,谢谢你哦,能不能交个朋友,我QQ3711382
      

  5.   

    其实你说的这个也很实现,不过需要一段时间,找个时间我用vc给你做个dll吧