WEB Creating an image in a Java Servlet and displaying it without storing it

Discussion in 'OT Technology' started by Contemptor, Dec 8, 2009.

  1. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    I have a large directory of images on my server. I want to slightly warp these images somehow in my servlet. The servlet currently generates some simple HTML and displays 8 images in it. It needs to warp and then display the warped version of these images. I believe the code for warping I have returns a BufferedImage. So I need to display that in the HTML without storing it. Any suggestions?
     
  2. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    Nobody know how to do this?
     
  3. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    Give me a better description of what you want to do.
     
  4. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    Yea, what exactly is warping..
     
  5. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    PHP would be better for image manipulation and display.
     
  6. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
  7. Supergeek

    Supergeek New Member

    Joined:
    Jan 23, 2007
    Messages:
    1,855
    Likes Received:
    0
    Location:
    Colorado
    PHP's GD functions and/or ImageMagick on the back end are good options. I've worked with both. PHP GD is a bit finicky creating transparent PNGs (you have to do things in a certain order and none of the tutorial pages are quite right; I had to figure it out by trial and error.)

    Taking a shot in the dark here; if you don't want images cached on the client you can just generate a random hash on the filename, like "Image_123_"+rand()*90000+".jpg". I needed to do that for a smiley generator site I created. I know that's not what you asked, but just guessing. Just ignore if it's not.
     
  8. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
  9. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    lol
     
  10. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    I have a large database of images. I want to select random images, and warp them somehow, just so they don't look the same when I display them on the website. It's creating a simple CAPTCHA. The random warping makes it so the images don't look the same each time when displayed. It may be distorted or stretched a little.

    I have some code that should do this, and will create a buffered image object with the warped image. But the warping is rather irrelevant unless someone has an easy way to warp. Ideally, I'd like to display these images in a JSP page, without saving them to the server. So basically, I wanted to know if it was possible to display a bufferedimage on a jsp page or in html created form a java servlet ideally.
     
  11. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    That seems similar to what I'm trying to do, but I'm not sure how it's displaying them on the webpage. I'm not great with anything relating to image processing in Java, or web design itself :hs:. It looks like the output may be just that one image. I need to do this to several images at once and display them, preferably in HTML through a servlet or jsp.

    I realize what I'm asking may not even be possible. If there's a different way I could accomplish it I might be open to if. My web development skills are limited to html/java/servlets/jsp and my friend knows some php. I basically just need to somehow distort/warp images so a person could still recognize them but they wouldn't have the exact same pixel intensities or hash the same.
     
    Last edited: Dec 11, 2009
  12. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    The only relevant part of that post for you is:

    OutputStream out = res.getOutputStream();
    ImageIO.write(croppedImage, IMAGE_TYPE, out);
    out.close();


    The servlet that he created is a direct image handler. So, like it says in the post, he is using just a regular image tag: <img href="yourserver/servlet/image" /> which points to the servlet url.
     
  13. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    So basically if I had a servlet that selected a random image and warped it, I could just call this servlet in multiple image tags and have it display different images? That makes sense. Thanks.

    I would have to do one more thing though that I didn't mention because that's not the mindset I had. The purpose is to make a CAPTCHA. Currently, I have a servlet that selects random images, and passes a list with the object type (classification) of the image to a JSP page. The JSP submits the users classification results to a servlet, which checks to see if the user's input matched the correct classification.

    In other words, if I was displaying one image per instance of the same servlet, I'd have to be able to pass a variable with the type of each image as well. So the servlet needs to select an image from the type 1 category, warp it, then display it. The JSP needs to know it's of type 1 for comparison purposes. Sorry if I'm a bit confusing trying to explain this.
     
  14. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    why reinvent the wheel? you can d/l and use captcha stuff already out there.
     
  15. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    It's for a project. The invention of the CAPTCHA has little to do with our project, but we are trying to make one that's easy to break :o. Our project is just trying to break the CAPTCHA, not create it. We just wanted one in the format that would be easy to process. We have image processing code to try to detect the type of image and break the CAPTCHA. We just wanted to create a captcha page that would display our images, let our software grab them in and try to break it.

    I mean I guess if there's code that does slight warping on a set of images we supply in a web based captcha format, we'd probably be open for it. We were just trying to go beyond the scope of the project a bit. Honestly, I thought it would be easier before I started.
     
  16. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    i know how to do it with php/gd :o
     
  17. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    My friend can do some php, but all my image processing stuff is in Java which is why I kinda need to keep it that way.

    The warping software for java has an output of bufferedimage. I can't find crap on how I can remember what image belongs to what category. I guess I could write cookies or something.
     
  18. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
  19. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    It's a combination of what was in this thread already and the idea I had in mind initailly. The captcha they create is one image, so they can create it with one servlet, and in that servlet store what the actual letters in the generated CAPTCHA is in a session attribute to pass to the display webpage.

    The thing is, I would be running a generate image servlet multiple times to generate different images to go in my CAPTCHA. I'm not sure how I could tie which image is which object type on the server to validate the user's entry. They would name their session attribute, for example, ACTUALCAPTCHA. Pass it in to the JSP display page, and then foward the users input and ACTUALCAPTCHA to java to validate it.

    My layout will be like this in the jsp page. Each one of the captcha servlet calls selects a random image from one or two categories, warps it, and returns the output. Actually while typing this I got an idea. I could generate the JSP image tags in a loop, and for each one I could pass in a variable saying image1, image2, image3. Then for each servlet, I store a session attribute named the number that is passed in as an attribute, with the object type stored in it.

    <img src="captchaservlet?imageNum="1">
    <img src="captchaservlet?imageNum="2">
    <img src="captchaservlet?imageNum="3">
    <img src="captchaservlet?imageNum="4">

    I'll try this all and see what I get, thanks for the input. If anyone sees a flaw in my logic let me know..
     
    Last edited: Dec 11, 2009
  20. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    Got it to work :bowdown:
     
  21. Contemptor

    Contemptor OT Supporter

    Joined:
    Nov 14, 2004
    Messages:
    13,600
    Likes Received:
    2
    Location:
    NC
    I take that back... it kindof works. Not sure why, but my images are randomly truncated sometimes, they don't finish loading basically. I'm not sure why. The concept it self works though. If I click on the image and open it, it will show fully. But when it displays in my table, not so much. Who knows...

    [​IMG]

    I'm guessing this is some kind of problem with the bufferedImage but not sure why.
     
  22. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    ImageForJ is an imagemagik for java. Real simple api
     

Share This Page