Ralph Daugherty skrev:
Hi Thorbjørn, I am already doing what you are suggesting. The class I 
started with is (as I posted back aways) 
org.apache.myfaces.custom.captcha.util and I am bringing in supporting 
classes for generating a PNG text image twisted based on this. It is 
Apache's robust CAPTCHA logic, unmodified.
  
Hi.
Please find below a proof-of-concept stand alone class that generates a 
PNG image.  You need tomahawk.jar, the batik.jars and servlet.jar on the 
classpath.  The DummyHttpServletResposeImplementation nested class is 
needed to wrap the overridden outputStream.
Regards,
/Thorbjørn
foo/Bar.java
====
package foo;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.myfaces.custom.captcha.util.CAPTCHAImageGenerator;
import org.apache.myfaces.custom.captcha.util.CAPTCHATextGenerator;
public class Bar {
   private static class DummyHttpServletResponseImplementation implements
           HttpServletResponse {
       public void setContentType(String arg0) {
       }
       public void setContentLength(int arg0) {
       }
       public PrintWriter getWriter() throws IOException {
           return null;
       }
       public ServletOutputStream getOutputStream() throws IOException {
           return null;
       }
       public String getCharacterEncoding() {
           return null;
       }
       public void setStatus(int arg0, String arg1) {
       }
       public void setStatus(int arg0) {
       }
       public void setIntHeader(String arg0, int arg1) {
       }
       public void setHeader(String arg0, String arg1) {
       }
       public void setDateHeader(String arg0, long arg1) {
       }
       public void sendRedirect(String arg0) throws IOException {
       }
       public void sendError(int arg0, String arg1) throws IOException {
       }
       public void sendError(int arg0) throws IOException {
       }
       public String encodeUrl(String arg0) {
           return null;
       }
       public String encodeURL(String arg0) {
           return null;
       }
       public String encodeRedirectUrl(String arg0) {
           return null;
       }
       public String encodeRedirectURL(String arg0) {
           return null;
       }
       public boolean containsHeader(String arg0) {
           return false;
       }
       public void addCookie(Cookie arg0) {
       }
   }
   /**
    * @param args
    * @throws IOException
    */
   public static void main(String[] args) throws IOException {
       CAPTCHAImageGenerator generator = new CAPTCHAImageGenerator();
       final ByteArrayOutputStream baos = new ByteArrayOutputStream();
       HttpServletResponse response = new 
DummyHttpServletResponseImplementation() {
           ServletOutputStream sos = new ServletOutputStream() {
               @Override
               public void write(int b) throws IOException {
                   baos.write(b);
               }
           };
           @Override
           public ServletOutputStream getOutputStream() throws 
IOException {
               return sos;
           }
       };
       Color[] colors = new Color[] {Color.RED, Color.GREEN, 
Color.PINK, Color.ORANGE, Color.CYAN};
      
       Color startingColor = colors[(int) (Math.random() * colors.length)];
       Color endingColor = colors[(int) (Math.random() * colors.length)];
       generator.generateImage(response, 
CAPTCHATextGenerator.generateRandomText(), startingColor, endingColor);
       System.out.println("PNG image in baos is " + baos.size() + " 
bytes");
       File f = new File("out.png");
       FileOutputStream fos = new FileOutputStream(f);
       fos.write(baos.toByteArray());
       fos.close();
   }
}
====
package foo;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.myfaces.custom.captcha.util.CAPTCHAImageGenerator;
import org.apache.myfaces.custom.captcha.util.CAPTCHATextGenerator;
public class Bar {
	private static class DummyHttpServletResponseImplementation implements
			HttpServletResponse {
		public void setContentType(String arg0) {
		}
		public void setContentLength(int arg0) {
		}
		public PrintWriter getWriter() throws IOException {
			return null;
		}
		public ServletOutputStream getOutputStream() throws IOException {
			return null;
		}
		public String getCharacterEncoding() {
			return null;
		}
		public void setStatus(int arg0, String arg1) {
		}
		public void setStatus(int arg0) {
		}
		public void setIntHeader(String arg0, int arg1) {
		}
		public void setHeader(String arg0, String arg1) {
		}
		public void setDateHeader(String arg0, long arg1) {
		}
		public void sendRedirect(String arg0) throws IOException {
		}
		public void sendError(int arg0, String arg1) throws IOException {
		}
		public void sendError(int arg0) throws IOException {
		}
		public String encodeUrl(String arg0) {
			return null;
		}
		public String encodeURL(String arg0) {
			return null;
		}
		public String encodeRedirectUrl(String arg0) {
			return null;
		}
		public String encodeRedirectURL(String arg0) {
			return null;
		}
		public boolean containsHeader(String arg0) {
			return false;
		}
		public void addCookie(Cookie arg0) {
		}
	}
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		CAPTCHAImageGenerator generator = new CAPTCHAImageGenerator();
		final ByteArrayOutputStream baos = new ByteArrayOutputStream();
		HttpServletResponse response = new DummyHttpServletResponseImplementation() {
			ServletOutputStream sos = new ServletOutputStream() {
				@Override
				public void write(int b) throws IOException {
					baos.write(b);
				}
			};
			@Override
			public ServletOutputStream getOutputStream() throws IOException {
				return sos;
			}
		};
		Color[] colors = new Color[] {Color.RED, Color.GREEN, Color.PINK, Color.ORANGE, Color.CYAN};
		
		Color startingColor = colors[(int) (Math.random() * colors.length)];
		Color endingColor = colors[(int) (Math.random() * colors.length)];
		generator.generateImage(response, CAPTCHATextGenerator.generateRandomText(), startingColor, endingColor);
		System.out.println("PNG image in baos is " + baos.size() + " bytes");
		File f = new File("out.png");
		FileOutputStream fos = new FileOutputStream(f);
		fos.write(baos.toByteArray());
		fos.close();
	}
}
As an Amazon Associate we earn from qualifying purchases.