Hi Guru's,
I am developing a website for matrimony, i want to add the text (like copyright @ company)
on image when the user uploading their photo. I got one program from internet, it is working
perfectly as a java program, but it doesn't work after i converted into jsp. I couldn't find
solution what is the problem in that code. I am using JDK1.5.0_06 and Tomcat 6.0. Please
look over my two program. Its throw me an error when i execute that jsp program(Watermark.jsp).
The Error is:
Error Occurred : javax.imageio.IIOException: Can't read input file!
Watermark.java
--------------
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Watermark
{
public static void main(String[] args)
{
try
{
Font font;
File _file = new File("before.jpg");
Image src = ImageIO.read(_file);
int width = src.getWidth(null);
int height = src.getHeight(null);
System.out.println("X = "+width+" and Y = "+height);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
// g.setBackground(Color.white);
g.setColor(Color.red);
g.setFont(new Font("Verdana", Font.BOLD, 20));
g.drawString("www.somelink.com", 5, height - (new Font("Arial", Font.BOLD, 20)).getSize() / 2 - 5);
g.dispose();
FileOutputStream out = new FileOutputStream("after.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception ee)
{
System.out.println("Error Occurred : "+ee);
}
}
}Watermark.jsp
-------------
<%@ page language="java" contentType="text/html" import="java.awt.AlphaComposite,java.awt.Color,java.awt.Font,java.awt.Graphics,java.awt.Graphics2D,java.awt.Image,java.awt.image.BufferedImage,java.io.File,java.io.FileOutputStream,javax.imageio.ImageIO,javax.swing.ImageIcon,com.sun.image.codec.jpeg.JPEGCodec,com.sun.image.codec.jpeg.JPEGEncodeParam,com.sun.image.codec.jpeg.JPEGImageEncoder" %>
<%
try
{
Font font;
File _file = new File("before.jpg");
Image src = ImageIO.read(_file);
int width = src.getWidth(null);
int height = src.getHeight(null);
System.out.println("X = "+width+" and Y = "+height);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
// g.setBackground(Color.white);
g.setColor(Color.red);
g.setFont(new Font("Verdana", Font.BOLD, 20));
g.drawString("www.somelink.com", 5, height - (new Font("Arial", Font.BOLD, 20)).getSize() / 2 - 5);
g.dispose();
FileOutputStream fout = new FileOutputStream("after.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fout);
encoder.encode(image);
fout.close();
} catch (Exception ee)
{
out.println("Error Occurred : "+ee);
}
%>Thanks in Advance,
V. Prasath @ Arjunan.
> it doesn't work after i
> it doesn't work after i converted into jsp
Do you mean that the client doesn't see the pic on his/her browser screen?
If you do, it is quite natural because you do not output anything on the response stream.
For example, after you make the after.jpg file on your server, then you should output the string
<IMG SRC="after.jpg">on the response stream to the client.
As an aside, use imagio.write() instead of the ancient encoder APIs for the image output.
Jsp Watermarks
Hi hiwa,
Thanks for your reply,
Yes, The client also view the image after write the watermarks on the image, but it does't create the any image, without creating any image how can i show the image in client browser.
Thanks,
See my first reply. Your
See my first reply.
Your server should send the HTTP response for the image to the client.
I think that the program
I think that the program can't find your "before.jpg".
Try running this :
<%@ page language="java" contentType="text/html" import="java.awt.AlphaComposite,java.awt.Color,java.awt.Font,java.awt.Graphics,java.awt.Graphics2D,java.awt.Image,java.awt.image.BufferedImage,java.io.File,java.io.FileOutputStream,javax.imageio.ImageIO,javax.swing.ImageIcon,com.sun.image.codec.jpeg.JPEGCodec,com.sun.image.codec.jpeg.JPEGEncodeParam,com.sun.image.codec.jpeg.JPEGImageEncoder" %><%
try
{
Font font;
File _file = new File("before.jpg");
Image src = ImageIO.read(_file);
} catch (Exception ee)
{
out.println("Error Occurred : "+ee);
}
%>
If you still see the same exception, it means that your problem is that the file before.jpg is unfound. In that case, you have to be sure that your jsp and your image are deployed under the same directory.
It is really nice
It is really nice information. It is very helpful. Thanks for sharing this knowledge.
Post new comment