首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

何位大神能给一个java的快速高效的图片缩放的功能

2013-12-09 
哪位大神能给一个java的快速高效的图片缩放的功能我自己也写了一个方法,但是速度不是很快,而且批量操作的

哪位大神能给一个java的快速高效的图片缩放的功能
我自己也写了一个方法,但是速度不是很快,而且批量操作的时候耗时太长。求做过的大神们给个提示
public static BufferedImage createThumbnail(BufferedImage image, int size, String type) {
    BufferedImage thumbnail = null;
    if(image == null) {
      System.out.println("文件不存在");
      return thumbnail;
    }
    if(size <= 0) {
      return thumbnail;
    }
    Image srcUpload = null;
    try {
      srcUpload = (Image)image;
      int newWidth = 0, newHight = 0;
      int w = srcUpload.getWidth(null);
      int h = srcUpload.getHeight(null);
      if("width".equals(type)) {
        newWidth = size;
        newHight = (int) (h * size / w);
      }
      else if("hight".equals(type)) {
        newHight = size;
        newWidth = (int) (w * size / h);
      }

      thumbnail = new BufferedImage(newWidth, newHight, BufferedImage.TYPE_3BYTE_BGR);
      thumbnail.getGraphics().drawImage(srcUpload.getScaledInstance(newWidth, newHight, Image.SCALE_SMOOTH), 0, 0, null);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    return thumbnail;
  } 图片缩放?图片?缩放??java图片缩放
[解决办法]
www.jmagick.org/? 这个不错,速度和效果都挺好,缺点是要安装imagemagick.
[解决办法]


    /**
     * Resize an image
     * @param originalImage The image file
     * @param to The destination file
     * @param w The new width (or -1 to proportionally resize)
     * @param h The new height (or -1 to proportionally resize)
     */
    public static void resize(File originalImage, File to, Integer w, Integer h) {
        try {
            BufferedImage source = ImageIO.read(originalImage);
            int owidth = source.getWidth();
            int oheight = source.getHeight();
            double ratio = (double) owidth / oheight;
            if (w < 0 && h < 0) {
                w = owidth;
                h = oheight;
            }
            if (w < 0 && h > 0) {
                w = (int) (h * ratio);
            }
            if (w > 0 && h < 0) {
                h = (int) (w / ratio);
            }


            String mimeType = "image/jpeg";
            if (to.getName().endsWith(".png")) {
                mimeType = "image/png";
            }
            if (to.getName().endsWith(".gif")) {
                mimeType = "image/gif";
            }
            // out
            BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Image srcSized = source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
            Graphics graphics = dest.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, w, h);
            graphics.drawImage(srcSized, 0, 0, null);
            ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next();
            ImageWriteParam params = writer.getDefaultWriteParam();
            FileImageOutputStream toFs = new FileImageOutputStream(to);
            writer.setOutput(toFs);
            IIOImage image = new IIOImage(dest, null, null);
            writer.write(null, image, params);
            toFs.flush();
            toFs.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


热点排行