| 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 | 0 | public class MosaicAnalysePicController extends BSAccountPageController { |
| 38 | |
|
| 39 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 40 | |
|
| 41 | |
private PicManFacade pmf; |
| 42 | |
private DatabaseFacade dbf; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public void setPicMan(PicManFacade pmf) { this.pmf = pmf; } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 0 | public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; } |
| 54 | |
|
| 55 | |
|
| 56 | 0 | private Pic masterPic = null; |
| 57 | 0 | private int masterMid = 0; |
| 58 | 0 | 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 | 0 | String x_ = request.getParameter("x"); |
| 72 | 0 | String y_ = request.getParameter("y"); |
| 73 | 0 | String w_ = request.getParameter("w"); |
| 74 | 0 | String h_ = request.getParameter("h"); |
| 75 | 0 | String mid_ = request.getParameter("mid"); |
| 76 | |
|
| 77 | 0 | int x = Integer.parseInt(x_); |
| 78 | 0 | int y = Integer.parseInt(y_); |
| 79 | 0 | int w = Integer.parseInt(w_); |
| 80 | 0 | int h = Integer.parseInt(h_); |
| 81 | 0 | int mid = Integer.parseInt(mid_); |
| 82 | |
|
| 83 | 0 | if (mid != masterMid || bi == null) { |
| 84 | 0 | Mosaic mosaic = dbf.getMosaic(mid); |
| 85 | 0 | masterMid = mid; |
| 86 | 0 | if (masterPic == null || !mosaic.getMasterPic().equals(masterPic.getName())) { |
| 87 | 0 | masterPic = dbf.getPic(mosaic.getMasterPic()); |
| 88 | 0 | bi = pmf.getPicReader().readPic(masterPic); |
| 89 | |
} |
| 90 | |
} |
| 91 | 0 | if (x + w > bi.getWidth()) |
| 92 | 0 | w = bi.getWidth() - x; |
| 93 | 0 | if (y + h > bi.getHeight()) |
| 94 | 0 | h = bi.getHeight() - y; |
| 95 | 0 | BufferedImage tile = null; |
| 96 | |
try { |
| 97 | 0 | tile = bi.getSubimage(x, y, w, h); |
| 98 | 0 | } catch (Exception e) { |
| 99 | 0 | String s = "Failed to create subimage: x/y = " + x + "/" + y + " w/h = " + w + "/" + h; |
| 100 | 0 | Exception e1 = new Exception(s, e); |
| 101 | 0 | logger.error(e1); |
| 102 | 0 | pmf.addError(e1); |
| 103 | 0 | return(null); |
| 104 | 0 | } |
| 105 | 0 | response.setContentType("image/jpeg"); |
| 106 | 0 | ServletOutputStream sos = response.getOutputStream(); |
| 107 | 0 | ImageOutputStream ios = ImageIO.createImageOutputStream(sos); |
| 108 | 0 | Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); |
| 109 | 0 | ImageWriter writer = (ImageWriter)writers.next(); |
| 110 | 0 | writer.setOutput(ios); |
| 111 | 0 | writer.write(tile); |
| 112 | 0 | return(null); |
| 113 | |
} |
| 114 | |
|
| 115 | |
} |