You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2009/10/20 00:50:50 UTC

svn commit: r826855 - in /labs/magma/trunk/beans-image: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/magma/ src/main/java/org/apache/magma/bean/ src/main/java/org/apache/magma/bean/image/ src/ma...

Author: simoneg
Date: Mon Oct 19 22:50:49 2009
New Revision: 826855

URL: http://svn.apache.org/viewvc?rev=826855&view=rev
Log:
Basic image support

Added:
    labs/magma/trunk/beans-image/pom.xml
    labs/magma/trunk/beans-image/src/
    labs/magma/trunk/beans-image/src/main/
    labs/magma/trunk/beans-image/src/main/java/
    labs/magma/trunk/beans-image/src/main/java/org/
    labs/magma/trunk/beans-image/src/main/java/org/apache/
    labs/magma/trunk/beans-image/src/main/java/org/apache/magma/
    labs/magma/trunk/beans-image/src/main/java/org/apache/magma/bean/
    labs/magma/trunk/beans-image/src/main/java/org/apache/magma/bean/image/
    labs/magma/trunk/beans-image/src/main/java/org/apache/magma/bean/image/ThumbnailMaker.java
    labs/magma/trunk/beans-image/src/main/resources/
    labs/magma/trunk/beans-image/src/test/
    labs/magma/trunk/beans-image/src/test/java/
    labs/magma/trunk/beans-image/src/test/resources/

Added: labs/magma/trunk/beans-image/pom.xml
URL: http://svn.apache.org/viewvc/labs/magma/trunk/beans-image/pom.xml?rev=826855&view=auto
==============================================================================
--- labs/magma/trunk/beans-image/pom.xml (added)
+++ labs/magma/trunk/beans-image/pom.xml Mon Oct 19 22:50:49 2009
@@ -0,0 +1,20 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <artifactId>magma-parent</artifactId>
+    <groupId>org.apache.magma</groupId>
+    <version>2</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.magma</groupId>
+  <artifactId>beans-image</artifactId>
+  <packaging>magma</packaging>
+  <version>0.0.2-SNAPSHOT</version>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.magma</groupId>
+      <artifactId>foundation-beans</artifactId>
+      <version>0.0.2-SNAPSHOT</version>
+    </dependency>
+  </dependencies>
+  
+</project>

Added: labs/magma/trunk/beans-image/src/main/java/org/apache/magma/bean/image/ThumbnailMaker.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/beans-image/src/main/java/org/apache/magma/bean/image/ThumbnailMaker.java?rev=826855&view=auto
==============================================================================
--- labs/magma/trunk/beans-image/src/main/java/org/apache/magma/bean/image/ThumbnailMaker.java (added)
+++ labs/magma/trunk/beans-image/src/main/java/org/apache/magma/bean/image/ThumbnailMaker.java Mon Oct 19 22:50:49 2009
@@ -0,0 +1,106 @@
+package org.apache.magma.bean.image;
+
+import java.awt.Image;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.imageio.ImageIO;
+
+/**
+ * Utility class for creating scaled down versions of images.
+ *
+ * @author Simone Gianni <si...@apache.org>
+ */
+public class ThumbnailMaker {
+	
+	private int maxw = 80;
+	private int maxh = 80;
+	private String format = "jpg";
+	
+	/**
+	 * Constructs using a string defined format.
+	 * 
+	 * The format is :
+	 * format widthxheight
+	 * 
+	 * So for example "jpg 80x80"
+	 * 
+	 * @param def The format
+	 */
+	public ThumbnailMaker(String def) {
+		def = def.trim();
+		if (def.indexOf(' ') > 0) {
+			this.format = def.substring(0, def.indexOf(' '));
+			def = def.substring(def.indexOf(' ') + 1);
+		}
+		String[] split = def.split("x");
+		this.maxw = Integer.parseInt(split[0]);
+		this.maxh = Integer.parseInt(split[1]);
+	}
+	
+	/**
+	 * Constructs using specific target size and format.
+	 * 
+	 * @param maxw Maximum width
+	 * @param maxh Maximum height
+	 * @param format Scaled down image format, one of "jpg", "gif" or other java supported output types.
+	 */
+	public ThumbnailMaker(int maxw, int maxh, String format) {
+		this.maxw = maxw;
+		this.maxh = maxh;
+		this.format = format;
+	}
+
+	/**
+	 * Constructs using specific target size, using jpg format.
+	 * 
+	 * @param maxw Maximum width
+	 * @param maxh Maximum height
+	 */
+	public ThumbnailMaker(int maxw, int maxh) {
+		this(maxw, maxh, "jpg");
+	}
+
+	/**
+	 * Scales an image to the required size and format.
+	 * 
+	 * Scaling keeps the size ratio of the original image,
+	 * scaling it so that it fits in a rectangle or required dimensions. 
+	 * 
+	 * @param image input stream of the original image
+	 * @return input stream from which to read the scaled image
+	 * @throws IOException
+	 */
+	public InputStream scaleImage(InputStream image) throws IOException {
+		BufferedImage origimg = ImageIO.read(image);
+		int prew = origimg.getWidth();
+		int preh = origimg.getHeight();
+		// scale factor based on width
+		double scalefw = ((double)maxw / (double)prew);
+		// scale factor based on height
+		double scalefh = ((double)maxh / (double)preh);
+		int tgw = -1;
+		int tgh = -1;
+		if (prew > maxw || preh > maxh) {
+			if (scalefw < scalefh) {
+				// we need to scale on width
+				tgw = maxw;
+			} else {
+				// we need to scale on height
+				tgh = maxh;
+			}
+		} else {
+			System.out.println("Not scaling image " + prew + "x" + preh);
+		}
+		Image scaled = origimg.getScaledInstance(tgw, tgh, Image.SCALE_SMOOTH);
+		BufferedImage scaledbi = new BufferedImage(scaled.getWidth(null), scaled.getHeight(null),BufferedImage.TYPE_INT_RGB);
+		scaledbi.getGraphics().drawImage(scaled, 0, 0, null);
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		ImageIO.write(scaledbi, format, baos);
+		return new ByteArrayInputStream(baos.toByteArray());
+	}
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org