zhyiwww
          用平實的筆,記錄編程路上的點點滴滴………
          posts - 536,comments - 394,trackbacks - 0
          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          -------------------------------------------
          崇尚原創精神,
          文章歡迎轉載,
          請您注明出處,
          在此特別聲明。
          版權所有@zhyiwww
          引用鏈接
          http://www.aygfsteel.com/zhyiwww

          --------------------------------------------

          常用鏈接

          留言簿(33)

          隨筆分類(626)

          朋友的博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 1560345
          • 排名 - 11

          最新評論

          閱讀排行榜

          評論排行榜

          (zhyiwww@163.com? 轉載請注明出處 作者:zhyiwww )
          最近,有兩個朋友在問我關于圖片裁剪的問題,不過以前的代碼找不到了,所以,就把完整的例子又整理了一下。可以實現在一個完整的圖片上,根據需要,定位想要截取的圖片的左上角的坐標和圖片的大小,就可以取得該圖片。
          完整的實現代碼如下:
          package org.zy.app;

          import java.io.File;
          import java.io.InputStream;
          import java.io.IOException;
          import java.util.Iterator;
          import java.awt.Rectangle;
          import java.awt.image.BufferedImage;
          import javax.imageio.ImageReader;
          import javax.imageio.ImageReadParam;
          import javax.imageio.ImageIO;
          import javax.imageio.stream.ImageInputStream;

          /**
          ?* @author zhangyi
          ?* date : 2007-10-18
          ?*/
          public class SplitImage {

          ??? /**
          ??? ?* ????????? ?? ImageReader
          ??? ?*
          ??? ?* @param imgPath
          ??? ?* @throws IOException
          ??? ?*/

          ??? public void readImage() throws IOException {
          ??? ??? // get JPEG image reader iterator
          ??? ??? Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
          ??? ??? System.out.println(readers);
          ??? ???
          ??? ??? // get image reader
          ??? ??? ImageReader reader = (ImageReader) readers.next();
          ??? ??? System.out.println(reader);

          ??? ??? // get original image input stream
          ??? ??? InputStream source = this.getClass().getResourceAsStream("img01.jpg");
          ??? ??? System.out.println("image input source is : " + source);
          ??? ???
          ??? ??? // get ImageInputStream of the image to split
          ??? ??? ImageInputStream iis = ImageIO.createImageInputStream(source);
          ??? ??? reader.setInput(iis, true);
          ??? ???
          ??? ??? // the image param
          ??? ??? ImageReadParam param = reader.getDefaultReadParam();
          ??? ??? int imageIndex = 0;
          //??? ???
          //??? ??? int half_width = reader.getWidth(imageIndex) / 2;
          //??? ??? int half_height = reader.getHeight(imageIndex) / 2;

          ??? ??? // the coordinate and the size on the image that you want to split on
          ??? ??? Rectangle rect = new Rectangle(300, 490, 200, 100);
          ??? ??? param.setSourceRegion(rect);
          ??? ???
          ??? ??? BufferedImage bi = reader.read(0, param);
          ??? ???
          ??? ??? // write the split picture
          ??? ??? ImageIO.write(bi, "jpg", this.initDestFile());
          ??? }

          ??? public File initDestFile() throws IOException {
          ??? ??? File f = new File("c:\\img02.jpg");

          ??? ??? if (f.exists()) {
          ??? ??? ??? f.delete();
          ??? ??? }
          ??? ??? f.createNewFile();
          ??? ??? return f;
          ??? }

          ??? public static void main(String[] args) {
          ??? ??? SplitImage si = new SplitImage();
          ??? ??? try {
          ??? ??? ??? si.readImage();
          ??? ??? } catch (IOException e) {
          ??? ??? ??? System.out.println("exception");
          ??? ??? }
          ??? }
          }

          代碼下載SplitImage
          這只是一個簡單的實現。當然,也可以在servlet端來實現此功能。


          |----------------------------------------------------------------------------------------|
                                     版權聲明  版權所有 @zhyiwww
                      引用請注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2007-10-18 17:54 zhyiwww 閱讀(1377) 評論(4)  編輯  收藏 所屬分類: java basic

          FeedBack:
          # re: 回復兩個朋友的圖片裁剪問題
          2007-10-22 17:17 | 趙軍華
          兄弟 謝謝了啊   回復  更多評論
            
          # re: 回復兩個朋友的圖片裁剪問題
          2007-12-29 15:49 | 七寶
          可以用鼠標選擇裁剪嗎?  回復  更多評論
            
          # re: 回復兩個朋友的圖片裁剪問題
          2007-12-29 15:52 | qibao
          我要做一個類似QQ截圖,但是是在頁面完成截取圖片的。
          大哥能給些思路嗎?
          謝謝了~!  回復  更多評論
            
          # re: 回復兩個朋友的圖片裁剪問題
          2008-08-20 14:26 | 付安平
          @qibao
          好像是用js做前臺,取得他的長寬已經坐標點
          在后臺用java操作  回復  更多評論
            
          主站蜘蛛池模板: 寻甸| 甘孜县| 永嘉县| 博湖县| 和硕县| 太仓市| 吐鲁番市| 安泽县| 来凤县| 特克斯县| 乐昌市| 额敏县| 明星| 宣化县| 泰来县| 新野县| 蒙城县| 夏邑县| 苏尼特右旗| 永善县| 枣庄市| 涟水县| 怀来县| 康马县| 襄城县| 宁阳县| 西青区| 双峰县| 专栏| 永定县| 若尔盖县| 广饶县| 永善县| 五寨县| 定南县| 习水县| 讷河市| 绥化市| 东阳市| 泰和县| 安阳县|