[FileUpload] Resizing JPEG before upload

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[FileUpload] Resizing JPEG before upload

Martin Grogan
Hi all,
I'm new to the list and am hoping someone can help with a file upload problem.
I have successfully installed and used FileUpload to write a JPEG to the server. What I am hoping to do is resize the image before writing it to the server (create thumbnails). I have been searching the internet for days, but with no luck.
Can anyone help me, or point me in the right direction.
Many thanks,
Martin Grogan
Reply | Threaded
Open this post in threaded view
|

Re: [FileUpload] Resizing JPEG before upload

Stefan Rufer
I don't know if I got you right, but you can not resize the JPEG before
uploading.

You have to upload the whole JPEG to the server and do the resize there.
For this check out Java Advanced Imaging I guess, e.g.
   http://forum.java.sun.com/thread.jspa?threadID=177124&messageID=554105
as starting point.

cu
Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [FileUpload] Resizing JPEG before upload

Martin Grogan
Hi Stefan,
Thanks for your quick reply. I have tried to integrate the code on that
site, but without much luck. I have the following fragment as an example. I
must admint, my knowledge of AWT and SWING are not good.
if(contentType.equals("image/pjpeg"))
                {
                    File uploadedFile = new File("output.jpg");
                    ImageIcon myImageIcon = new ImageIcon(item.get());    //
get byts in file item
                    myImageIcon = resizeImage( myImageIcon, 150, 100);    //
i need to scale the image to max 150x100 pixels
                    //  need now to convert myImageIcon back to a JPEG of
size 150x100, but don't know where to start with this.
                    // newitem should be a byte[] ? that has been re-encoded
to JPEG?
                    try{
                        item.write(newitem);
                    }
                    catch(Exception e){out.println("Problem uploading
file");}
                }
This is probably all wrong, and not the way to go about things, but I'm
coding in the dark on this one.
Many thanks,
Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [FileUpload] Resizing JPEG before upload

Martin Grogan
In reply to this post by Stefan Rufer
Here's the code, for resizing JPEG images after they are uploaded, for
anyone that's interested.
Only the relevant portions shown, hope it will be of use to some people.
All the best,
Martin

String contentType = item.getContentType();
                out.println(contentType);
                if(contentType.equals("image/pjpeg"))
                {
                    File uploadedFile = new File("output.jpg");
                    ImageIcon myImageIcon = new ImageIcon(item.get());
                    java.awt.Image image = myImageIcon.getImage();
                    // attempt to code jpeg
                    int thumbWidth=150;
                    int thumbHeight=100;
                    BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
                    java.awt.Graphics2D graphics2D =
thumbImage.createGraphics();
                    graphics2D.setRenderingHint(java.awt.RenderingHints.KEY_INTERPOLATION,java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                    graphics2D.drawImage(image, 0, 0, thumbWidth,
thumbHeight, null);
                    String newfilename=thisuser+"_"+thistime+".jpg";
                    BufferedOutputStream outfile = new
BufferedOutputStream(new FileOutputStream("c:\\"+newfilename));
                    JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(outfile);
                    JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(thumbImage);
                    int quality = 100;
                    param.setQuality((float)quality / 100.0f, false);
                    encoder.setJPEGEncodeParam(param);
                    encoder.encode(thumbImage);
                    outfile.close();
                }

---------------------
----- Original Message -----
From: "Stefan Rufer" <[hidden email]>
To: "Jakarta Commons Users List" <[hidden email]>
Sent: Thursday, June 02, 2005 10:59 AM
Subject: Re: [FileUpload] Resizing JPEG before upload


>I don't know if I got you right, but you can not resize the JPEG before
>uploading.
>
> You have to upload the whole JPEG to the server and do the resize there.
> For this check out Java Advanced Imaging I guess, e.g.
>   http://forum.java.sun.com/thread.jspa?threadID=177124&messageID=554105
> as starting point.
>
> cu
> Stefan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [FileUpload] Resizing JPEG before upload

sabernar
In reply to this post by Martin Grogan
Another solution is to use something like ImageMagick (http://www.imagemagick.org) which gives you a ton of image-related functionality in addition to image resizing.

-Shawn

-----Original Message-----
From: Martin Grogan [mailto:[hidden email]]
Sent: Thursday, June 02, 2005 8:54 AM
To: Jakarta Commons Users List
Subject: Re: [FileUpload] Resizing JPEG before upload


Here's the code, for resizing JPEG images after they are uploaded, for
anyone that's interested.
Only the relevant portions shown, hope it will be of use to some people.
All the best,
Martin

String contentType = item.getContentType();
                out.println(contentType);
                if(contentType.equals("image/pjpeg"))
                {
                    File uploadedFile = new File("output.jpg");
                    ImageIcon myImageIcon = new ImageIcon(item.get());
                    java.awt.Image image = myImageIcon.getImage();
                    // attempt to code jpeg
                    int thumbWidth=150;
                    int thumbHeight=100;
                    BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
                    java.awt.Graphics2D graphics2D =
thumbImage.createGraphics();
                    graphics2D.setRenderingHint(java.awt.RenderingHints.KEY_INTERPOLATION,java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                    graphics2D.drawImage(image, 0, 0, thumbWidth,
thumbHeight, null);
                    String newfilename=thisuser+"_"+thistime+".jpg";
                    BufferedOutputStream outfile = new
BufferedOutputStream(new FileOutputStream("c:\\"+newfilename));
                    JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(outfile);
                    JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(thumbImage);
                    int quality = 100;
                    param.setQuality((float)quality / 100.0f, false);
                    encoder.setJPEGEncodeParam(param);
                    encoder.encode(thumbImage);
                    outfile.close();
                }

---------------------
----- Original Message -----
From: "Stefan Rufer" <[hidden email]>
To: "Jakarta Commons Users List" <[hidden email]>
Sent: Thursday, June 02, 2005 10:59 AM
Subject: Re: [FileUpload] Resizing JPEG before upload


>I don't know if I got you right, but you can not resize the JPEG before
>uploading.
>
> You have to upload the whole JPEG to the server and do the resize there.
> For this check out Java Advanced Imaging I guess, e.g.
>   http://forum.java.sun.com/thread.jspa?threadID=177124&messageID=554105
> as starting point.
>
> cu
> Stefan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]