CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "ImageResize" AS
import java.sql.Blob;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.RenderingHints;
import java.io.*; 
import java.sql.SQLException;
import oracle.sql.BLOB;
import java.sql.*;

public class ImageResize
{ 

   public static double getImageScale(int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {

		double scalex = (double) targetWidth / sourceWidth;
		double scaley = (double) targetHeight / sourceHeight;
		return Math.min(scalex, scaley);
	}
    
  public static ByteArrayOutputStream resize(Blob image,int width, int height) throws IOException, SQLException
  {
        InputStream in = image.getBinaryStream();
		BufferedImage inputImage = ImageIO.read(in);

        double scale = getImageScale(inputImage.getWidth(),inputImage.getHeight(),width,height);   
        int scaled_width = (int)(scale * inputImage.getWidth());
        int scaled_height = (int)(scale * inputImage.getHeight());
        
		BufferedImage outputImage = new BufferedImage(scaled_width,scaled_height,inputImage.getType());
		Graphics2D g2d = outputImage.createGraphics();
		
		g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g2d.drawImage(inputImage.getScaledInstance(scaled_width, scaled_height, Image.SCALE_SMOOTH),0,0,scaled_width,scaled_height,null);
		g2d.dispose();
						
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(outputImage, "jpeg", baos);
        
        return baos;
  }
  public static int resize_thumbnails(int person_id, Blob image , String mime_type) throws IOException ,SQLException
  {
        ByteArrayOutputStream baos_small  = resize(image,136,136);
        ByteArrayOutputStream baos_medium = resize(image,308,328);
        
        BLOB small_blob,medium_blob;
        PreparedStatement stmt = null;
        try{
        Connection conn = DriverManager.getConnection("jdbc:default:connection");        
        small_blob = BLOB.createTemporary(conn,true,BLOB.DURATION_SESSION);
        small_blob.setBytes(1,baos_small.toByteArray());
        medium_blob = BLOB.createTemporary(conn,true,BLOB.DURATION_SESSION);
        medium_blob.setBytes(1,baos_medium.toByteArray());        
        
        stmt=conn.prepareStatement("update xxas_per_thumbnails set blob_thumb_small=?,blob_thumb_med=?,mime_type=? where person_id=?");    
        stmt.setBlob(1,small_blob);
        stmt.setBlob(2,medium_blob);
        stmt.setString(3,mime_type);
        stmt.setInt(4,person_id);
        
        stmt.execute();
        }
        finally {
            try {
                stmt.close();
            }
            catch (Exception e) {
                System.err.println(e);
            }
        }
        return 1;
	}
    
};
/
COMMIT;
EXIT;