圖片壓縮和縮略圖[JAVA]

          最近工作需要對服務(wù)器上面的文件進(jìn)行壓縮,并且把原來一些縮略圖進(jìn)行壓縮。貼出代碼片斷供大家交流。
          搜略圖實現(xiàn)規(guī)定大小畫布上縮小圖片,方便列表顯示。
          圖片壓縮提供了2種方法。1、可以自定義壓縮比。2、采用ImageIO進(jìn)行默認(rèn)壓縮(更安全一些,不會出現(xiàn)內(nèi)存溢出)。

          ???? private ? void ?doThumbCompress(File?file) {
          ????????
          ????????BufferedImage?bis
          = null ;
          ????????
          ????????BufferedImage?bid
          = null ;
          ????????
          ????????
          try {

          ????????????
          int ?nw = ? 113 ;? // ?定義寬為113
          ???????????? int ?nh = ? 84 ;? // ??定義高為84
          ????????????
          ????????????bis?
          = ?ImageIO.read(file);
          ????????????
          int ?c_w = ?bis.getWidth();
          ????????????
          int ?c_h = ?bis.getHeight();

          ????????????
          if (c_w < 114 ? && ?file.length() < MIN_LENGTH) {????????
          ????????????????bis?
          = ? null ;
          ????????????????
          return ;
          ????????????}

          ????????????
          if (c_w > WIDTH) {
          ????????????????nw??
          = ?WIDTH;
          ????????????????nh?
          = ?c_h? * ?(WIDTH) / c_w;
          ????????????????c_h
          = nh;
          ????????????????c_w
          = nw;
          ????????????}
          ????
          ????????????
          ????????????
          if (c_h > HEIGHT) {
          ????????????????nh?
          = ?HEIGHT;
          ????????????????nw??
          = ?c_w? * ?(HEIGHT) / c_h;?????????????
          ????????????}

          ????????????
          ????????????
          ????????????
          ????????????bid
          = ? new ?BufferedImage(WIDTH,?HEIGHT,BufferedImage.TYPE_INT_RGB);????????????????????????
          ????????????Graphics?g?
          = ?bid.getGraphics();

          ????????????g.setColor(Color.white);

          ????????????g.fillRect(
          0 , 0 ,?WIDTH,?HEIGHT);
          ????????????
          ????????????g.drawImage(bis,(WIDTH
          - nw) / 2 , 0 ,nw,nh, null );
          ????????????
          ????????????
          ????????????
          ????????????
          long ?oldFileSize = file.length();
          ????????????
          ?????????????
          ????????????
          if (isOverlay) {
          ????????????????ImageIO.write(bid,?
          " jpeg " ,?file);
          ????????????}
          ? else {
          ????????????????ImageIO.write(ImageIO.read(file),?
          " jpeg " ,? new ?File(outPath
          ????????????????????????????????
          + File.separator + file.getName()));
          ????????????}

          ????????????
          long ?newFileSize = file.length();
          ????????????log(
          " ok: " + file + " \t " + oldFileSize + " \t " + newFileSize);
          ????????}
          ? catch ?(Exception?e) {
          ????????????log(file
          + " ?error: " + e.getMessage());
          ?????????????
          ????????}

          ????????
          ????????
          ????????bis
          = null ;
          ????????
          ????????bid
          = null ;
          ????}

          ????????

          ???? 壓縮圖片代碼:
          ????/**
          ?????*?可以自定義壓縮比例
          ?????*?
          @param?file
          ?????
          */

          ????
          private?void?doCompressQuality(File?file)??{
          ????????BufferedImage?src?
          =?null;
          ????????FileOutputStream?out?
          =??null;
          ????????JPEGImageEncoder?encoder?
          =?null;
          ????????JPEGEncodeParam??param???
          =?null;
          ????????
          ????????
          try{
          ????????????src
          =?ImageIO.read(file);//or?png.
          ????????????if(isOverlay){
          ????????????????out
          =?new?FileOutputStream(file);
          ????????????}
          else{
          ????????????????out
          =?new?FileOutputStream(outPath+File.separator+file.getName());
          ????????????}

          ????????????encoder
          =?JPEGCodec.createJPEGEncoder(out);
          ????????????param
          =?encoder.getDefaultJPEGEncodeParam(src);????????????
          ????????????param.setQuality(imageQuality,?
          false);
          ????????????encoder.setJPEGEncodeParam(param);????????????
          ????????????encoder.encode(src);
          //近JPEG編碼
          ????????????out.close();
          ????????????
          //ImageIO.write(src,?"jpeg",?new?File(outPath+File.separator+file.getName()));????????????
          ????????????log("over:"+file.getPath());
          ????????}
          ?catch?(IOException?e){
          ????????????log(
          "compress?error:"+file.getPath()+";error:"+e.getMessage());
          ????????}

          ????????out?
          =?null;
          ????????encoder?
          =?null;
          ????????param???
          =?null;
          ????????src?????
          =?null;
          ????}


          ????
          /**
          ?????*?壓縮圖片,默認(rèn)壓縮比例75%
          ?????*?
          @param?file
          ?????
          */

          ????
          private?void?doCompress(File?file){
          ????????File?descFile?
          =?null;
          ????????
          boolean?writeOk=false;
          ????????
          long?fileSize=file.length();

          ????????
          try{
          ????????????
          ????????????
          if(isOverlay){????????????????
          ????????????????descFile?
          =?file;
          ????????????????writeOk?
          =?ImageIO.write(ImageIO.read(file),?"jpeg",?descFile);
          ????????????}
          else{
          ????????????????descFile?
          =?new?File(outPath+File.separator+file.getName());
          ????????????????writeOk?
          =?ImageIO.write(ImageIO.read(file),?"jpeg",?descFile);
          ????????????}
          ????????
          ????????????
          ????????????
          if(writeOk){
          ????????????????log(
          "ok:"+file.getPath()+"\t"+fileSize+"\t"+descFile.length());
          ????????????}
          else{
          ????????????????log(
          "fail:"+file.getPath()+"\t"+fileSize+"\t"+descFile.length());
          ????????????}
          ????????????
          ????????????
          ????????}
          ?catch?(Exception?e){
          ????????????log(
          "compress?error:"+file.getPath()+";descFile="+descFile+";error:"+e.getMessage());
          ????????}

          ????????descFile
          =null;
          ????}

          posted on 2010-01-25 11:30 飛熊 閱讀(601) 評論(0)  編輯  收藏 所屬分類: java

          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 喀喇沁旗| 宜春市| 霞浦县| 苍溪县| 佛学| 南皮县| 盐亭县| 霍城县| 高唐县| 宾阳县| 福贡县| 广德县| 马边| 余干县| 昌邑市| 偃师市| 库尔勒市| 军事| 邵东县| 宜君县| 宁强县| 五河县| 鱼台县| 嵊泗县| 苍梧县| 富阳市| 屯门区| 太湖县| 海丰县| 普格县| 工布江达县| 洛隆县| 买车| 泾源县| 河东区| 玉林市| 临夏市| 汉沽区| 海伦市| 莱西市| 格尔木市|