Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Jsp Watermarks



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);
    }
%>

Let me know if anybody find the solution.

Thanks in Advance,

V. Prasath @ Arjunan.