Author: andrei_exadel
Date: 2009-01-27 14:27:36 -0500 (Tue, 27 Jan 2009)
New Revision: 12445
Added:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java
Log:
Image resize
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java
===================================================================
---
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java 2009-01-27
18:40:17 UTC (rev 12444)
+++
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java 2009-01-27
19:27:36 UTC (rev 12445)
@@ -20,16 +20,11 @@
*/
package org.richfaces.realworld.fileupload;
-import java.awt.Graphics2D;
-import java.awt.RenderingHints;
-import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;
@@ -37,7 +32,9 @@
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.ui.graphicImage.Image;
import org.richfaces.realworld.service.Constants;
+import org.richfaces.realworld.util.ImageUtils;
@Name("fileManager")
@Scope(ScopeType.CONVERSATION)
@@ -152,6 +149,9 @@
ImageIO.write(bsrc, Constants.JPG, new File(dest));
return;
}
+
+ BufferedImage bdest = new ImageUtils(bsrc).resizeImage(width, height);
+ /*
BufferedImage bdest = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bdest.createGraphics();
@@ -161,8 +161,9 @@
hints.put(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.addRenderingHints(hints);
g.drawRenderedImage(bsrc, at);
+ */
+ String dest = getUploadRoot() + transformPath(fileName, format);
- String dest = getUploadRoot() + transformPath(fileName, format);
ImageIO.write(bdest, Constants.JPG, new File(dest));
inputStream.close();
}
Added:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java
===================================================================
---
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java
(rev 0)
+++
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java 2009-01-27
19:27:36 UTC (rev 12445)
@@ -0,0 +1,74 @@
+/**
+ *
+ */
+package org.richfaces.realworld.util;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+
+/**
+ * @author Andrey Markavtsov
+ *
+ */
+public class ImageUtils {
+
+ BufferedImage bufferedImage;
+
+ double width;
+ double height;
+
+ public ImageUtils(BufferedImage bufferedImage) {
+ if (bufferedImage == null) {
+ throw new NullPointerException("Buffered image is null");
+ }
+
+ this.height = bufferedImage.getHeight();
+ this.width = bufferedImage.getWidth();
+ this.bufferedImage = bufferedImage;
+ }
+
+ private void scaleToWidth(int width) throws IOException {
+ double height = width * this.height / this.width;
+ BufferedImage newImage = new BufferedImage(width, (int) height,
+ BufferedImage.TYPE_INT_RGB);
+ Graphics2D graphics2D = createGraphics(newImage);
+ graphics2D.drawImage(bufferedImage, 0, 0, (int) width, (int) height,
+ null);
+ bufferedImage = newImage;
+ }
+
+ private void scaleToHeight(int height) throws IOException {
+ double width = height * this.width / this.height;
+ BufferedImage newImage = new BufferedImage((int) width, (int) height,
+ BufferedImage.TYPE_INT_RGB);
+ Graphics2D graphics2D = createGraphics(newImage);
+ graphics2D.drawImage(bufferedImage, 0, 0, (int) width, (int) height,
+ null);
+ bufferedImage = newImage;
+ }
+
+ private Graphics2D createGraphics(BufferedImage image) {
+ Graphics2D graphics2D = image.createGraphics();
+ graphics2D.setBackground(new Color(255, 255, 255));
+ graphics2D.clearRect(0, 0, image.getWidth(), image.getHeight());
+ graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+ RenderingHints.VALUE_INTERPOLATION_BICUBIC);
+ return graphics2D;
+ }
+
+ public BufferedImage resizeImage(double width, double height)
+ throws IOException {
+
+ if (width > height) {
+ scaleToWidth((int) width);
+ } else {
+ scaleToHeight((int) height);
+ }
+
+ return bufferedImage;
+ }
+
+}