Re: image service?
I wrote an engine service which we use for dynamic thumbnailing/resizing. It's really great because you simply say the dimensions and it resizes them. Also, we set up short urls for the service so the thumnails are browser cached. If you would like it as a reference you can email me and I'll send it to you. It's tied into our own Image class which stores the images as a byte[] but it should be a good reference for doing the aspect ratio stuff and all that. Let me know if you would like it. On Sun, 2006-06-11 at 00:47 -0400, Henri Dupre wrote: > I seem to recall that quite a while ago someone was working on an image > service. > I would need a service that allows to resize pictures (hopefully cache the > images). Has anyone developped such service? > Thanks, > > Henri. -- Dan Adams Software Engineer Interactive Factory 617.235.5857 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: image service?
This might help you with image resizing. It was built for a very specific purpose: take an input image, downsize it to fit a specified maximum width and output it as a JPEG. It could easily be extended to work with a maximum height as well (typical thumbnail bounding box approach). The useful thing in this code is that it demonstrates how to get good quality resized JPEG's (something people seem to have a lot of trouble with using awt imaging). This is the guts of the "service" with all the image processing code. import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream; import org.apache.tapestry.ApplicationRuntimeException; public class ImageService { public static final float JPEG_COMPRESSION_RATIO = 0.90f; public static void saveConstrainedJpeg(InputStream input, int maxWidth, File outFile) { // Read source file into a BufferedImage. BufferedImage inputImage; try { inputImage = ImageIO.read(input); } catch (IOException ex) { throw new ApplicationRuntimeException("Cannot read image file.", ex); } // Get input image width and height. int imgWidth = inputImage.getWidth(); int imgHeight = inputImage.getHeight(); // Write the original image if it is within the given constraints. if (imgWidth <= maxWidth) { writeJpeg(inputImage, outFile); return; } // Calculate new width and height; double factor = (double) maxWidth / imgWidth; int newWidth = (int) Math.round(factor * imgWidth); int newHeight = (int) Math.round(factor * imgHeight); // Create and save target image. BufferedImage target = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); writeJpeg(resize(inputImage, target), outFile); } private static BufferedImage resize(BufferedImage source, BufferedImage target) { Graphics2D g2 = target.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); double scalex = (double) target.getWidth() / source.getWidth(); double scaley = (double) target.getHeight() / source.getHeight(); AffineTransform at = AffineTransform.getScaleInstance(scalex, scaley); g2.drawRenderedImage(source, at); g2.dispose(); return target; } private static void writeJpeg(BufferedImage image, File outFile) { // Find a jpeg writer. ImageWriter writer = null; Iterator iter = ImageIO.getImageWritersByMIMEType("image/jpeg"); while (iter.hasNext()) { writer = (ImageWriter) iter.next(); } // Prepare the output file. ImageOutputStream ios = null; try { ios = ImageIO.createImageOutputStream(outFile); } catch (IOException ex) { throw new ApplicationRuntimeException("Could not open file for output.", ex); } writer.setOutput(ios); // Set the compression quality. ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(JPEG_COMPRESSION_RATIO); // Write the image. try { writer.write(null, new IIOImage(image, null, null), param); ios.flush(); writer.dispose(); ios.close(); } catch (IOException ex) { throw new ApplicationRuntimeException("Could not write image file.", ex); } } } -Ryan Henri Dupre wrote: I seem to recall that quite a while ago someone was working on an image service. I would need a service that allows to resize pictures (hopefully cache the images). Has anyone developped such service? Thanks, Henri. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: image service?
| That would be *exactly* what I need... Do you have any pointers to | "Stiches"? | Is that a tapestry framework?? -> http://stitches.authsum.org/ Cheers, Andreas - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: image service?
I have only used it for a small number of images, actaully. Also, there's a problem with transferring files larger than 1MB. I have not noticed any lags for ~10 images on a page that is served by this method as compared to putting them on a nearby apache or as static assets. Cheers, PS On 6/11/06, Henri Dupre <[EMAIL PROTECTED]> wrote: Thanks alot Peter! Yes that would definitely save me time. I'm planning to save the images in files and I'd like my service to get dimentions and do some java 2d magic. How well does it perform to store all the images in the DB? On 6/11/06, Peter Svensson <[EMAIL PROTECTED]> wrote: > > And as I said, the main part to change here is the part in getImage where > you get a persisitenceService from some kind of Hibernate manager to > actually read the object referred to in the service HTTP argument. > > Not much to it, really. Please use any or all if you can . > Thanks, > > Henri.
Re: image service?
Thanks alot Peter! Yes that would definitely save me time. I'm planning to save the images in files and I'd like my service to get dimentions and do some java 2d magic. How well does it perform to store all the images in the DB? On 6/11/06, Peter Svensson <[EMAIL PROTECTED]> wrote: And as I said, the main part to change here is the part in getImage where you get a persisitenceService from some kind of Hibernate manager to actually read the object referred to in the service HTTP argument. Not much to it, really. Please use any or all if you can . Thanks, Henri.
Re: image service?
On 6/11/06, spamsucks <[EMAIL PROTECTED]> wrote: Stitches has an image upload service in addition to components to search and pick images. Thumbnails of various sizes are auto generated. That would be *exactly* what I need... Do you have any pointers to "Stiches"? Is that a tapestry framework?? Thanks, Henri.
Re: image service?
Stitches has an image upload service in addition to components to search and pick images. Thumbnails of various sizes are auto generated. Images are cached in the browser and only re-transmitted if they have changed. Same code as in abstractservice. Henri Dupre wrote: I seem to recall that quite a while ago someone was working on an image service. I would need a service that allows to resize pictures (hopefully cache the images). Has anyone developped such service? Thanks, Henri. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: image service?
This is embarrasing. It's really not much, and not very well written. On the other hand I "ported" it fairly easy to another project by changing class names and lookup so I suppose you could do the same. I'm not doing any java2D magic in it though, just passing a byte[] as an image/jpeg to the out stream from the defined object; In my ImageEdit.html I have; And in the coresponding ImageEdit.java I have ; public String getUrl() { Picture sp = (Picture)getModel(); System.out.println("StoredPictureEdit getCurrentUrl pic is "+sp); System.out.println("StoredPictureEdit getCurrentUrl pic id is "+sp); Integer id = sp.getID(); return "/Art/app?service=image&imageid=" + id ; } Where model is the "current object" in trails. You could easily just change this to any getter for the class contatining a byte[] for the pciture you want to show. Not that I have chickened out on the link generation department. Works great, though. Then I define my ImageService in a hivemodule.xml I just put in a META-INF directory directly under my /src directory; The comes the fairly half-and again not really longish ImageService.java; import java.io.IOException; import java.io.OutputStream; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.engine.IEngineService; import org.apache.tapestry.engine.ILink; import org.trails.persistence.PersistenceService; public class ImageService implements IEngineService { private HttpServletResponse response; private String TYPE="jpeg"; private int imgid; public void setResponse(HttpServletResponse response) { this.response = response; } public String getName() { return "image"; } public void service(IRequestCycle cycle) throws IOException { System.out.println("ImageService called"); String id = cycle.getParameter("imageid"); imgid = Integer.parseInt(id); System.out.println("ImageService id == '"+imgid+"'"); Picture sp = getImage(); byte[] raw = sp.getPicdata(); OutputStream os = response.getOutputStream(); os.write(raw); os.close(); } public Picture getImage() { Picture rv = null; List tmp = null; Class arg0 = null; try { arg0 = Class.forName("Picture"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } PersistenceService ps = PictureEdit.getRefrence ().getPersistenceService(); Picture s = (Picture) ps.getInstance(arg0, imgid); System.out.println("ImageService Object from persistence is "+s); rv = s; return rv; } public ILink getLink(boolean post, Object parameter) { return null; } public ILink getLink(IRequestCycle arg0, boolean arg1, Object arg2) { // TODO Auto-generated method stub return null; } } And as I said, the main part to change here is the part in getImage where you get a persisitenceService from some kind of Hibernate manager to actually read the object referred to in the service HTTP argument. Not much to it, really. Please use any or all if you can . Cheers, PS On 6/11/06, Andreas Bulling <[EMAIL PROTECTED]> wrote: On 11. Jun 2006 - 10:11:00, Peter Svensson wrote: | I have a simple image service which is not very well packaged, but depends | on an Image class, and uses the trails frameworks Hibernate DAO to get and | do stuff to images stored as byte[]s in the Image class. Could any of that | be of use? I'd also be interested in it ;-) Andreas - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: image service?
On 11. Jun 2006 - 10:11:00, Peter Svensson wrote: | I have a simple image service which is not very well packaged, but depends | on an Image class, and uses the trails frameworks Hibernate DAO to get and | do stuff to images stored as byte[]s in the Image class. Could any of that | be of use? I'd also be interested in it ;-) Andreas - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: image service?
Henri who is youI think I might know youl...where do you reside? On 6/11/06, Henri Dupre <[EMAIL PROTECTED]> wrote: I seem to recall that quite a while ago someone was working on an image service. I would need a service that allows to resize pictures (hopefully cache the images). Has anyone developped such service? Thanks, Henri.
Re: image service?
I have a simple image service which is not very well packaged, but depends on an Image class, and uses the trails frameworks Hibernate DAO to get and do stuff to images stored as byte[]s in the Image class. Could any of that be of use? I don't think you would need to use trails at all, really, since it's only one line which uses the getPersistenceService() Cheers, PS On 6/11/06, Henri Dupre <[EMAIL PROTECTED]> wrote: I seem to recall that quite a while ago someone was working on an image service. I would need a service that allows to resize pictures (hopefully cache the images). Has anyone developped such service? Thanks, Henri.