1 /*** 2 * Copyright (C) 2009 TM-Search Community. 3 * 4 * This file is part of TM-Search Services. 5 * 6 * Foobar is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Foobar is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Foobar. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package eu.europa.tmsearch.services.business.util; 21 22 import java.awt.AlphaComposite; 23 import java.awt.Graphics2D; 24 import java.awt.Image; 25 import java.awt.image.BufferedImage; 26 import java.io.ByteArrayInputStream; 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 30 import javax.imageio.ImageIO; 31 32 import org.apache.log4j.Logger; 33 34 import eu.europa.tmsearch.services.business.exception.BusinessServiceException; 35 import eu.europa.tmsearch.services.model.TradeMarkImage; 36 37 /*** 38 * An ImageProcessor implementation using standard Java imaging for manipulating 39 * images. 40 * 41 * 42 */ 43 public class AWTImageProcessorImpl implements ImageProcessor { 44 private final Logger log = Logger.getLogger(getClass()); 45 46 /*** 47 * Generates a thumbnail trade mark image from a master's TradeMark image. 48 * 49 * @param tradeMarkImage 50 * Master trademark image 51 * @param height 52 * Desired height. If 0 thumbnail height is calculated to 53 * preserve master image's aspect ratio. 54 * @param width 55 * Desired width 56 * @return A TradeMarkImage containing the thumbnail 57 */ 58 public TradeMarkImage getThumbnailImage(TradeMarkImage tradeMarkImage, int height, int width) { 59 TradeMarkImage tradeMarkThumbnailImage = new TradeMarkImage(); 60 try { 61 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tradeMarkImage 62 .getImageData()); 63 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 64 BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream); 65 height = (height > 0) ? height : (width * bufferedImage.getHeight() / bufferedImage.getWidth()); 66 ImageIO.write(createResizedCopy(bufferedImage, width, height), "jpeg", byteArrayOutputStream); 67 tradeMarkThumbnailImage.setImageData(byteArrayOutputStream.toByteArray()); 68 byteArrayInputStream.close(); 69 byteArrayOutputStream.close(); 70 } catch (IOException e) { 71 log.error("Caught IOException while converting image", e); 72 throw new BusinessServiceException(); 73 } 74 return tradeMarkThumbnailImage; 75 76 } 77 78 BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight) { 79 BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB); 80 Graphics2D g = scaledBI.createGraphics(); 81 g.setComposite(AlphaComposite.Src); 82 g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); 83 g.dispose(); 84 return scaledBI; 85 } 86 87 public TradeMarkImage getImage(TradeMarkImage tradeMarkImage) { 88 throw new UnsupportedOperationException(); 89 } 90 91 }