Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert BufferedImage to Blob in Stored procedure

843859May 8 2006 — edited May 9 2006
I've been trying to figure this out for some time now but I just can't get it. I want to have a stored procedure that gets a BLOB containing an image (jpg, png, ...), creates a thumbnail version of the image and returns a BLOB containing the thumbnail version. I'm pretty sure that it works up until I have a BufferedImage with the thumbnail but I'm not sure how to get back into a BLOB. I've tried with the setBytes but it returns null.
Any help would be very much appreciated.

Here is my code:
public class JSProcedure {
    public static java.sql.Blob miniature(int imgID) {
    	java.sql.Blob mini = null;
    	java.sql.Blob blob = null;
    	try{
            java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:default:connection:");
            java.sql.PreparedStatement pstmt = conn.prepareStatement("SELECT image FROM image WHERE id=?");
    		pstmt.setInt(1,imgID);
    		java.sql.ResultSet rs = pstmt.executeQuery();
    		rs.next();
    		blob = rs.getBlob(1);
    		java.io.InputStream in = blob.getBinaryStream();
    		java.awt.image.BufferedImage bi = javax.imageio.ImageIO.read(in);
  
    		double scale = (double) 150 / (double) bi.getHeight(null);
    		if (bi.getWidth(null) > bi.getHeight(null)){
    			scale = (double) 150 / (double) bi.getWidth(null);
   	        }

    	    int scaledW = (int) (scale * bi.getWidth(null));
    	    int scaledH = (int) (scale * bi.getHeight(null));
    		  
    	    java.awt.image.BufferedImage thumb = new    java.awt.image.BufferedImage(scaledW,scaledH,java.awt.image.BufferedImage.TYPE_USHORT_565_RGB);
    	    java.awt.geom.AffineTransform tx = new java.awt.geom.AffineTransform();

    	    if(scale<1.0d){
    	    	tx.scale(scale, scale);
    	    }

    	    java.awt.Graphics2D g2d = thumb.createGraphics();
    	    g2d.drawImage(bi,tx,null);
    	    g2d.dispose();

    	    java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
    	    javax.imageio.ImageIO.write(thumb, "jpg", baos);
    	    
    	    byte[] bytesOut = baos.toByteArray();
    	    // I think this is where I'm getting some problems!!!   	    
    	    mini.setBytes(0,bytesOut);
    	 
           pstmt.close();
            conn.commit();
            conn.close();
        }
        catch(Exception e) {
            System.out.println("Error");
            e.printStackTrace();
        }
        return mini;
    }
}
Then I publish my function with:
CREATE OR REPLACE FUNCTION MINIATURE(num NUMBER) 
    RETURN BLOB
AS
LANGUAGE JAVA
NAME 'JSProcedure.miniature(int) return java.sql.Blob';
/
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 6 2006
Added on May 8 2006
2 comments
1,381 views