|
Subject:
|
Working with images
|
|
Posted By:
|
Pallav
|
Post Date:
|
9/22/2004 7:41:12 AM
|
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 By:
|
gokul_blr
|
Reply Date:
|
9/22/2004 11:45:33 PM
|
Refer to earlier post at : http://p2p.wrox.com/topic.asp?TOPIC_ID=17616
Gokulan Ethiraj
|
|
Reply By:
|
Pallav
|
Reply Date:
|
9/23/2004 12:15:09 AM
|
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 By:
|
Pallav
|
Reply Date:
|
9/24/2004 6:23:44 AM
|
I finally found the solution at the following site:
http://www.geocities.com/marcoschmidt.geo/image-info.html
|
|
Reply By:
|
angrycat
|
Reply Date:
|
9/24/2004 12:25:14 PM
|
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 By:
|
angrycat
|
Reply Date:
|
9/24/2004 12:35:20 PM
|
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?forum=20&thread=505366
|
|
Reply By:
|
angrycat
|
Reply Date:
|
9/24/2004 12:51:43 PM
|
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(resizedImage); 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.getHeight(null)); g.drawImage(image,0,0,null); g.dispose();
return bufferedImage; } }
|