Java - 縮圖

摘要:Java - 縮圖

參考這個頁面程式

http://togetherjava.blogspot.tw/2012/08/java-image_30.html

 

我取其中的我需要的函式下來

並且include兩個必要的jar

https://code.google.com/p/java-image-scaling/downloads/detail?name=java-image-scaling-0.8.5.jar

http://www.jhlabs.com/ip/filters/download.html

 

取得image buffer資料

File imgFile = new File(imgFilePath);

BufferedImage image =  ImageIO.read(imgFile );

放入我複製過來的函式裡,就可以做好縮圖效果

resizeAndSave(image,newWidth(),newHeight(),savePath)

 


public static boolean resizeAndSave(BufferedImage source, int width, int height, String toFileName) throws IOException {

        if (!toFileName.toLowerCase().matches("(.*\\.jpe?g)")) {
            return false;
        }

        ResampleOp resampleOp = new ResampleOp(width, height);
        resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal);
        BufferedImage dest = resampleOp.filter(source, null);

        // 檢查圖檔儲存的資料夾是否已存在, 不存在則建立資料夾
        String path = toFileName;

        File containerDir = new File(path);
        if (!containerDir.getParentFile().isDirectory()) {
            containerDir.getParentFile().mkdirs();
        }

        if (source.getType() == BufferedImage.TYPE_CUSTOM) { // png
            String format = "png";
            ImageIO.write(dest, format, new File(toFileName));
        } else if (source.getType() == BufferedImage.TYPE_BYTE_INDEXED) { // gif
            String format = "png";
            ImageIO.write(dest, format, new File(toFileName));
        } else {
            ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
            ImageOutputStream ios = ImageIO.createImageOutputStream(new File(toFileName));
            writer.setOutput(ios);
            ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
            iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwparam.setCompressionQuality(0.95F);
            try {
                writer.write(null, new IIOImage(dest, null, null), iwparam);
                ios.flush();
                writer.abort();
                writer.dispose();
            } finally {
                if (ios != null) {
                    ios.close();
                }
                if (writer != null) {
                }
            }
        }
        return true;
    }

 

以及縮圖比例演算法,ImgSize是我定義的 Class,只有width嶼height屬性及縮圖比例演算法方法,其中縮圖的width必須大於零,height則是可以為零


public class ImgSize {
    private int width = 0  ;
    private int height = 0 ;

    public int getWidth()
    {
        return width;
    }

    public void setWidth(int width)
    {
        this.width = width;
    }

    public int getHeight()
    {
        return height;
    }

    public void setHeight(int height)
    {
        this.height = height;
    }

    //調整縮圖比例
    public static ImgSize GetResizeRatio(int width,int height,int nowWidth,int nowHeight)
    {
        ImgSize newSize = new ImgSize();
        int tempWidth = 0 ;
        int tempHeight = 0 ;
        int newWidth = 0 ;
        int newHeight = 0 ;
        //例外處理   //不處理
        if(width <=0 || nowWidth ==0 || nowHeight == 0 || (width <=0 && height<=0))
            ;
        else
        {
            if(width >0 && height >0 && nowWidth >0 && nowHeight >0)
            {
                 //現在寬度大於縮圖寬度,依寬度比例縮放,否則,依原圖顯示
                 if(nowWidth >  width)
                 {
                      //
                      tempWidth = nowWidth*width/nowWidth;
                      tempHeight = nowHeight*width/nowWidth;
                 }
                 else
                 {
                      tempWidth = nowWidth;
                      tempHeight = nowHeight;
                 }

                 //暫存高度大於現在高度,依高度比例縮放,否則,依原圖顯示
                 if(tempHeight >  height)
                 {
                      //依寬度比例縮放
                      newHeight = tempHeight*height/tempHeight;
                      newWidth = tempWidth*height/tempHeight;
                 }
                 else
                 {
                      newWidth = tempWidth;
                      newHeight = tempHeight;
                 }
            }
            else
            {
                if(width>0 && nowWidth>0)
                {
                      if(nowWidth>width)
                      {
                          newWidth = nowWidth*width/nowWidth;
                          newHeight = nowHeight*width/nowWidth;
                      }
                      else
                      {
                          newWidth = nowWidth;
                          newHeight = nowHeight;
                      }
                }
                else
                {
                      if(nowHeight>height)
                      {
                          newWidth = nowWidth*height/nowHeight;
                          newHeight = nowHeight*height/nowHeight;
                      }
                      else
                      {
                          newWidth = nowWidth;
                          newHeight = nowWidth;
                      }
                }
            }
        }

        newSize.setWidth(newWidth);
        newSize.setHeight(newHeight);

        return newSize;
    }
}