• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying a buffered image in html from a servlet

 
Jake Farson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a CAPTCHA like page that reads in images randomly from a folder on my server, converts them to BufferedImages, applys a slight image warp, then displays them on a webpage as BufferedImages. To do this, I just set it the buffered image in the outputstream in a servlet.


I call this servlet multiple times in image tags in html that is generated from another servlet. The concept works and I don't get any errors, but for some reason my images don't load correctly lots of the time. If I just look at the servlet that generates an image one at a time, there are no issues. I am displaying them in an html table with a specified height width if that could be causing any of my problem. This is an example of my error. Images 1, 2, and 3 are missing parts of the image and have white bars in them.



Any ideas on what could be causing this?

EDIT- I removed the width/height specifications in HTML and it doesn't do it anymore. Any idea why? Any idea how I can get it to work? I want the image to be displayed smaller, but want to get the actual resolution with a right click save as.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jake, welcome to javaranch.

What I understood is that if the servlet handles only one request at a time, then there's no problem. It seems to be a concurrency issue then. How is myBufferedImage declared i.e. is it a local variable or an instance field in the servlet class?? I'm assuming its an instance of java.awt.image.BufferedImage and you are using javax.imageio.ImageIO class to write to the output stream. How are you actually generating the image i.e. read the image and warp it?? Is the code to generate the image thread safe??
 
Jake Farson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Hi Jake, welcome to javaranch.

What I understood is that if the servlet handles only one request at a time, then there's no problem. It seems to be a concurrency issue then. How is myBufferedImage declared i.e. is it a local variable or an instance field in the servlet class?? I'm assuming its an instance of java.awt.image.BufferedImage and you are using javax.imageio.ImageIO class to write to the output stream. How are you actually generating the image i.e. read the image and warp it?? Is the code to generate the image thread safe??

The image is read in from a file, then a warp is performed on it. Your assumptions are correct. I removed all resizing from my html to be generated form my display servlet, and displayed the full size images without error. I'm not sure why resizing in html would cause this though.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed the width/height specifications in HTML and it doesn't do it anymore.

I couldn't understand this statement earlier. So the images distorted only when the browser tries to resize it. The only thing (that I can think of) that will generate this behavior is if the image is corrupt by some means. If there's some problem with the encoding of the image or if the image data is not correct, then resizing the image might create this kind of behavior. The image might seem correct in full size, I've seen this kind of behavior on an image processing software (which one and when is a long story). Can you check if the original images are absolutely fine, try to point the html page to the original images directly and apply resizing through HTML...
 
Jake Farson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
I couldn't understand this statement earlier. So the images distorted only when the browser tries to resize it. The only thing (that I can think of) that will generate this behavior is if the image is corrupt by some means. If there's some problem with the encoding of the image or if the image data is not correct, then resizing the image might create this kind of behavior. The image might seem correct in full size, I've seen this kind of behavior on an image processing software (which one and when is a long story). Can you check if the original images are absolutely fine, try to point the html page to the original images directly and apply resizing through HTML...

I did do this. If I display the pages in my HTML without resizing, it works. I hadn't done this when I first posted my thread, but edited it to post that at the bottom.

For some reason it seems like the resizing in HTML is what causes it but I'm not sure why. It looks like

Removing height and width causes it to display the full images which works without distortion, but I want them resized..
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake, what I meant was try to put the original (unmodified i.e. not warped) bike and plane images in the HTML and resize them in HTML. If they are rendered okay, then there's something wrong with the code that warps the images. Also check if generating multiple warped images is causing the problem or not. So put only 1 warped image in your HTML and refresh it many times. If there's no problem, then your code to warp the images is having concurrency issues. This is all that I can think of as the cause...
 
Jake Farson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Jake, what I meant was try to put the original (unmodified i.e. not warped) bike and plane images in the HTML and resize them in HTML. If they are rendered okay, then there's something wrong with the code that warps the images. Also check if generating multiple warped images is causing the problem or not. So put only 1 warped image in your HTML and refresh it many times. If there's no problem, then your code to warp the images is having concurrency issues. This is all that I can think of as the cause...

One of my coding partners ran it in IE. I didn't think to check. It actually works perfectly in IE and Chrome, but not firefox. Who know's why. Oh well this is just for a demonstration mainly, I'll just run it in IE I suppose.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic