participate


Java Programming [Archive] - Stuck on problem - thumbnail image, byte[ ] array, blob, mysql
<<   Back to Forum  |   Give us Feedback
This topic has no replies.
Love2Java
Posts:121
Registered: 11/12/04
Stuck on problem - thumbnail image, byte[ ] array, blob, mysql   
Oct 10, 2005 8:52 AM

 
I'm using the tech tip found on sun's site for "Creating Image Thumbnails" at http://java.sun.com/developer/TechTips/1999/tt1021.html#tip1

I've looked over the code, and now I'm trying to mesh some of the code from that example with the code for my servlet program.

My servlet takes a jpeg image submitted from an html form on a web page (all of which I'm testing on my localhost Tomcat server) and uploads it into my mysql database into a column of type BLOB. I do not upload a reference to a FILE, rather, I upload the BYTES that make up that image.

I wanted to make the images into a thumbnail size PRIOR to uploading them into the db.

My code snippet for my servlet that just uploads the file without modifying its size is:
//.......
boolean isPart = FileUpload.isMultipartContent(req);
	if(isPart)
	{
      DiskFileUpload upload = new DiskFileUpload(); // upload a file from the local disk.
				
      List items = upload.parseRequest(req); // Create a list of all uploaded files.
	Iterator it = items.iterator(); // Create an iterator to iterate through the list.
		while(it.hasNext())
		{
	FileItem item = (FileItem)it.next();
	File f = new File(item1.getName()); // Create a FileItem object to access the file.
	
        byte[] bytes = new byte[(int)f.length()];
		
	FileInputStream fs = new FileInputStream(f);
	BufferedInputStream bis = new BufferedInputStream(fs);
	bis.read(bytes);
						
PreparedStatement stmt = conn.prepareStatement("UPDATE car SET Image1 = ? where Account=1");
	stmt.setBytes(1, bytes);
																	
	int i = stmt1.executeUpdate();
//....


Here is the code from the Tech Tip:
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
 
class Thumbnail {
    public static void main(String[] args) {
        createThumbnail(args[0], args[
          1], Integer.parseInt(args[2]));
    }
 
    /**
     * Reads an image in a file and creates 
     * a thumbnail in another file.
     * @param orig The name of image file.
     * @param thumb The name of thumbnail file.  
     * Will be created if necessary.
     * @param maxDim The width and height of 
     * the thumbnail must 
     * be maxDim pixels or less.
     */
    public static void createThumbnail(
     String orig, String thumb, int maxDim) {
        try {
            // Get the image from a file.
            Image inImage = new ImageIcon(
                          orig).getImage();
 
            // Determine the scale.
	    double scale = (double)maxDim/(
	       double)inImage.getHeight(null);
            if (inImage.getWidth(
             null) > inImage.getHeight(null)) {
                scale = (double)maxDim/(
                double)inImage.getWidth(null);
            }
 
            // Determine size of new image. 
            //One of them
            // should equal maxDim.
            int scaledW = (int)(
             scale*inImage.getWidth(null));
            int scaledH = (int)(
             scale*inImage.getHeight(null));
 
            // Create an image buffer in 
            //which to paint on.
            BufferedImage outImage = 
              new BufferedImage(scaledW, scaledH,
                BufferedImage.TYPE_INT_RGB);
 
            // Set the scale.
            AffineTransform tx = 
              new AffineTransform();
 
            // If the image is smaller than 
            //the desired image size,
            // don't bother scaling.
            if (scale < 1.0d) {
                tx.scale(scale, scale);
            }
 
            // Paint image.
            Graphics2D g2d = 
             outImage.createGraphics();
            g2d.drawImage(inImage, tx, null);
            g2d.dispose();
 
            // JPEG-encode the image 
            //and write to file.
            OutputStream os = 
             new FileOutputStream(thumb);
            JPEGImageEncoder encoder = 
              JPEGCodec.createJPEGEncoder(os);
            encoder.encode(outImage);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
}


I will be changing the following line right off the bat:
1 ) changing the argument passed to:
Image inImage = new ImageIcon(bytes);

I'm passing it my array
bytes
which I created in my upload servlet, it's holding the image
.

I don't want to write the newly thumbnail sized image to a JPEG stream as it is coded at the end of the program from Sun.

Instead, I want to read the newly sized image into a
byte[ ]
array and then continue with my servlet program, and place the image into the db. I have a column of type BLOB waiting for it:)

Does anyone know how to do this?
 
This topic has no replies.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 63
  • Guests : 118

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums