征行天下_思己及人

习武之人,避免不了磕磕碰碰~关键要的是:气~气质!

   ::  ::  ::  :: 管理

不多说,直接上代码

前台用到的是jquery插件,叫:imgareaselect,它可以传过来要裁剪图的宽+高+上边距+左边距,有这四个参数就能裁剪一个小图出来。

后台java代码如下:

  1package com.wodexiangce;
  2
  3import java.awt.Rectangle;
  4import java.awt.image.BufferedImage;
  5import java.io.File;
  6import java.io.FileInputStream;
  7import java.io.IOException;
  8import java.util.Iterator;
  9
 10import javax.imageio.ImageIO;
 11import javax.imageio.ImageReadParam;
 12import javax.imageio.ImageReader;
 13import javax.imageio.stream.ImageInputStream;
 14
 15/** *//**
 16
 17*/

 18public class OperateImage {
 19       
 20    //===源图片路径名称如:c:\1.jpg 
 21    private String srcpath ;
 22         
 23    //===剪切图片存放路径名称.如:c:\2.jpg
 24    private String subpath ;
 25    
 26    //===剪切点x坐标
 27    private int x ;
 28    
 29    private int y ;    
 30      
 31    //===剪切点宽度
 32    private int width ;
 33     
 34    private int height ;
 35    
 36    public OperateImage(){
 37            
 38    }
 
 39    public OperateImage(int x,int y,int width,int height){
 40         this.x = x ;
 41         this.y = y ;
 42         this.width = width ;   
 43         this.height = height ;
 44    }

 45    
 46    /** *//** 
 47     * 对图片裁剪,并把裁剪完蛋新图片保存 。
 48     */

 49    public void cut() throws IOException
 50         
 51        FileInputStream is = null ;
 52        ImageInputStream iis =null ;
 53     
 54        try{   
 55            //读取图片文件
 56            is = new FileInputStream(srcpath); 
 57            
 58            /**//*
 59             * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 
 60             * 声称能够解码指定格式。 参数:formatName - 包含非正式格式名称 .
 61             *(例如 "jpeg" 或 "tiff")等 。 
 62             */

 63            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg"); 
 64            ImageReader reader = it.next(); 
 65            //获取图片流 
 66            iis = ImageIO.createImageInputStream(is);
 67               
 68            /**//* 
 69             * <p>iis:读取源.true:只向前搜索 </p>.将它标记为 ‘只向前搜索’。
 70             * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader
 71             * 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。
 72             */

 73            reader.setInput(iis,true) ;
 74            
 75            /**//* 
 76             * <p>描述如何对流进行解码的类<p>.用于指定如何在输入时从 Java Image I/O 
 77             * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件
 78             * 将从其 ImageReader 实现的 getDefaultReadParam 方法中返回 
 79             * ImageReadParam 的实例。 
 80             */

 81            ImageReadParam param = reader.getDefaultReadParam(); 
 82             
 83            /**//*
 84             * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象
 85             * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 
 86             */
 
 87            Rectangle rect = new Rectangle(x, y, width, height); 
 88            
 89              
 90            //提供一个 BufferedImage,将其用作解码像素数据的目标。 
 91            param.setSourceRegion(rect);
 92
 93            /**//*
 94             * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将
 95             * 它作为一个完整的 BufferedImage 返回。
 96             */

 97            BufferedImage bi = reader.read(0,param);                
 98      
 99            //保存新图片 
100            ImageIO.write(bi, "jpg"new File(subpath));     
101        }

102        
103        finally{
104            if(is!=null)
105               is.close() ;       
106            if(iis!=null)
107               iis.close(); 
108        }
 
109        
110         
111     
112    }

113
114    public int getHeight() {
115        return height;
116    }

117
118    public void setHeight(int height) {
119        this.height = height;
120    }

121
122    public String getSrcpath() {
123        return srcpath;
124    }

125
126    public void setSrcpath(String srcpath) {
127        this.srcpath = srcpath;
128    }

129
130    public String getSubpath() {
131        return subpath;
132    }

133
134    public void setSubpath(String subpath) {
135        this.subpath = subpath;
136    }

137
138    public int getWidth() {
139        return width;
140    }

141
142    public void setWidth(int width) {
143        this.width = width;
144    }

145
146    public int getX() {
147        return x;
148    }

149
150    public void setX(int x) {
151        this.x = x;
152    }

153
154    public int getY() {
155        return y;
156    }

157
158    public void setY(int y) {
159        this.y = y;
160    }
 
161    public static void main(String[] args)throws Exception{
162        
163        String name = "d:\2005121210161588950.jpg"
164        
165        OperateImage o = new OperateImage(100,100,100,100);
166        o.setSrcpath(name); 
167        o.setSubpath("D:\2.jpg");
168        o.cut() ; 
169          
170    }

171
172
173}

174
posted on 2010-11-04 10:58 征行天下 阅读(1603) 评论(4)  编辑  收藏 所属分类: JavaJquery

Feedback

# re: java + imgareaselect 裁剪图片 2011-05-13 10:40 sss
截图后怎么有偏差呢?  回复  更多评论
  

# re: java + imgareaselect 裁剪图片 2011-05-27 18:22 zsz
可能你的css写的有问题,显示的有偏差吧。  回复  更多评论
  

# re: java + imgareaselect 裁剪图片[未登录] 2011-06-03 14:49 hello
什么css,不知道不要乱说,明明是图片裁剪后保存到后台时图片已经失真的,变模糊了  回复  更多评论
  

# re: java + imgareaselect 裁剪图片 2011-06-03 14:53 zsz
@hello

请你看好一楼的问题,你知道个屁啊。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: