| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.web; |
| 9 | |
|
| 10 | |
import java.awt.Dimension; |
| 11 | |
import java.awt.Graphics2D; |
| 12 | |
import java.awt.Image; |
| 13 | |
import java.awt.image.BufferedImage; |
| 14 | |
import java.io.IOException; |
| 15 | |
import java.io.PrintWriter; |
| 16 | |
import java.util.Iterator; |
| 17 | |
|
| 18 | |
import javax.imageio.ImageIO; |
| 19 | |
import javax.imageio.ImageWriter; |
| 20 | |
import javax.imageio.stream.ImageOutputStream; |
| 21 | |
import javax.servlet.ServletException; |
| 22 | |
import javax.servlet.ServletOutputStream; |
| 23 | |
import javax.servlet.http.HttpServletRequest; |
| 24 | |
import javax.servlet.http.HttpServletResponse; |
| 25 | |
|
| 26 | |
import org.apache.commons.logging.Log; |
| 27 | |
import org.apache.commons.logging.LogFactory; |
| 28 | |
import org.springframework.web.servlet.ModelAndView; |
| 29 | |
|
| 30 | |
import com.buckosoft.BSAccount.web.BSAccountPageController; |
| 31 | |
import com.buckosoft.BSAccount.web.BSAccountUserWebSession; |
| 32 | |
import com.buckosoft.PicMan.business.PicManFacade; |
| 33 | |
import com.buckosoft.PicMan.db.DatabaseFacade; |
| 34 | |
import com.buckosoft.PicMan.domain.Mosaic; |
| 35 | |
import com.buckosoft.PicMan.domain.Pic; |
| 36 | |
import com.buckosoft.PicMan.domain.Thumbnail; |
| 37 | |
import com.buckosoft.PicMan.domain.User; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public class GetThumbNailController extends BSAccountPageController { |
| 46 | |
|
| 47 | 0 | private static boolean DEBUG = false; |
| 48 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 49 | |
|
| 50 | |
private DatabaseFacade dbf; |
| 51 | |
private PicManFacade pmf; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 0 | public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 0 | public void setPicMan(PicManFacade pmf) { this.pmf = pmf; } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public void setDEBUG(boolean debugFlag) { |
| 68 | 0 | DEBUG = debugFlag; |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) |
| 77 | |
throws ServletException, IOException { |
| 78 | |
String s; |
| 79 | 0 | Pic pic = null; |
| 80 | 0 | String label = null; |
| 81 | |
|
| 82 | 0 | Thumbnail tn = null; |
| 83 | |
|
| 84 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 85 | |
User user; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | 0 | user = (User)userWebSession.getUser(); |
| 93 | 0 | s = request.getParameter("label"); |
| 94 | 0 | if (s != null) { |
| 95 | 0 | label = s; |
| 96 | |
} |
| 97 | |
|
| 98 | 0 | s = request.getParameter("moswork"); |
| 99 | 0 | if (s != null) { |
| 100 | 0 | outMosaicPic(request, response); |
| 101 | 0 | return(null); |
| 102 | |
} |
| 103 | |
|
| 104 | 0 | s = request.getParameter("posterMid"); |
| 105 | 0 | if (s != null) { |
| 106 | 0 | outPosterMid(request, response); |
| 107 | 0 | return(null); |
| 108 | |
} |
| 109 | |
|
| 110 | 0 | s = request.getParameter("importPic"); |
| 111 | 0 | if (s != null) { |
| 112 | 0 | pic = new Pic(true, s); |
| 113 | |
} |
| 114 | |
|
| 115 | 0 | if (pic == null) { |
| 116 | 0 | s = request.getParameter("pic"); |
| 117 | 0 | if (s == null) { |
| 118 | 0 | PrintWriter out = response.getWriter(); |
| 119 | 0 | out.print("No pic in request"); |
| 120 | 0 | return(null); |
| 121 | |
} |
| 122 | |
} |
| 123 | 0 | if (pic == null) |
| 124 | 0 | pic = dbf.getPic(s); |
| 125 | 0 | if (DEBUG) { |
| 126 | 0 | if (pic != null) |
| 127 | 0 | logger.info("Getting thumb '" + pic.getName() + "'"); |
| 128 | |
else |
| 129 | 0 | logger.info("Getting Xthumb for '" + s + "'"); |
| 130 | |
} |
| 131 | 0 | int height = dbf.getSystem().getThumbHeight(); |
| 132 | 0 | s = request.getParameter("mosthumb"); |
| 133 | 0 | if (s != null) |
| 134 | 0 | height = dbf.getSystem().getMosaicThumbHeight(); |
| 135 | 0 | s = request.getParameter("home"); |
| 136 | 0 | if (s != null) { |
| 137 | 0 | int width = pic.getWidth(); |
| 138 | 0 | if (width == 0) { |
| 139 | 0 | Dimension d = pmf.determinePicSize(pic); |
| 140 | 0 | if (d.height == -1) |
| 141 | 0 | width = height; |
| 142 | |
else { |
| 143 | 0 | logger.warn("Updating size for pic " + pic.getName()); |
| 144 | 0 | pic.setHeight(d.height); |
| 145 | 0 | pic.setWidth(d.width); |
| 146 | 0 | pmf.getDB().updatePic(pic); |
| 147 | |
} |
| 148 | 0 | } else { |
| 149 | 0 | height = user.getHomePageThumbWidth() * pic.getHeight() / width; |
| 150 | |
} |
| 151 | |
} |
| 152 | 0 | s = request.getParameter("mos"); |
| 153 | 0 | if (s != null) |
| 154 | 0 | tn = pmf.getTestMosaicThumbNail(pic, height, Integer.parseInt(s)); |
| 155 | |
else |
| 156 | 0 | tn = pmf.getThumbNail(pic, height, label); |
| 157 | 0 | response.setContentType("image/jpeg"); |
| 158 | 0 | ServletOutputStream sos = response.getOutputStream(); |
| 159 | 0 | ImageOutputStream ios = ImageIO.createImageOutputStream(sos); |
| 160 | 0 | Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); |
| 161 | 0 | ImageWriter writer = (ImageWriter)writers.next(); |
| 162 | 0 | writer.setOutput(ios); |
| 163 | 0 | writer.write(tn.getImage()); |
| 164 | 0 | return(null); |
| 165 | |
} |
| 166 | |
|
| 167 | |
private void outMosaicPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 168 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 169 | 0 | User user = (User)userWebSession.getUser(); |
| 170 | 0 | response.setContentType("image/jpeg"); |
| 171 | 0 | response.addHeader("Cache-Control", "max-age=0"); |
| 172 | 0 | response.addHeader("Cache-Control", "no-cache"); |
| 173 | 0 | response.addHeader("expires", "0"); |
| 174 | 0 | response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT"); |
| 175 | 0 | response.addHeader("Pragma", "no-cache"); |
| 176 | 0 | ServletOutputStream sos = response.getOutputStream(); |
| 177 | 0 | ImageOutputStream ios = ImageIO.createImageOutputStream(sos); |
| 178 | 0 | Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); |
| 179 | 0 | ImageWriter writer = (ImageWriter)writers.next(); |
| 180 | 0 | writer.setOutput(ios); |
| 181 | |
BufferedImage small; |
| 182 | 0 | if (pmf.getMosaicMan() == null || pmf.getMosaicMan().getMosaicEngine() == null) { |
| 183 | 0 | small = pmf.getPicReader().getXThumb(dbf.getSystem().getThumbHeight()).getImage(); |
| 184 | |
} else { |
| 185 | |
BufferedImage bi; |
| 186 | 0 | bi = pmf.getMosaicMan().getMosaicEngine().getImage(); |
| 187 | 0 | int width = bi.getWidth(); |
| 188 | 0 | int height = bi.getHeight(); |
| 189 | 0 | int picHeight = user.getMosaicPreviewHeight(); |
| 190 | |
|
| 191 | 0 | double dW = ((double)picHeight/(double)height) * width; |
| 192 | 0 | int newW = (int)dW; |
| 193 | 0 | int newH = picHeight; |
| 194 | |
|
| 195 | 0 | small = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR); |
| 196 | |
Graphics2D g; |
| 197 | 0 | g = small.createGraphics(); |
| 198 | 0 | g.drawImage(bi.getScaledInstance(-1, newH, Image.SCALE_SMOOTH), null, null); |
| 199 | |
} |
| 200 | 0 | writer.write(small); |
| 201 | 0 | } |
| 202 | |
|
| 203 | |
private void outPosterMid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 204 | |
|
| 205 | |
|
| 206 | 0 | response.setContentType("image/jpeg"); |
| 207 | 0 | response.addHeader("Cache-Control", "max-age=0"); |
| 208 | 0 | response.addHeader("Cache-Control", "no-cache"); |
| 209 | 0 | response.addHeader("expires", "0"); |
| 210 | 0 | response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT"); |
| 211 | 0 | response.addHeader("Pragma", "no-cache"); |
| 212 | 0 | ServletOutputStream sos = response.getOutputStream(); |
| 213 | 0 | ImageOutputStream ios = ImageIO.createImageOutputStream(sos); |
| 214 | 0 | Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); |
| 215 | 0 | ImageWriter writer = (ImageWriter)writers.next(); |
| 216 | 0 | writer.setOutput(ios); |
| 217 | |
BufferedImage small; |
| 218 | 0 | String s = request.getParameter("posterMid"); |
| 219 | 0 | Integer mid = Integer.parseInt(s); |
| 220 | 0 | Mosaic mosaic = pmf.getDB().getMosaic(mid); |
| 221 | 0 | String fname = mosaic.getOutPic(); |
| 222 | 0 | BufferedImage bi = pmf.getPicReader().readPic(fname); |
| 223 | 0 | if (bi == null) { |
| 224 | 0 | small = pmf.getPicReader().getXThumb(dbf.getSystem().getThumbHeight()).getImage(); |
| 225 | |
} else { |
| 226 | 0 | int width = bi.getWidth(); |
| 227 | 0 | int height = bi.getHeight(); |
| 228 | 0 | int picHeight = pmf.getDB().getThumbHeight(); |
| 229 | |
|
| 230 | 0 | double dW = ((double)picHeight/(double)height) * width; |
| 231 | 0 | int newW = (int)dW; |
| 232 | 0 | int newH = picHeight; |
| 233 | |
|
| 234 | 0 | small = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR); |
| 235 | |
Graphics2D g; |
| 236 | 0 | g = small.createGraphics(); |
| 237 | 0 | g.drawImage(bi.getScaledInstance(-1, newH, Image.SCALE_SMOOTH), null, null); |
| 238 | |
} |
| 239 | 0 | writer.write(small); |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
} |