甜咖啡

          我的IT空間

          前臺用javaScript,后臺用java裁剪圖片

          總體思想:

          1.前臺網頁用js得到裁剪圖片的id及x,y,寬度和高度。

          2.服務端根據id取出要裁剪的圖片 。

          3.根據這些參數來生成裁剪的圖像。

          后臺代碼如下:

          package com.wodexiangce;

          import java.awt.Rectangle;

          import java.awt.image.BufferedImage;

          import java.io.File;

          import java.io.FileInputStream;

          import java.io.IOException;

          import java.util.Iterator;

          import javax.imageio.ImageIO;

          import javax.imageio.ImageReadParam;

          import javax.imageio.ImageReader;

          import javax.imageio.stream.ImageInputStream;

          /** *//**

          *

          *

          * @author <a href="mailto:lqinglong@yahoo.cn">qinglong.lu</a>

          *

          * 2008-3-21

          */

          public class OperateImage ...{

                

              //===源圖片路徑名稱如:c:/1.jpg

              private String srcpath ;

                  

              //===剪切圖片存放路徑名稱.如:c:/2.jpg

              private String subpath ;

             

              //===剪切點x坐標

              private int x ;

             

              private int y ;   

               

              //===剪切點寬度

              private int width ;

              

              private int height ;

             

              public OperateImage()...{

                     

              } 

              public OperateImage(int x,int y,int width,int height)...{

                   this.x = x ;

                   this.y = y ;

                   this.width = width ;  

                   this.height = height ;

              }

             

              /** *//**

               * 對圖片裁剪,并把裁剪完蛋新圖片保存 。

               */

              public void cut() throws IOException...{

                  

                  FileInputStream is = null ;

                  ImageInputStream iis =null ;

              

                  try...{  

                      //讀取圖片文件

                      is = new FileInputStream(srcpath);

                     

                      /**//*

                       * 返回包含所有當前已注冊 ImageReader 的 Iterator,這些 ImageReader

                       * 聲稱能夠解碼指定格式。 參數:formatName - 包含非正式格式名稱 .

                       *(例如 "jpeg" 或 "tiff")等 。

                       */

                      Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg"); 

                      ImageReader reader = it.next();

                      //獲取圖片流

                      iis = ImageIO.createImageInputStream(is);

                        

                      /**//*

                       * <p>iis:讀取源.true:只向前搜索 </p>.將它標記為 ‘只向前搜索’。

                       * 此設置意味著包含在輸入源中的圖像將只按順序讀取,可能允許 reader

                       *  避免緩存包含與以前已經讀取的圖像關聯的數據的那些輸入部分。

                       */

                      reader.setInput(iis,true) ;

                     

                      /**//*

                       * <p>描述如何對流進行解碼的類<p>.用于指定如何在輸入時從 Java Image I/O

                       * 框架的上下文中的流轉換一幅圖像或一組圖像。用于特定圖像格式的插件

                       * 將從其 ImageReader 實現的 getDefaultReadParam 方法中返回

                       * ImageReadParam 的實例。 

                       */

                      ImageReadParam param = reader.getDefaultReadParam();

                      

                      /**//*

                       * 圖片裁剪區域。Rectangle 指定了坐標空間中的一個區域,通過 Rectangle 對象

                       * 的左上頂點的坐標(x,y)、寬度和高度可以定義這個區域。

                       */

                      Rectangle rect = new Rectangle(x, y, width, height);

                     

                       

                      //提供一個 BufferedImage,將其用作解碼像素數據的目標。

                      param.setSourceRegion(rect);

                      /**//*

                       * 使用所提供的 ImageReadParam 讀取通過索引 imageIndex 指定的對象,并將

                       * 它作為一個完整的 BufferedImage 返回。

                       */

                      BufferedImage bi = reader.read(0,param);               

               

                      //保存新圖片

                      ImageIO.write(bi, "jpg", new File(subpath));    

                  }

                 

                  finally...{

                      if(is!=null)

                         is.close() ;      

                      if(iis!=null)

                         iis.close(); 

                  }

                 

                  

              

              }

              public int getHeight() ...{

                  return height;

              }

              public void setHeight(int height) ...{

                  this.height = height;

              }

              public String getSrcpath() ...{

                  return srcpath;

              }

              public void setSrcpath(String srcpath) ...{

                  this.srcpath = srcpath;

              }

              public String getSubpath() ...{

                  return subpath;

              }

              public void setSubpath(String subpath) ...{

                  this.subpath = subpath;

              }

              public int getWidth() ...{

                  return width;

              }

              public void setWidth(int width) ...{

                  this.width = width;

              }

              public int getX() ...{

                  return x;

              }

              public void setX(int x) ...{

                  this.x = x;

              }

              public int getY() ...{

                  return y;

              }

              public void setY(int y) ...{

                  this.y = y;

              }

              public static void main(String[] args)throws Exception...{

                 

                  String name = "d:/2005121210161588950.jpg";

                 

                  OperateImage o = new OperateImage(100,100,100,100);

                  o.setSrcpath(name); 

                  o.setSubpath("D:/2.jpg");

                  o.cut() ; 

                   

              }

          }

          posted on 2011-08-02 18:43 甜咖啡 閱讀(729) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2011年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汤阴县| 新沂市| 东兴市| 黄浦区| 朝阳市| 吉木萨尔县| 信阳市| 延吉市| 措勤县| 嫩江县| 黎平县| 如皋市| 吉安县| 银川市| 昌平区| 闽清县| 杨浦区| 大港区| 新沂市| 温泉县| 邢台县| 梁平县| 竹溪县| 晋江市| 东辽县| 肃南| 平利县| 常德市| 盱眙县| 屏山县| 营口市| 边坝县| 民县| 陇川县| 宁国市| 阳朔县| 明水县| 阳新县| 马山县| 宣威市| 靖远县|