1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.web;
9
10 import java.awt.image.BufferedImage;
11 import java.io.IOException;
12 import java.util.Iterator;
13
14 import javax.imageio.ImageIO;
15 import javax.imageio.ImageWriter;
16 import javax.imageio.stream.ImageOutputStream;
17 import javax.servlet.ServletException;
18 import javax.servlet.ServletOutputStream;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.springframework.web.servlet.ModelAndView;
25
26 import com.buckosoft.BSAccount.web.BSAccountPageController;
27 import com.buckosoft.PicMan.business.PicManFacade;
28 import com.buckosoft.PicMan.db.DatabaseFacade;
29 import com.buckosoft.PicMan.domain.Mosaic;
30 import com.buckosoft.PicMan.domain.Pic;
31
32
33
34
35
36
37 public class MosaicAnalysePicController extends BSAccountPageController {
38
39 protected final Log logger = LogFactory.getLog(getClass());
40
41 private PicManFacade pmf;
42 private DatabaseFacade dbf;
43
44
45
46
47 public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
48
49
50
51
52
53 public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
54
55
56 private Pic masterPic = null;
57 private int masterMid = 0;
58 private BufferedImage bi = null;
59
60
61
62
63
64
65
66
67
68 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
69 throws ServletException, IOException {
70
71 String x_ = request.getParameter("x");
72 String y_ = request.getParameter("y");
73 String w_ = request.getParameter("w");
74 String h_ = request.getParameter("h");
75 String mid_ = request.getParameter("mid");
76
77 int x = Integer.parseInt(x_);
78 int y = Integer.parseInt(y_);
79 int w = Integer.parseInt(w_);
80 int h = Integer.parseInt(h_);
81 int mid = Integer.parseInt(mid_);
82
83 if (mid != masterMid || bi == null) {
84 Mosaic mosaic = dbf.getMosaic(mid);
85 masterMid = mid;
86 if (masterPic == null || !mosaic.getMasterPic().equals(masterPic.getName())) {
87 masterPic = dbf.getPic(mosaic.getMasterPic());
88 bi = pmf.getPicReader().readPic(masterPic);
89 }
90 }
91 if (x + w > bi.getWidth())
92 w = bi.getWidth() - x;
93 if (y + h > bi.getHeight())
94 h = bi.getHeight() - y;
95 BufferedImage tile = null;
96 try {
97 tile = bi.getSubimage(x, y, w, h);
98 } catch (Exception e) {
99 String s = "Failed to create subimage: x/y = " + x + "/" + y + " w/h = " + w + "/" + h;
100 Exception e1 = new Exception(s, e);
101 logger.error(e1);
102 pmf.addError(e1);
103 return(null);
104 }
105 response.setContentType("image/jpeg");
106 ServletOutputStream sos = response.getOutputStream();
107 ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
108 Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
109 ImageWriter writer = (ImageWriter)writers.next();
110 writer.setOutput(ios);
111 writer.write(tile);
112 return(null);
113 }
114
115 }