Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package util; import util.WebUtil; import util.GlobalConst; import util.ExpiringObjectPool; import model.Photo; import java.io.BufferedOutputStream; import java.awt.Dimension; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import com.sun.media.jai.codec.SeekableStream; import javax.media.jai.RenderedOp; import javax.media.jai.JAI; /** * * @author Smitha Gudur (smitha@redbasinnetworks.com) * @version $Revision: 1.1 $ */ public class NewStreamBlobUtil { /** Logger for this class and subclasses */ protected static final Log logger = LogFactory.getLog("util.NewStreamBlobUtil"); protected ExpiringObjectPool eop; private ImgUtil imageHelper = null; private static final String JAI_STREAM_ACTION = "stream"; /** * Returns scaled blob * @param b byte stream * @param type that is "jpeg", "gif", "png" * @param width width of the image * @param height height of the image * @param scale boolean yes or no * @return scaled byte stream */ public synchronized byte[] streamBlob(byte[] b, int width, int height, boolean doIScale) throws BasicObjException { if (b == null) { return null; } try { initializeImageInfo(); ImgMetadata scaled = null; if (doIScale) { // scale the image InputStream imageInputStream = new ByteArrayInputStream(b); // read in the original image from an input stream SeekableStream seekableImageStream = SeekableStream.wrapInputStream(imageInputStream, true); RenderedOp originalImage = JAI.create(JAI_STREAM_ACTION, seekableImageStream); //RenderedOp originalImage = JAI.create("stream", seekableImageStream, true); Dimension d = new Dimension(originalImage.getWidth(), originalImage.getHeight()); d = WebUtil.getDimension(d, width, height); ImgMetadata data = imageHelper.makeImg(b); scaled = imageHelper.scaleImg(data, (int) d.getWidth(), (int) d.getHeight()); } else { // don't scale the image ImgMetadata data = imageHelper.makeImg(b); scaled = imageHelper.scaleImg(data, width, height); } if (scaled != null) { ByteArrayOutputStream os = new ByteArrayOutputStream(); int quality = 75; String type = "jpeg"; // any output encoding scaled.writeTo(os, type, quality); //calls JAI.create("encode", result, fos, type, null); return os.toByteArray(); } } catch (Exception e) { throw new BasicObjException("Error writing image stream ", e); } return null; } public HttpServletResponse setHeader(HttpServletResponse response, Photo photo, String btitle) { if (photo == null || response == null) { return response; } int maxAge = 2592000; response.setContentType(photo.getValue(DbConstants.MIMETYPE)); response.setHeader("Content-Disposition", "attachment; filename=" + btitle); response.setHeader("Cache-Control", "max-age=" + maxAge); response.setHeader("Accept-Ranges", "bytes"); response.setHeader("Connection", "Keep-Alive"); response.setHeader("Keep-Alive", "timeout=15, max=100"); return response; } public Photo setPhoto(Photo photo, byte[] blob) { if (photo == null || blob == null) { return null; } Photo sPhoto = (Photo) eop.newObject(DbConstants.PHOTO); sPhoto.setValue(DbConstants.MIMETYPE, photo.getValue(DbConstants.MIMETYPE)); sPhoto.setValue(DbConstants.BTITLE, photo.getValue(DbConstants.BTITLE)); sPhoto.setBlob(blob); return sPhoto; } public synchronized void initializeImageInfo() throws BasicObjException { System.setProperty("com.sun.media.jai.disableMediaLib", "true"); try { if (imageHelper == null) { imageHelper = new ImgJaiTool(); } } catch (Exception e) { throw new BasicObjException("Error creating new ImageHelper", e); } } /** * This method is called by spring. * @param eop */ public void setEop(ExpiringObjectPool eop) { this.eop = eop; } }