JAVA切割图片

2019-04-15 15:52发布

1.protected void processRequest(HttpServletRequest request,
2.
3.HttpServletResponse response) throws ServletException, IOException {
4.
5.String Pic = (String) request.getParameter("p");
6.
7.int PointX = Integer.parseInt(request.getParameter("x"));
8.
9.int PointY = Integer.parseInt(request.getParameter("y"));
10.
11.int CutWidth = Integer.parseInt(request.getParameter("w"));
12.
13.int CutHeight = Integer.parseInt(request.getParameter("h"));
14.
15.// 图片宽度
16.
17.int PicWidth = Integer.parseInt(request.getParameter("pw"));
18.
19.// 图片高度
20.
21.int PicHeight = Integer.parseInt(request.getParameter("ph"));
22.
23.response.setContentType("image/jpeg");
24.
25.response.setHeader("Pragma", "No-cache");
26.
27.response.setHeader("Cache-Control", "no-cache");
28.
29.response.setDateHeader("Expires", 0);
30.
31.HttpSession session = request.getSession();
32.
33.String picPath = Pic;
34.
35.String path = request.getRealPath(picPath);
36.
37.ServletOutputStream responseOutputStream = response.getOutputStream();
38.
39.cut(path, responseOutputStream, CutWidth, CutHeight, PointX, PointY,
40.
41.PicWidth, PicHeight);
42.
43.// 以下关闭输入流!
44.
45.responseOutputStream.flush();
46.
47.responseOutputStream.close();
48.
49.}
50.
51./**
52.
53.* 图片切割
54.
55.* @param srcImageFile 图片地址
56.
57.* @param responseOutputStream servlet输出流
58.
59.* @param w 切割宽度
60.
61.* @param h 切割高度
62.
63.* @param x1 开始x结点(left)
64.
65.* @param y1 开始y结点(top)
66.
67.* @param sw 图片宽度
68.
69.* @param sh 图片高度
70.
71.*/
72.
73.public void cut(String srcImageFile,ServletOutputStream responseOutputStream, int w, int h, int x1,
74.
75.int y1, int sw, int sh) {
76.
77.try {
78.
79.// http://localhost:8080/ImpCra/createServlet?p=Sunset.jpg&x=117&y=201&w=61&h=50&pw=300&ph=400
80.
81.Image img;
82.
83.ImageFilter cropFilter;
84.
85.// 读取源图像
86.
87.BufferedImage bi = ImageIO.read(new File(srcImageFile));
88.
89.if (sw >= w && sh >= h) {
90.
91.Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);
92.
93.// 剪切起始坐标点
94.
95.int x = x1;
96.
97.int y = y1;
98.
99.int destWidth = w; // 切片宽度
100.
101.int destHeight = h; // 切片高度
102.
103.// 图片比例
104.
105.double pw = sw;
106.
107.double ph = sh;
108.
109.double m = (double) sw / pw;
110.
111.double n = (double) sh / ph;
112.
113.int wth = (int) (destWidth * m);
114.
115.int hth = (int) (destHeight * n);
116.
117.int xx = (int) (x * m);
118.
119.int yy = (int) (y * n);
120.
121.// 四个参数分别为图像起点坐标和宽高
122.
123.// 即: CropImageFilter(int x,int y,int width,int height)
124.
125.cropFilter = new CropImageFilter(xx, yy, wth, hth);
126.
127.img = Toolkit.getDefaultToolkit().createImage(
128.
129.new FilteredImageSource(image.getSource(), cropFilter));
130.
131.BufferedImage tag = new BufferedImage(w, h,
132.
133.BufferedImage.TYPE_INT_RGB);
134.
135.Graphics g = tag.getGraphics();
136.
137.g.drawImage(img, 0, 0, null); // 绘制缩小后的图
138.
139.g.dispose();
140.
141.// 输出为文件
142.
143.ImageIO.write(tag, "JPEG", responseOutputStream);
144.
145.}
146.
147.} catch (Exception e) {
148.
149.e.printStackTrace();
150.
151.}
152.
153.}
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.JavaScript 图片切割效果(带拖放、缩放效果)
164.
165.
166.
167.
168.
169.
176.
177.
178.
179.
180.
181.
208.
209.
210.
211.
212.
213.

182.
183.

184.
185.

186.
187.

188.
189.

190.
191.

192.
193.

194.
195.

196.
197.

198.
199.

200.
201.

202.
203.

204.
205.

206.
207.

214.
215.
1256.
1257.
1302.
1303.

1304.
1305.

1306.
1307.

1308.
1309.
1310.
1311.
1312.
1313.

1314.
1315.

1316.
1317.

1318.
1319.
1320.
1321.
1322.
1323.

1324.
1325.

1326.
1327.

1328.
1329.
1330.
1331.
1332.
1333.

1334.
1335.

1336.
1337.

1338.
1339.
1340.
1341.
1342.
1343.

1344.
1345.

1346.
1347.

1348.
1349.
1350.
1351.

1352.
1353.
1354.
1355.

1356.
1357.
1380.
1381.
1382.
1383.