<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 431,  comments - 344,  trackbacks - 0

    如果給出縮略圖的寬度和高度,那么就會根據給出的寬度和高度生產縮略圖,如果只給出寬度或者高度值,那么就會根據比例生成縮略圖。
    package com.founder.common.utils;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;

    //生成等比例高質量縮略圖
    public class ThumbnailUtil {
     private static int width;
     private static int height;
     private static int scaleWidth;
     static double support = (double) 3.0;
     static double PI = (double) 3.14159265358978;
     static double[] contrib;
     static double[] normContrib;
     static double[] tmpContrib;
     static int startContrib, stopContrib;
     static int nDots;
     static int nHalfDots;

     public static void saveImage(String fromFileStr, String saveToFileStr, int formatWidth, int formatHeight) throws Exception {
      File saveFile = new File(saveToFileStr);
      File fromFile = new File(fromFileStr);
      saveImage(fromFile, saveFile, formatWidth, formatHeight);
     }
     
     public static void saveImage(File fromFile  , File saveFile, int formatWidth, int formatHeight) throws Exception {
      BufferedImage srcImage;
      srcImage = javax.imageio.ImageIO.read(fromFile); // construct image
      int imageWidth = srcImage.getWidth(null);
      int imageHeight = srcImage.getHeight(null);
      int changeToWidth = 0;
      int changeToHeight = 0;
      if (formatWidth > 0 && formatHeight > 0) {
       changeToWidth = formatWidth;
       changeToHeight = formatHeight;
      } else {
       if (imageWidth > 0 && imageHeight > 0) {
        if (imageWidth / imageHeight >= formatWidth / formatHeight) {
         if (imageWidth > formatWidth) {
          changeToWidth = formatWidth;
          changeToHeight = (imageHeight * formatWidth) / imageWidth;
         } else {
          changeToWidth = imageWidth;
          changeToHeight = imageHeight;
         }
        } else {
         if (imageHeight > formatHeight) {
          changeToHeight = formatHeight;
          changeToWidth = (imageWidth * formatHeight) / imageHeight;
         } else {
          changeToWidth = imageWidth;
          changeToHeight = imageHeight;
         }
        }
       }
      }
      
      srcImage = imageZoomOut(srcImage, changeToWidth, changeToHeight);
      ImageIO.write(srcImage, "JPEG", saveFile);
     }

     public static BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {
      width = srcBufferImage.getWidth();
      height = srcBufferImage.getHeight();
      scaleWidth = w;

      if (DetermineResultSize(w, h) == 1) {
       return srcBufferImage;
      }
      CalContrib();
      BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);
      BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);
      return pbFinalOut;
     }

     /**
      * 決定圖像尺寸
      */
     private static int DetermineResultSize(int w, int h) {
      double scaleH, scaleV;
      scaleH = (double) w / (double) width;
      scaleV = (double) h / (double) height;
      // 需要判斷一下scaleH,scaleV,不做放大操作
      if (scaleH >= 1.0 && scaleV >= 1.0) {
       return 1;
      }
      return 0;

     }

     private static double Lanczos(int i, int inWidth, int outWidth, double Support) {
      double x;

      x = (double) i * (double) outWidth / (double) inWidth;

      return Math.sin(x * PI) / (x * PI) * Math.sin(x * PI / Support)
        / (x * PI / Support);

     }

     private static void CalContrib() {
      nHalfDots = (int) ((double) width * support / (double) scaleWidth);
      nDots = nHalfDots * 2 + 1;
      try {
       contrib = new double[nDots];
       normContrib = new double[nDots];
       tmpContrib = new double[nDots];
      } catch (Exception e) {
       System.out.println("init contrib,normContrib,tmpContrib" + e);
      }

      int center = nHalfDots;
      contrib[center] = 1.0;

      double weight = 0.0;
      int i = 0;
      for (i = 1; i <= center; i++) {
       contrib[center + i] = Lanczos(i, width, scaleWidth, support);
       weight += contrib[center + i];
      }

      for (i = center - 1; i >= 0; i--) {
       contrib[i] = contrib[center * 2 - i];
      }

      weight = weight * 2 + 1.0;

      for (i = 0; i <= center; i++) {
       normContrib[i] = contrib[i] / weight;
      }

      for (i = center + 1; i < nDots; i++) {
       normContrib[i] = normContrib[center * 2 - i];
      }
     }

     // 處理邊緣
     private static void CalTempContrib(int start, int stop) {
      double weight = 0;

      int i = 0;
      for (i = start; i <= stop; i++) {
       weight += contrib[i];
      }

      for (i = start; i <= stop; i++) {
       tmpContrib[i] = contrib[i] / weight;
      }

     }

     private static int GetRedValue(int rgbValue) {
      int temp = rgbValue & 0x00ff0000;
      return temp >> 16;
     }

     private static int GetGreenValue(int rgbValue) {
      int temp = rgbValue & 0x0000ff00;
      return temp >> 8;
     }

     private static int GetBlueValue(int rgbValue) {
      return rgbValue & 0x000000ff;
     }

     private static int ComRGB(int redValue, int greenValue, int blueValue) {

      return (redValue << 16) + (greenValue << 8) + blueValue;
     }

     // 行水平濾波
     private static int HorizontalFilter(BufferedImage bufImg, int startX, int stopX,
       int start, int stop, int y, double[] pContrib) {
      double valueRed = 0.0;
      double valueGreen = 0.0;
      double valueBlue = 0.0;
      int valueRGB = 0;
      int i, j;

      for (i = startX, j = start; i <= stopX; i++, j++) {
       valueRGB = bufImg.getRGB(i, y);

       valueRed += GetRedValue(valueRGB) * pContrib[j];
       valueGreen += GetGreenValue(valueRGB) * pContrib[j];
       valueBlue += GetBlueValue(valueRGB) * pContrib[j];
      }

      valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),
        Clip((int) valueBlue));
      return valueRGB;

     }

     // 圖片水平濾波
     private static BufferedImage HorizontalFiltering(BufferedImage bufImage, int iOutW) {
      int dwInW = bufImage.getWidth();
      int dwInH = bufImage.getHeight();
      int value = 0;
      BufferedImage pbOut = new BufferedImage(iOutW, dwInH,
        BufferedImage.TYPE_INT_RGB);

      for (int x = 0; x < iOutW; x++) {

       int startX;
       int start;
       int X = (int) (((double) x) * ((double) dwInW) / ((double) iOutW) + 0.5);
       int y = 0;

       startX = X - nHalfDots;
       if (startX < 0) {
        startX = 0;
        start = nHalfDots - X;
       } else {
        start = 0;
       }

       int stop;
       int stopX = X + nHalfDots;
       if (stopX > (dwInW - 1)) {
        stopX = dwInW - 1;
        stop = nHalfDots + (dwInW - 1 - X);
       } else {
        stop = nHalfDots * 2;
       }

       if (start > 0 || stop < nDots - 1) {
        CalTempContrib(start, stop);
        for (y = 0; y < dwInH; y++) {
         value = HorizontalFilter(bufImage, startX, stopX, start,
           stop, y, tmpContrib);
         pbOut.setRGB(x, y, value);
        }
       } else {
        for (y = 0; y < dwInH; y++) {
         value = HorizontalFilter(bufImage, startX, stopX, start,
           stop, y, normContrib);
         pbOut.setRGB(x, y, value);
        }
       }
      }

      return pbOut;

     }

     private static int VerticalFilter(BufferedImage pbInImage, int startY, int stopY,
       int start, int stop, int x, double[] pContrib) {
      double valueRed = 0.0;
      double valueGreen = 0.0;
      double valueBlue = 0.0;
      int valueRGB = 0;
      int i, j;

      for (i = startY, j = start; i <= stopY; i++, j++) {
       valueRGB = pbInImage.getRGB(x, i);

       valueRed += GetRedValue(valueRGB) * pContrib[j];
       valueGreen += GetGreenValue(valueRGB) * pContrib[j];
       valueBlue += GetBlueValue(valueRGB) * pContrib[j];
      }

      valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen), Clip((int) valueBlue));
      
      return valueRGB;

     }

     private static BufferedImage VerticalFiltering(BufferedImage pbImage, int iOutH) {
      int iW = pbImage.getWidth();
      int iH = pbImage.getHeight();
      int value = 0;
      BufferedImage pbOut = new BufferedImage(iW, iOutH,
        BufferedImage.TYPE_INT_RGB);

      for (int y = 0; y < iOutH; y++) {

       int startY;
       int start;
       int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5);

       startY = Y - nHalfDots;
       if (startY < 0) {
        startY = 0;
        start = nHalfDots - Y;
       } else {
        start = 0;
       }

       int stop;
       int stopY = Y + nHalfDots;
       if (stopY > (int) (iH - 1)) {
        stopY = iH - 1;
        stop = nHalfDots + (iH - 1 - Y);
       } else {
        stop = nHalfDots * 2;
       }

       if (start > 0 || stop < nDots - 1) {
        CalTempContrib(start, stop);
        for (int x = 0; x < iW; x++) {
         value = VerticalFilter(pbImage, startY, stopY, start, stop,
           x, tmpContrib);
         pbOut.setRGB(x, y, value);
        }
       } else {
        for (int x = 0; x < iW; x++) {
         value = VerticalFilter(pbImage, startY, stopY, start, stop,
           x, normContrib);
         pbOut.setRGB(x, y, value);
        }
       }

      }

      return pbOut;

     }

     static int Clip(int x) {
      if (x < 0)
       return 0;
      if (x > 255)
       return 255;
      return x;
     }
    }

    posted on 2011-07-26 09:49 周銳 閱讀(979) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲国产成人久久精品软件| 亚洲愉拍一区二区三区| 国产一卡2卡3卡4卡无卡免费视频| 亚洲导航深夜福利| 黄色三级三级免费看| 野花高清在线电影观看免费视频| 亚洲国产精品SSS在线观看AV| 激情婷婷成人亚洲综合| 久久久久亚洲AV无码专区首JN | 亚洲精品视频在线观看视频| 成人久久免费网站| 久久亚洲国产视频| 日本人成在线视频免费播放| 亚洲AV成人精品日韩一区18p| 国产成人免费高清激情明星| a级片免费观看视频| 97无码人妻福利免费公开在线视频 | 国产精品免费综合一区视频| 免费毛片a在线观看67194| 小草在线看片免费人成视久网| 99精品视频免费| 青柠影视在线观看免费| 久久久精品国产亚洲成人满18免费网站 | 爽爽爽爽爽爽爽成人免费观看 | 亚洲精品国产福利一二区| 亚洲精品视频免费观看| 亚洲乱码国产一区三区| 91麻豆精品国产自产在线观看亚洲 | 亚洲av无码专区国产乱码在线观看 | 亚洲中文字幕久久精品无码A | 午夜神器成在线人成在线人免费| 亚洲一区在线免费观看| 亚洲第一网站免费视频| 亚洲日韩精品无码AV海量| CAOPORN国产精品免费视频| 久久久久久影院久久久久免费精品国产小说| 丁香花在线视频观看免费| 成人黄软件网18免费下载成人黄18免费视频 | 久久精品成人免费网站| 亚洲精品无码不卡在线播放| 免费福利资源站在线视频|