[Logo] Enterprise Client Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Server side image management, e.g. creation of thumbnails  XML
Forum Index -> Development - Code Snippets
Author Message
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5517
Online

Hi,

the following class is a image-"shrinker" that shows how to create serer side images using Java 2D.

Regards, Björn

Code:
 package org.eclnt.client.util.valuemgmt;
 
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayOutputStream;
 import java.util.Iterator;
 
 import javax.imageio.IIOImage;
 import javax.imageio.ImageIO;
 import javax.imageio.ImageWriteParam;
 import javax.imageio.ImageWriter;
 import javax.imageio.stream.FileImageOutputStream;
 import javax.imageio.stream.ImageOutputStream;
 import javax.imageio.stream.MemoryCacheImageOutputStream;
 import javax.swing.ImageIcon;
 
 import org.eclnt.client.util.file.FileManager;
 import org.eclnt.client.util.log.CLog;
 
 public class ImageShrinker
 {
     public static class ImageShrinkerException extends Exception
     {
         public ImageShrinkerException() { super(); }
         public ImageShrinkerException(String message, Throwable cause) { super(message, cause); }
         public ImageShrinkerException(String message) { super(message); }
         public ImageShrinkerException(Throwable cause) { super(cause); }
     }
     
     public static boolean checkIfShrinkingIsSupported(String extension)
     {
         if (extension == null) return false;
         extension = extension.toLowerCase();
         if (extension.equals("jpg")) return true;
         if (extension.equals("jpeg")) return true;
         if (extension.equals("gif")) return true;
         if (extension.equals("giff")) return true;
         if (extension.equals("png")) return true;
         return false;
     }
     
     public static byte[] shrinkImage(byte[] bytes, int width, int height, String format)
         throws ImageShrinkerException
     {
         return shrinkImage(bytes,width,height,0.85f,format);
     }
     
     public static byte[] shrinkImage(byte[] bytes, int width, int height, float quality, String format)
         throws ImageShrinkerException
     {
         try
         {
             format = format.toLowerCase();
             if ("jpeg".equals(format)) format = "jpg";
             if ("giff".equals(format)) format = "gif";
             ImageIcon originalImage = new ImageIcon(bytes);
             boolean landscape = true;
             if (originalImage.getIconWidth() < originalImage.getIconHeight())
             {
                 int dummy = width;
                 width = height;
                 height = dummy;
                 landscape = false;
             }
             if (originalImage.getIconWidth() <= width && originalImage.getIconHeight() <= height)
                 return bytes;
             BufferedImage newImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
             Graphics g = newImage.getGraphics();
             int newX = 0;
             int newY = 0;
             int newWidth = width;
             int newHeight = height;
             newHeight = Math.round(((float)width / (float)originalImage.getIconWidth()) * (float)originalImage.getIconHeight()); 
             newY = height/2 - newHeight/2;
             if (newY < 0)
             {
                 newY = 0;
                 newHeight = height;
                 newWidth = Math.round(((float)height / (float)originalImage.getIconHeight()) * (float)originalImage.getIconWidth()); 
                 newX = width/2 - newWidth/2;
             }
             if ("jpg".equalsIgnoreCase(format) || "gif".equalsIgnoreCase(format))
             {
                 g.setColor(Color.WHITE);
                 g.fillRect(0,0,width,height);
             }
             g.drawImage(originalImage.getImage(),newX,newY,newWidth,newHeight,null);
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             if ("jpg".equalsIgnoreCase(format))
             {
                 try
                 {
                     Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(format);
                     ImageWriter iw = iter.next();
                     ImageWriteParam iwp = iw.getDefaultWriteParam();
                     iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                     iwp.setCompressionQuality(quality);
                     ImageOutputStream ios = new MemoryCacheImageOutputStream(bos);
                     iw.setOutput(ios);
                     IIOImage image = new IIOImage(newImage, null, null);
                     iw.write(null, image, iwp);
                     iw.dispose();                
                 }
                 catch (Throwable t)
                 {
                     CLog.L.log(CLog.LL_INF,"Using default ImageIO.write, exception: " + t.toString());
                     ImageIO.write(newImage,format,bos);
                 }
             }
             else
             {
                 ImageIO.write(newImage,format,bos);
             }
             bos.close();
             return bos.toByteArray();
         }
         catch (Throwable t)
         {
             CLog.L.log(CLog.LL_INF,"Error occurred when shrinking image",t);
             throw new ImageShrinkerException(t);
         }
     }
     
     public static void main(String[] args)
     {
         try
         {
             byte[] bytes = FileManager.readFile("C:/temp/aaaaademos/images/pictures/adressbuch.gif", true);
             byte[] newBytes = shrinkImage(bytes,100,80,0.8f,"gif");
             FileManager.writeFile("c:/temp/abcdeedcba.gif",newBytes,true);
             System.out.println("OK");
         }
         catch (Throwable t)
         {
             t.printStackTrace();
         }
     }
 }
 

Björn Müller, CaptainCasa GmbH
 
Forum Index -> Development - Code Snippets
Go to:   
Powered by JForum 2.1.6 © JForum Team