java图片裁剪

总体思想:

    1.前台网页用js得到裁剪图片的id及x,y,宽度和高度。

    2.服务端根据id取出要裁剪的图片 。

   3.根据这些参数来生成裁剪的图像。 


[代码] java图片裁剪

001package com.wodexiangce;
002
003import java.awt.Rectangle;
004
005import java.awt.image.BufferedImage;
006
007import java.io.File;
008
009import java.io.FileInputStream;
010
011import java.io.IOException;
012
013import java.util.Iterator;
014
015import javax.imageio.ImageIO;
016
017import javax.imageio.ImageReadParam;
018
019import javax.imageio.ImageReader;
020
021import javax.imageio.stream.ImageInputStream;
022
023/**
024 *
025 * 2012-10-25
026 *
027 */
028
029public class OperateImage {
030
031    // ===源图片路径名称如:c:\1.jpg
032
033    private String srcpath;
034
035    // ===剪切图片存放路径名称。如:c:\2.jpg
036
037    private String subpath;
038
039    // ===剪切点x坐标
040
041    private int x;
042
043    private int y;
044
045    // ===剪切点宽度
046
047    private int width;
048
049    private int height;
050
051    public OperateImage() {
052
053    }
054
055    public OperateImage(int x, int y, int width, int height) {
056
057        this.x = x;
058
059        this.y = y;
060
061        this.width = width;
062
063        this.height = height;
064
065    }
066
067    /**
068     *
069     * 对图片裁剪,并把裁剪完的新图片保存 。
070     *
071     */
072
073    public void cut() throws IOException {
074
075        FileInputStream is = null;
076
077        ImageInputStream iis = null;
078
079        try {
080
081            // 读取图片文件
082
083            is = new FileInputStream(srcpath);
084
085            /**
086             *
087             * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader
088             *
089             * 声称能够解码指定格式。 参数:formatName - 包含非正式格式名称 .
090             *
091             * (例如 "jpeg" 或 "tiff")等 。
092             *
093             */
094
095            Iterator<ImageReader> it = ImageIO
096                    .getImageReadersByFormatName(" jpg ");
097
098            ImageReader reader = it.next();
099
100            // 获取图片流
101
102            iis = ImageIO.createImageInputStream(is);
103
104            /**
105             *
106             * <p>
107             * iis:读取源。true:只向前搜索
108             * </p>
109             * .将它标记为 ‘只向前搜索’。
110             *
111             * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader
112             *
113             * 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。
114             *
115             */
116
117            reader.setInput(iis, true);
118
119            /**
120             *
121             * <p>
122             * 描述如何对流进行解码的类
123             * <p>
124             * .用于指定如何在输入时从 Java Image I/O
125             *
126             * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件
127             *
128             * 将从其 ImageReader 实现的 getDefaultReadParam 方法中返回
129             *
130             * ImageReadParam 的实例。
131             *
132             */
133
134            ImageReadParam param = reader.getDefaultReadParam();
135
136            /**
137             *
138             * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象
139             *
140             * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。
141             *
142             */
143
144            Rectangle rect = new Rectangle(x, y, width, height);
145
146            // 提供一个 BufferedImage,将其用作解码像素数据的目标。
147
148            param.setSourceRegion(rect);
149
150            /**
151             *
152             * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将
153             *
154             * 它作为一个完整的 BufferedImage 返回。
155             *
156             */
157
158            BufferedImage bi = reader.read(0, param);
159
160            // 保存新图片
161
162            ImageIO.write(bi, " jpg ", new File(subpath));
163
164        } finally {
165
166            if (is != null)
167
168                is.close();
169
170            if (iis != null)
171
172                iis.close();
173
174        }
175
176    }
177
178    public int getHeight() {
179
180        return height;
181
182    }
183
184    public void setHeight(int height) {
185
186        this.height = height;
187
188    }
189
190    public String getSrcpath() {
191
192        return srcpath;
193
194    }
195
196    public void setSrcpath(String srcpath) {
197
198        this.srcpath = srcpath;
199
200    }
201
202    public String getSubpath() {
203
204        return subpath;
205
206    }
207
208    public void setSubpath(String subpath) {
209
210        this.subpath = subpath;
211
212    }
213
214    public int getWidth() {
215
216        return width;
217
218    }
219
220    public void setWidth(int width) {
221
222        this.width = width;
223
224    }
225
226    public int getX() {
227
228        return x;
229
230    }
231
232    public void setX(int x) {
233
234        this.x = x;
235
236    }
237
238    public int getY() {
239
240        return y;
241
242    }
243
244    public void setY(int y) {
245
246        this.y = y;
247
248    }
249
250    public static void main(String[] args) throws Exception {
251
252        String name = " d:\2005121210161588950.jpg ";
253
254        OperateImage o = new OperateImage(100, 100, 100, 100);
255
256        o.setSrcpath(name);
257
258        o.setSubpath(" D:\2.jpg ");
259
260        o.cut();
261
262    }
263
264}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值