001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.image.services.impl;
016    
017    import org.apache.tapestry5.ioc.Resource;
018    import org.chenillekit.image.services.ImageService;
019    
020    import javax.imageio.IIOImage;
021    import javax.imageio.ImageIO;
022    import javax.imageio.ImageWriteParam;
023    import javax.imageio.ImageWriter;
024    import javax.imageio.metadata.IIOMetadata;
025    import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
026    import java.awt.*;
027    import java.awt.image.BufferedImage;
028    import java.io.IOException;
029    import java.io.OutputStream;
030    
031    /**
032     * some image based helpers.
033     *
034     * @version $Id: ImageServiceImpl.java 594 2009-12-05 15:17:26Z mlusetti $
035     */
036    public class ImageServiceImpl implements ImageService
037    {
038        /**
039         * converts an image resource to an AWT image.
040         *
041         * @param imageResource image resource
042         *
043         * @return AWT image
044         */
045        private Image toAwtImage(Resource imageResource)
046        {
047            Image image = Toolkit.getDefaultToolkit().getImage(imageResource.toURL());
048            MediaTracker mediaTracker = new MediaTracker(new Container());
049            mediaTracker.addImage(image, 0);
050    
051            try
052            {
053                mediaTracker.waitForID(0);
054            }
055            catch (InterruptedException e)
056            {
057                throw new RuntimeException(e);
058            }
059    
060            return image;
061        }
062    
063        /**
064         * reduce the quality of an image.
065         *
066         * @param image   the original image
067         * @param quality quality
068         * @param output  data stream of the quality reduced image
069         */
070        public void reduceImageQuality(BufferedImage image, float quality, OutputStream output)
071        {
072            ImageWriter encoder = (ImageWriter) ImageIO.getImageWritersByFormatName("JPEG").next();
073            JPEGImageWriteParam param = new JPEGImageWriteParam(null);
074    
075            quality = Math.max(0, Math.min(quality, 100));
076            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
077            param.setCompressionQuality(quality / 100.0f);
078    
079            encoder.setOutput(output);
080    
081            try
082            {
083                encoder.write((IIOMetadata) null, new IIOImage(image, null, null), param);
084            }
085            catch (IOException e)
086            {
087                throw new RuntimeException(e);
088            }
089        }
090    
091        /**
092         * scale image object to a new size.
093         *
094         * @param imageResource image resource to scale
095         * @param height        scale to height
096         *
097         * @return scaled image
098         */
099        public BufferedImage scaleImage(Resource imageResource, int height)
100        {
101            int thumbWidth;
102            Image image = toAwtImage(imageResource);
103    
104            double imageRatio = (double) image.getWidth(null) / (double) image.getHeight(null);
105            thumbWidth = (int) (height * imageRatio);
106    
107            BufferedImage thumbImage = new BufferedImage(thumbWidth, height, BufferedImage.TYPE_INT_RGB);
108            Graphics2D graphics2D = thumbImage.createGraphics();
109            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
110            graphics2D.drawImage(image, 0, 0, thumbWidth, height, null);
111    
112            return thumbImage;
113        }
114    }