Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 22nd, 2004, 07:41 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Working with images

 Hi,
I have an application where i allow users to upload images as blob data type to the database...

How do i restrict the size of the image (both pixel size n image size) that is being uploaded by the user...

Preferably i would like the user to be able to upload an image of pixel size(say 100 x 100) or 50Kb ..

And while displaying the image(on a jsp page) i wud lyk to display it as an image of pixel size (say 50 x 50)..

I guess it wud be wiser to store the image(as blob data type) after resizing it to the required display size ie.50 x 50..

Would be grateful if anybody has a solution..

Thnx.
Reply With Quote
  #2 (permalink)  
Old September 22nd, 2004, 11:45 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , India.
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to gokul_blr Send a message via Yahoo to gokul_blr
Default

Refer to earlier post at :
http://p2p.wrox.com/topic.asp?TOPIC_ID=17616


Gokulan Ethiraj
Reply With Quote
  #3 (permalink)  
Old September 23rd, 2004, 12:15 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thnx 4 replyin gokul_blr...

I suppose u misunderstood my requirement..

The post that u r referring 2, deals with displaying images in a JSP page..

Whereas i want 2 restrict the size(both pixel size n byte size) of the image that is being uploaded by the users of my application..

Much like the same way that sites lyk www.distant-help.com restrict the image size of the Avatar tat u upload...

The avatar feature is available in the "user cp(control panel)" section of the afore-mentioned forum..

Hope u understand my requirement now...
Reply With Quote
  #4 (permalink)  
Old September 24th, 2004, 06:23 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I finally found the solution at the following site:

http://www.geocities.com/marcoschmid...mage-info.html
Reply With Quote
  #5 (permalink)  
Old September 24th, 2004, 12:25 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found a solution of sorts.
I upload the file to a byte array, restricting the size of this causes an exception if the file is too big.

Its a bad fix as it does not fail until the file has exceeded the limit, meaning you have already wasted the bandwidth.

I am not sure if the post method gives the file size, its a problem I will re visit when I get time.

Reply With Quote
  #6 (permalink)  
Old September 24th, 2004, 12:35 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry forgot to mention, I found a solution to resize images on the sun java forum, will post code later, also found code to add a watermark if your interested:

http://forum.java.sun.com/thread.jsp...&thread=505366

Reply With Quote
  #7 (permalink)  
Old September 24th, 2004, 12:51 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again,

Heres the code Ive got, Im afraid Ive messed with it a lot, Ive left the commented stuff in just in case you want to play with it.
Sorry its such a mess, cant remember where I got it, think it was on a sun turorial, you will need to get the relevant API's if you do not have them installed.

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;


public class ResizeImageServlet
{

    public final void doPost(String inScale )throws Exception
    {
        try
        {
          int targetWidth=0;
          int targetHeight=0;

          // Get a path to the image to resize.
          // ImageIcon is a kluge to make sure the image is fully
          // loaded before we proceed.
          Image sourceImage = new ImageIcon(Toolkit.getDefaultToolkit().
          getImage("C:\\testImage.jpg")).getImage();

          // Calculate the target width and height
          float scale = Float.parseFloat(inScale)/100;
          targetWidth = (int)(sourceImage.getWidth(null)*scale);
          targetHeight = (int)(sourceImage.getHeight(null)*scale);

          BufferedImage resizedImage = this.scaleImage
          (sourceImage,targetWidth,targetHeight);


          // Output the finished image straight to the response as a JPEG!
          //res.setContentType("image/jpeg");
          //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder

          //InputStream is = new BufferedImag("test");
          //JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);

          ByteArrayOutputStream out = new ByteArrayOutputStream();
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          encoder.encode(resizedImage);
          byte[] b = out.toByteArray();
          //int offset=0;
          //int length = (b, 0, b.length());
          //inputLine = new String (b, 0, 0, length);


          //********************************************

          JPEGCodec.createJPEGEncoder(new FileOutputStream("C:\\testImage.jpg")).encode(resi zedImage);

          for (int i=0; i<200; i++)
          {
              System.out.println(b[i]);
          }

          //InputStream is = new FileInputStream(img);



          //(res.getOutputStream());
          //encoder.encode(resizedImage);

          String databaseDriver = "org.gjt.mm.mysql.Driver";
          Class.forName( databaseDriver );
          String databaseName = "jdbc:mysql://localhost/test";
          Connection con = null;
          con = DriverManager.getConnection(databaseName, "testDb","");

          String str = "INSERT INTO images (detail, image) VALUES (?,?)";
          PreparedStatement pstmt = con.prepareStatement(str);

          pstmt.setString(1, "test Sting");

          //pstmt.setBinaryStream( 2, resizedImage, (int)(resizedImage.length()));
          pstmt.setString( 2, "resizedImage");
          pstmt.executeUpdate();

          pstmt.close();
          pstmt = null;
          con.close();
          con = null;



        }
        catch(Exception e)
        {
            //res.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }

    }

    private BufferedImage scaleImage(Image sourceImage, int width, int height)
    {
        ImageFilter filter = new ReplicateScaleFilter(width,height);
        ImageProducer producer = new FilteredImageSource
        (sourceImage.getSource(),filter);
        Image resizedImage = Toolkit.getDefaultToolkit().createImage(producer);

        return this.toBufferedImage(resizedImage);
    }

    private BufferedImage toBufferedImage(Image image)
    {
        image = new ImageIcon(image).getImage();
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null)
        ,image.getHeight(null),BufferedImage.TYPE_INT_RGB) ;
        Graphics g = bufferedImage.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0,0,image.getWidth(null),image.getHeigh t(null));
        g.drawImage(image,0,0,null);
        g.dispose();

        return bufferedImage;
    }
}

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
Working with TIFF images Freedolen Perl 3 March 16th, 2007 10:55 AM
Php code to put&get images from mysql not working! angelbeans PHP Databases 1 September 8th, 2006 12:44 PM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM
Working with Images Issue Capt D PHP Databases 6 January 31st, 2005 04:49 PM



All times are GMT -4. The time now is 07:29 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.